1.折线图
#绘制折线图
import matplotlib.pyplot as plt
year = [2011,2012,2013,2014]
pop = [1.2,3.4,4.5,6.5]
#折线图绘制函数
plt.plot(year,pop)
plt.show();
2.散点图
#绘制折线图
import matplotlib.pyplot as plt
year = [2011,2012,2013,2014]
pop = [1.2,3.4,4.5,6.5]
#散点图绘制函数
plt.scatter(year,pop)
plt.show();
3.直方图
#绘制直方图
import matplotlib.pyplot as plt
values = [0,1,2,3,4,1,2,3,4,4,5,2,4,1]
#直方图绘制函数,bins为直方图间隔份数
plt.hist(values,bins=10)
plt.show()
4.修饰图
title(’图形名称’) (都放在单引号内)
xlabel(’x轴说明’)
ylabel(’y轴说明’)
text(x,y,’图形说明’)
legend(’图例1’,’图例2’,…)
!!!!!!!!!!!!!!!
#coding=utf-8
import matplotlib.pyplot as plt
year = [1950,1970,1990,2010]
pop = [2.3,3.4,5.8,6.5]
#折线图,实体填充
plt.fill_between(year,pop,0,color='green')
#轴的标签
plt.xlabel('Year')
plt.ylabel('Population')
#轴的标题
plt.title('World Population')
#轴的y刻度
plt.yticks([0,2,4,6,8,10],['0B','2B','4B','6B','8B','10B'])
5.绘制多个子图
import matplotlib.pyplot as plt
import numpy as np
def f(t):
return np.exp(-t) * np.cos(2 * np.pi * t)
t1 = np.arange(0, 5, 0.1)
t2 = np.arange(0, 5, 0.02)
plt.figure(12)
plt.subplot(221)
plt.plot(t1, f(t1), 'bo', t2, f(t2), 'r--')
plt.subplot(222)
plt.plot(t2, np.cos(2 * np.pi * t2), 'r--')
plt.subplot(212)
plt.plot([1, 2, 3, 4], [1, 4, 9, 16])
plt.show()