matplotlib最重要的plot()函数,
调用方法
plt.plot(x, y, format_string, **kwargs)
参数值:
#x : X轴数据,列表或数组
#y : Y轴数据,列表或数组
#format_string : 控制曲线的格式字符串,可选:
#**kwargs :第二组或更多(x,y,format_string)
其中要说明的是format_string,包含的主要类型有
颜色字符:‘b’,‘k’,‘g’代表各种颜色缩写
风格字符:’-’,’–‘等
标记字符:每个数据点的标志方式,’.’,’*’,'o’等
还要很多其他的参数值,到时候查文档
,
import matplotlib.pyplot as plt
import numpy as np
plt.plot([2,3,1,2,3])
#绘图接收一个list,如果只有一个列表默认其为Y轴,X轴数据为其索引值,从0开始
plt.show()
#x轴默认从0开始
plt.plot([0,1,2],[1,1,3])#两个列表,前一个为X值,后一个为对应的Y值
plt.show()
plt.plot([0,1,2,4,6,5,9,2,4],[8,1,3,4,3,6,7,6,0])
plt.plot(np.arange(0,9),np.random.randint(1,9,size=9),'r')
#变量X、Y个数必须相等
plt.show
<function matplotlib.pyplot.show(*args, **kw)>
添加图例
plt.plot(np.arange(0,10),np.random.randn(10),'r',label='red')
plt.plot(range(0,10),np.random.rand(10),'g',label='green')
#label标签
plt.legend()
#使用legend显示标签、loc参数设置标签位置
plt.show
<function matplotlib.pyplot.show(*args, **kw)>
plt.plot(np.arange(0,10),np.random.randn(10),'r',label='red')
plt.plot(range(0,10),np.random.rand(10),'g',label='green')
#label标签
plt.legend(loc='upper right')
#使用legend显示标签、参数设置标签位置
plt.show
<function matplotlib.pyplot.show(*args, **kw)>
参数设置
‘best’ : 0, (only implemented for axes legends)(自适应方式)
‘upper right’ : 1,
‘upper left’ : 2,
‘lower left’ : 3,
‘lower right’ : 4,
‘right’ : 5,
‘center left’ : 6,
‘center right’ : 7,
‘lower center’ : 8,
‘upper center’ : 9,
‘center’ : 10,
import matplotlib.pyplot as plt
import numpy as np
plt.plot(np.arange(0,10),np.random.randn(10),'r',label='red')
plt.plot(range(0,10),np.random.rand(10),'g',label='green')
#label标签
plt.title('An Example Axes')#设置图像标题
plt.xlabel('X-Axis')#设置X轴名称
plt.ylabel('Y-Axis')#设置Y轴名称
plt.legend(loc='upper right')
#使用legend显示标签、参数设置标签位置
plt.show
<function matplotlib.pyplot.show(*args, **kw)>
如显示错误“‘str’ object is not callable”重启编译器即可。