Matplotlib绘图库初探


Matplotlib是Python的2D&3D绘图库,产生各种已经拷贝格式和交互幻剑中跨平台形式的印刷质量图标。Matplot语法与Matlab相似,绘图绘图功能强大,而且十分容易上手。

“个人永远不能超过集体的力量”(Ken Blanchard)。Python强大的原因之一就在于其开源,有很多优秀的程序员为其提供了丰富的类库。Matplotlib就是其中之一,但他的创始人John D. Hunter英年早逝,在今年8月份死于治疗癌症引起的并发症。向这位优秀的程序员致敬!



安装matplot之前先要安装Numpy

Numpy也是python的一个扩展包,提供基础的科学计算,包括:

  • 强大的N维矩阵对象
  • C/C++ 和 Fortran 代码集成工具
  • 有用的线性代数、傅立叶转换和随机数生成函数
Numpy的下载地址: http://scipy.org/Download
也可以从我的csdn资源下载(附有说明文档):
安装都很简单,一路双击就可以~

以下是一个简单的绘制正弦三角函数y=sin(x)的例子。
[python]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. # plot a sine wave from 0 to 4pi  
  2. from pylab import *  
  3. x_values = arange(0.0, math.pi * 40.01)  
  4. y_values = sin(x_values)  
  5. plot(x_values, y_values, linewidth=1.0)  
  6. xlabel('x')  
  7. ylabel('sin(x)')  
  8. title('Simple plot')  
  9. grid(True)  
  10. savefig("sin.png")  
  11. show()  
效果如图:


pylab的plot函数与matlab很相似,也可以在后面增加属性值,可以用
[python]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. help(pylab.plot)  
查看说明:

例如用‘r*’,即红色,星形来画图:
[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. import os  
  2. import math  
  3. import pylab  
  4. y_values = []  
  5. x_values = []  
  6. num = 0.0  
  7. #collect both num and the sine of num in a list  
  8. while num < math.pi * 4:  
  9.     y_values.append(math.sin(num))  
  10.     x_values.append(num)  
  11.     num += 0.1  
  12.   
  13. pylab.plot(x_values,y_values,'r*')  
  14. pylab.show()  

Matplot中可以使用Latex来编辑公式。比如最上面那个Matplotlib的logo,背景的公式就是使用的Latex:
[python]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. """ 
  2. Thanks to Tony Yu <tsyu80@gmail.com> for the logo design 
  3. """  
  4.   
  5. import numpy as np  
  6. import matplotlib as mpl  
  7. import matplotlib.pyplot as plt  
  8. import matplotlib.cm as cm  
  9.   
  10. mpl.rcParams['xtick.labelsize'] = 10  
  11. mpl.rcParams['ytick.labelsize'] = 12  
  12. mpl.rcParams['axes.edgecolor'] = 'gray'  
  13.   
  14.   
  15. axalpha = 0.05  
  16. #figcolor = '#EFEFEF'  
  17. figcolor = 'white'  
  18. dpi = 80  
  19. fig = plt.figure(figsize=(61.1),dpi=dpi)  
  20. fig.figurePatch.set_edgecolor(figcolor)  
  21. fig.figurePatch.set_facecolor(figcolor)  
  22.   
  23.   
  24. def add_math_background():  
  25.     ax = fig.add_axes([0.0.1.1.])  
  26.   
  27.     text = []  
  28.     text.append((r"$W^{3\beta}_{\delta_1 \rho_1 \sigma_2} = U^{3\beta}_{\delta_1 \rho_1} + \frac{1}{8 \pi 2} \int^{\alpha_2}_{\alpha_2} d \alpha^\prime_2 \left[\frac{ U^{2\beta}_{\delta_1 \rho_1} - \alpha^\prime_2U^{1\beta}_{\rho_1 \sigma_2} }{U^{0\beta}_{\rho_1 \sigma_2}}\right]$", (0.70.2), 20))  
  29.     text.append((r"$\frac{d\rho}{d t} + \rho \vec{v}\cdot\nabla\vec{v} = -\nabla p + \mu\nabla^2 \vec{v} + \rho \vec{g}$",  
  30.                 (0.350.9), 20))  
  31.     text.append((r"$\int_{-\infty}^\infty e^{-x^2}dx=\sqrt{\pi}$",  
  32.                 (0.150.3), 25))  
  33.     #text.append((r"$E = mc^2 = \sqrt{{m_0}^2c^4 + p^2c^2}$",  
  34.     #            (0.7, 0.42), 30))  
  35.     text.append((r"$F_G = G\frac{m_1m_2}{r^2}$",  
  36.                 (0.850.7), 30))  
  37.     for eq, (x, y), size in text:  
  38.         ax.text(x, y, eq, ha='center', va='center', color="#11557c", alpha=0.25,  
  39.                 transform=ax.transAxes, fontsize=size)  
  40.     ax.set_axis_off()  
  41.     return ax  
  42.   
  43. def add_matplotlib_text(ax):  
  44.     ax.text(0.950.5'matplotlib', color='#11557c', fontsize=65,  
  45.                ha='right', va='center', alpha=1.0, transform=ax.transAxes)  
  46.   
  47. def add_polar_bar():  
  48.     ax = fig.add_axes([0.0250.0750.20.85], polar=True)  
  49.   
  50.   
  51.     ax.axesPatch.set_alpha(axalpha)  
  52.     ax.set_axisbelow(True)  
  53.     N = 7  
  54.     arc = 2. * np.pi  
  55.     theta = np.arange(0.0, arc, arc/N)  
  56.     radii = 10 * np.array([0.20.60.80.70.40.50.8])  
  57.     width = np.pi / 4 * np.array([0.40.40.60.80.20.50.3])  
  58.     bars = ax.bar(theta, radii, width=width, bottom=0.0)  
  59.     for r, bar in zip(radii, bars):  
  60.         bar.set_facecolor(cm.jet(r/10.))  
  61.         bar.set_alpha(0.6)  
  62.   
  63.     for label in ax.get_xticklabels() + ax.get_yticklabels():  
  64.         label.set_visible(False)  
  65.   
  66.     for line in ax.get_ygridlines() + ax.get_xgridlines():  
  67.         line.set_lw(0.8)  
  68.         line.set_alpha(0.9)  
  69.         line.set_ls('-')  
  70.         line.set_color('0.5')  
  71.   
  72.     ax.set_yticks(np.arange(192))  
  73.     ax.set_rmax(9)  
  74.   
  75. if __name__ == '__main__':  
  76.     main_axes = add_math_background()  
  77.     add_polar_bar()  
  78.     add_matplotlib_text(main_axes)  
  79.     plt.show()  



(转载请注明作者和出处:http://blog.csdn.net/xiaowei_cqu 未经允许请勿用于商业用途)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值