pythonmatplot教程_python matplotlib 基本教程

Matplotlib  简介

Matplotlib专门用于2d图表,以渐进、交互方式实现数据可视化。

Matplotlib 三层结构:

容器层(canvas 画板 figure 画布)

辅助显示层:  主要用于添加 图名  坐标抽 图例等

图像层: 绘制不同的图像(plot,scatter。。。。)

绘制流程:  1、创建画布  2、 绘制  3、显示

各种图像: plot  scatter   bar  hist  pie      hist如果20个数据,需要21个区割    x=pd.range(_min,_max,21)

辅助功能:

保存: savefig

xy刻度:  plt.xticks(x,label)     plt.yticks(y,label)

显示网格: plt.grid(true,linestyle='--',alpha=0.5)

添加描述信息:plt.xlabel()   ylabel()  title()

多次plot:多次 plot

图例展示:legend

添加多个绘画区:  subplots(nrows,ncols)

pd的df 的series  需要用在 metplotlib,需要去values  比如:

plt.hist(data['分数'].values,20)

中文乱码的处理正确的处理:

import matplotlib

print (matplotlib.matplotlib_fname())   # 显示 mpl-data  目录路径

print(matplotlib.get_cachedir())  # 缓存目录

1、SimHei.ttf 拷贝到 ttf 文件夹下面,在路径 mpl-data 目录下面 font  ttf

2、使用vim打开mpl-data/matplotlibrc    删除font.family和font.sans-serif两行前的#不删没有效果,并在font.sans-serif后添加SimHei,保存退出

3、需要清除Matplotlib缓存

4、重启jupter  notebook

下面实测没用:

1、plt.rcParams['font.sans-serif'] = ['SimHei']

plt.rcParams['axes.unicode_minus']=False

2、from matplotlib.font_manager import _rebuild

_rebuild()

代码示例:

import matplotlib.pyplot as plt

# 创建画板

plt.figure(figsize=(20,8),dpi=100)

# 绘画

x = [1,2,3,4,5,6]

y = [3,6,3,5,3,10]

plt.plot(x,y)

# 图形保存  必须show之前 因为show会释放资源

plt.savefig('./test.png')

# 显示

plt.show()

082c7f3a9afdcce001dda82f5523e043.png

import random

x = range(60)

y = [random.uniform(10,15) for i in x]

plt.figure(figsize=(20,8),dpi=100)

plt.plot(x,y,label='shanghai')  # 没有label  不能出 legend

# 再画一个图

y2 = [random.uniform(1,3) for i in x]

plt.plot(x,y2,label='beijin')  # 没有label  不能出 legend

# 添加 xy 轴刻度

y_ticks = range(40)

plt.yticks(y_ticks[::5]) # 间隔 5

x_ticks_label = ['11:{}'.format(i) for i in x]

plt.xticks(x[::5],x_ticks_label[::5])

# 添加 网格

plt.grid(True,linestyle='--',alpha=0.6)

# 添加描述信息

plt.xlabel('time')

plt.ylabel('temple')

plt.title('1 hours temple change',fontsize=20)

# color  r g b w c m y k   linestyle  -  -- -. :

# 显示图例

plt.legend() #  loc='best'  0 1 2 3 ....

plt.show()

41e99456695300130134a478aaf84b29.png

x = range(60)

y = [random.uniform(10,15) for i in x]

fig,axes =plt.subplots(nrows=1,ncols=2,figsize=(20,8),dpi=100)

axes[0].plot(x,y,label='shanghai')  # 没有label  不能出 legend

# 再画一个图

y2 = [random.uniform(1,3) for i in x]

axes[1].plot(x,y2,label='beijin')  # 没有label  不能出 legend

# # 添加 xy 轴刻度

y_ticks = range(40)

x_ticks_label = ['11:{}'.format(i) for i in x]

axes[0].set_yticks(y_ticks[::5]) # 间隔 5

axes[0].set_xticks(x[::5])

axes[0].set_xticklabels(x_ticks_label[::5])

axes[1].set_yticks(y_ticks[::5]) # 间隔 5

axes[1].set_xticks(x[::5])

axes[1].set_xticklabels(x_ticks_label[::5])

# # 添加 网格

axes[0].grid(True,linestyle='--',alpha=0.6)

axes[1].grid(True,linestyle='--',alpha=0.6)

# # 添加描述信息

axes[0].set_xlabel('time')

axes[0].set_ylabel('temple')

axes[0].set_title('1 hours temple shanghai change',fontsize=20)

axes[1].set_xlabel('time')

axes[1].set_ylabel('temple')

axes[1].set_title('1 hours temple beijin change',fontsize=20)

# # color  r g b w c m y k   linestyle  -  -- -. :

# # 显示图例

axes[0].legend() #  loc='best'  0 1 2 3 ....

axes[1].legend() #  loc='best'  0 1 2 3 ....

plt.show()

459d222fa372a721734647cbf6e6c716.png

import numpy as np

x = np.linspace(-10,10,1000)

y = np.sin(x)

y1 = np.cos(x)

# 创建画板

plt.figure(figsize=(20,8),dpi=100)

# 绘画

plt.plot(x,y)

plt.plot(x,y1)

plt.grid(True,linestyle='--',alpha=0.6)

# 显示

plt.show()

a430ce993d6876e1f074648093379bea.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值