前言
matplotlib是python的一个绘图库,如果你没有绘制过图,可以先试试js的绘图库http://www.runoob.com/highcharts/highcharts-line-labels.html,再来看本章.
安装
pip install matplotlib
matplotlib常用属性
import matplotlib.pyplot as plt plt.xticks([x坐标],[标签字符串])#设置x坐标点标签的 #x、y轴标签与图形标题 plt.xlabel('x主题类别') plt.ylabel('y主题') plt.title('主标题') #图形数据标签点添加数据 for a,b in zip([12,13,14,15,16,17],[20.1,30.2,10,2,3,5.6]): plt.text(a, b+0.1, '%.0f(min)' % b, ha='right', va= 'bottom',fontsize=7) #a是x,b是y,text参数分别是x,y,数据标签值(在x和y相交的点绘制数据) plt.pie(),plt.bar(),plot() #饼子图,柱子图,曲线图 plt.legend(['不值得买','值得买'],loc='upper right',fontsize=10) #标签格,必须在图的下方声明 plt.grid() #出网格线 plt.ylim(0,270)#y轴范围
饼子图
import matplotlib.pyplot as plt
labels = ['usa','bj','sh','China'] #标签 quants = [100,200,300,400] #标签数据 colors = ["pink","coral","yellow","orange","red"] #调整图形高度,对饼子图有效 plt.figure(1, figsize=(6,6)) #饼子图爆炸 def explode(label, target='China'): if label == target: return 0.1 else: return 0 expl = map(explode,labels) plt.pie(quants, explode=expl, colors=colors, labels=labels, autopct='%1.1f%%',pctdistance=0.7, shadow=True) plt.show()
曲线图
import matplotlib.pyplot as plt
for a,b in zip([12,13,14,15,16,17],[20.1,30.2,10,2,3,5.6]):
plt.text(a, b+0.1, '%.0f(min)' % b, ha='right', va= 'bottom',fontsize=7)
plt.xticks([12,13,14,15,16,17],['a','b','c','d','e','f'])
plt.plot([12,13,14,15,16,17],[20.1,30.2,10,2,3,5.6]) #plt.plot([x坐标],[y坐标]
plt.show()
画出柱状图
#plt.bar(x,y,width = 0.35,align='center',color = 'c',alpha=0.8)
import matplotlib.pyplot as plt for a,b in zip([12,13,14,15,16,17],[20.1,30.2,10,2,3,5.6]): plt.text(a, b+0.1, '%.0f(min)' % b, ha='right', va= 'bottom',fontsize=7) plt.xticks([12,13,14,15,16,17],['a','b','c','d','e','f']) plt.bar([12,13,14,15,16,17],[20.1,30.2,10,2,3,5.6],width=0.35,align="center",color="y",alpha=0.8) plt.show()
累积柱状图
是基于柱壮图用不同颜色表示,标签值是两个for.
总结:
来源参考如下连接:
http://www.jianshu.com/p/5ae17ace7984
http://blog.csdn.net/blog_empire/article/details/42393609