Draw Line and Bar Chart with Matplotlib 画线和图表

来源:互联网 发布:jquery遍历json集合 编辑:程序博客网 时间:2024/06/03 01:45

做了实验以后,一般需要用图形说明数据的含义,我常用简单的曲线图,和柱状图。


我之前只会用Excel,但是Excel画出来的图总感觉不够专业,在看师弟的presentation的时候,看到他的图画的真漂亮,原来是用matplotlib画出来的。


matplotlib用python画图,简单,专业。


曲线图:


 from pylab import * x = [1000,2000,3000,4000,5000] y1 = [0.011953034,0.007304378,0.005531108,0.004629965,0.004060263] y2 = [0.035454429,0.022320288,0.01772984,0.015249259,0.01372336] y3 = [0.026161949,0.016665585,0.012492591,0.010455093,0.009240063] plot ( x,y1,'-*',label='p(a11)' ) plot ( x,y2,'-.',label='p(a15)' ) plot ( x,y3,'-^',label='p(a9)' ) xlabel('Query Cost') ylabel('Relative Error') title('') legend(('p(a11)','p(a15)', 'p(a9)' )) savefig("12.eps",dpi=(640/8)) 

柱状图:


 from pylab import * import numpy as np import matplotlib.pyplot as plt  N = 10 ind = np.arange(N)  # the x locations for the groups  real = (0.002347311,0.746810124,0.010814069,0.105738581,0.001854101,0.0106588,0.002365578,0.007982683,0.003616868,0.00516043)  width = 0.35       # the width of the bars  fig = plt.figure() ax = fig.add_subplot(111) rects1 = ax.bar(ind, real, width, color='r')  estimate = (0.002356675,0.75296556,0.010905585,0.107045611,0.001877654,0.017312228,0.004060263,0.01372336,0.006420114,0.009240063)  rects2 = ax.bar(ind+width, estimate, width, color='y')  # add some ax.set_ylabel('p(ai)') ax.set_xticks(ind+width) ax.set_xticklabels( ('a32', 'a19', 'a65', 'a40', 'a68','a10', 'a11', 'a15', 'a20', 'a9') )  ax.legend( (rects1[0], rects2[0]), ('real-p(ai)', 'estimate-p(ai)') )  savefig("11.eps",dpi=(640/8)) 

参考资料:


matplotlib使用小结(基本篇):http://flyfeeling.blogbus.com/logs/53148228.html


matplotlib主页:http://matplotlib.org/

0 0
原创粉丝点击