Matplotlib的安装:
pip install matplotlib
导入Matplotlib模块:
import matplotlib.pyplot as plt
例子:
plt.plot([1,2,3],[5,7,4]);
plt.show()
import matplotlib.pyplot as plt
x=[1,2,3]
y=[5,7,4]
x2=[1,2,3]
y2=[10,14,12]
plt.plot(x,y,label='First Line')
plt.plot(x2,y2,label='Second Line')
plt.xlabel('Plot NUmber')
plt.ylabel('Important var')
plt.title('Interesting Graph\nCheck it out')
plt.legend()
plt.show()
条形图:
import matplotlib.pyplot as plt
plt.bar([1,3,5,7,9],[5,2,7,8,2],label='EXample one')
plt.bar([2,4,6,8,10],[8,6,2,5,6],label='EXample two',color='g')
plt.legend()
plt.xlabel('bar number')
plt.ylabel('bar height')
plt.title('Epic Graph\nAnother Line!Whoa')
plt.show()
直方图:
import matplotlib.pyplot as plt
population_ages=[22,55,62,45,21,22,34,42,42,4,99,102,110,120,121,122,130,111,115,112,80,75,65,54,44,43,42,48]
bins=[0,10,20,30,40,50,60,70,80,90,100,110,120,130]
plt.hist(population_ages,bins,histtype='bar',rwidth=0.8)
plt.xlabel('x')
plt.ylabel('y')
plt.title('Interesting Graph\nCheck it out')
plt.legend()
plt.show()