Matplotlib库的使用
- Matplotlib库由各种可视化类构成,内部结构复杂,受Matlab启发。
- matplotlib.pyplot是绘制各类可视化图形的命令字库,相当于快捷方式。
import matplotlib.pyplot as plt
plt.plot([3,1,4,5,2])
plt.ylabel("Grade")
plt.savefig('test',dip=600) ##存储成PNG的格式,默认PNG格式,可以通过dip修改输出质量
plt.show()
import matplotlib.pyplot as plt
plt.plot([0,2,4,6,8],[3,1,4,5,2])##plt.plot(x,y)当由2各以上参数时,按照X轴和Y轴顺序绘制数据点。
plt.ylabel("Grade")
plt.axis([-1,10,0,6])##是X和Y轴的范围
plt.show()
pyplot的绘图区域:在全域绘图区域中创建一个分区体系,并定位到一个子绘图区域。
plt.subplot(nrows,ncols,plot_number)
plt.subplot(3,2,4)
plt.subplot(324)
举个栗子:
import numpy as np
import matplotlib.pyplot as plt
def f(t):
return np.exp(-t)*np.cos(2*np.pi*t) ##定义一个能量衰减函数
a=np.arange(0.0,5.0,0.02)##随机生成一个数组a
plt.subplot(211)##将绘图区域分成2行一列,选择二第一个区域作图
plt.plot(a,f(a))
plt.subplot(2,1,2)
plt.plot(a,np.cos(2*np.pi*a),"r--")
plt.show()
plt.plot(x,y,format_string,kwargs)
- x:x轴数据,列表或者数组,可选。
- y:y轴数据,列表或数组。
- format_string:控制曲线的格式字符串,可选。
- kwargs:第二组或更多(x,y,format_string)
import matplotlib.pyplot as plt
import numpy as np
a=np.arange(10)
plt.plot(a,a*1.5,a,a*2.5,a,a*3.5,a,a*4.5)
plt.show()
for