本文仅作为笔记使用,详细用法详见官方文档
目录
1.导入pyplot
2.常用函数用法
1.plot(x,y,color = 'r',label='图例',linestyle=':',linewidth=2,alpha='0.6')
2.plt.lengend(loc='lower left') 注明图例
3.plt.figure(figsize=(m,n),dpi=q,facecolor='b') 设置图像画布大小
4.plt.xlim(xmin,xmax)
plt.ylim(ymin,ymax)设置x,y轴的显示范围
5.plt.xticks(n,m,rotation=q)
plt.yticks(n,m,rotation=q):设置轴的刻度
6.plt.xlabel("Text",fontsize=m,verticalalignment='top', horizontalalignment='center', rotation='vertical')
plt.ylabel("Text",fontsize=m,verticalalignment='top', horizontalalignment='center', rotation='vertical')
7.plt.grid(linestyle, color,alpha) 给图像画上网格
8.plt.title("标题") 注明标题
9.plt.savefig("./名称.png") 保存图像
代码:
3.其它常用函数
1.plt.axhline(y = 0,c = "r",linestyle = "-",linewidth = 2) 绘制平行于x轴的水平参考线
plt.axvline(y = 0,c = "r",linestyle = "-",linewidth = 2) 绘制平行于y轴的水平参考线
2. plt.axvspan(xmin=2,xmax=5,facecolor=‘b’,alpha=0.2) 绘制垂直于x轴的参考区域
plt.axhspan(xmin=2,xmax=5,facecolor=‘b’,alpha=0.2) 绘制垂直于h轴的参考区域
3.plt.annotate() 添加图形内容细节的指向型注释文本
完整代码:
1.导入pyplot
from matplotlib import pyplot as plt
from matplotlib import pyplot as plt
import random
plt.rcParams['font.sans-serif']=['SimHei']#让中文字符正常显示
x = range(0,101)
y = [random.randint(0,101) for i in range(0,101)]
plt.plot(x,y,color = 'r',label='图例',linestyle=':',linewidth=2,alpha=0.6)
plt.legend() #显示图例
plt.show()
x,y:x,y轴上的数值
color设置颜色:
linestyle设置折线风格:
linewidth 设置线条宽度
label 标记图形内容的标签文本(图例)
alpha 透明度(0,1)
best,upper right,upper left,lower left,lower right,right,center left,center right,lower center,upper center,center
figsize:以英寸为单位的宽高 注:一英寸等于2.54厘米
dpi:图形分辨率
facecolor:背景颜色
xmin:x 轴上的最小值
xmax:x 轴上的最大值
n:列表或range生成的可迭代对象
m:与n一一对应显示在图标上
rotation:刻度文字旋转度数
fontsize:数字或者(small,large,medium)
verticalalignment:距离坐标轴的位置(top,bottom,center,baseline)目前不清楚
hoizontalalignment:位置(center,right,left)
ratation:数字角度或者(vertical,horizontal,vertical)
linestyle,color,alpha同上
也可以输出为svg格式,矢量图放大不会有锯齿
建模过程中不建议直接输出出图片来
from matplotlib import pyplot as plt
import random
plt.rcParams['font.sans-serif']=['SimHei']
x = range(0,101)
y = [random.randint(0,101) for i in range(0,101)]
#y1 = [random.randint(0,101) for i in range(0,101)]
plt.figure(figsize=(10,8),dpi = 80)
plt.plot(x,y,color = 'r',label="图例",linestyle='-',linewidth=2,alpha=0.6)
#plt.plot(x,y1,color = 'b',label='down',linestyle=':',linewidth=2,alpha=0.6)
plt.xticks(x[::5],["粥可温{}".format(i) for i in range(0,101,5)],rotation=45)
plt.xlabel("x标题",fontsize=20,verticalalignment='top', horizontalalignment='center', rotation=0)
plt.yticks([i for i in range(0,101,10)],["辰{}".format(i) for i in range(0,101,10)],rotation=45)
plt.ylabel("y雨")
plt.title("总标题")
plt.grid(linestyle=":",color='b',alpha = 0.5)
plt.legend()
plt.show()
注:y为参考线的起始位置
xmin,xmax:参考区域起始位置和终止位置
facecolor:参考区域填充颜色
alpha:透明度
可以看看下面这个链接
http://t.csdn.cn/wLc5s
from matplotlib import pyplot as plt
import random
plt.rcParams['font.sans-serif']=['SimHei']
x = range(0,101)
y = [random.randint(0,101) for i in range(0,101)]
#y1 = [random.randint(0,101) for i in range(0,101)]
plt.figure(figsize=(10,8),dpi = 80)
plt.plot(x,y,color = 'r',label="图例",linestyle='-',linewidth=2,alpha=0.6)
#plt.plot(x,y1,color = 'b',label='down',linestyle=':',linewidth=2,alpha=0.6)
plt.xticks(x[::5],["粥可温{}".format(i) for i in range(0,101,5)],rotation=45)
plt.xlabel("x标题",fontsize=20,verticalalignment='top', horizontalalignment='center', rotation=0)
plt.yticks([i for i in range(0,101,10)],["辰{}".format(i) for i in range(0,101,10)],rotation=45)
plt.ylabel("y雨")
plt.title("总标题")
plt.axhline(y = 29,c = "y",linestyle = "-",linewidth = 2)
plt.axvspan(xmin=50,xmax=70,facecolor='g',alpha=0.2)
plt.annotate('目标点', xy=(10, 20), xytext=(25, 50),arrowprops=dict(arrowstyle='-|>',connectionstyle='angle3',color='b'))
plt.grid(linestyle=":",color='b',alpha = 0.5)
plt.legend()
plt.show()