Python_14-绘图

 1.1      简介... 1

 1.2      应用实例... 2

 1.2.1       实例1. 2

 1.2.2       实例2. 2

 1.3      参考资料... 4

 

 

 

 

 

 

1.1   简介

 matplotlib python最著名的绘图库,它提供了一整套和matlab相似的命令API,十分适合交互式地进行制图。而且也可以方便地将它作为绘图控件,嵌入GUI应用程序中。

 

 

 

http://matplotlib.org/

 

http://my.oschina.net/bery/blog/203595

 

 

 

需要安装(可以从csdn下载Python2.7版本的几个包)

dateutil

pyparsing

 

安装问题:

 

>>>

Traceback (most recent call last):

 

  File "D:/Python279/tdrawf1.py", line 1, in <module>

 

    from matplotlib.matlab import *

 

  File "D:\Python279\lib\site-packages\matplotlib\__init__.py", line 105, in <module>

 

    import six

 

ImportError: No module named six

 

办法:

 

Copy six.py and six.pyc to \Lib.

 

 

 

帮助:

 

http://labix.org/python-dateutil

 

 

 

1.2   应用实例

1.2.1   实例1

 

import os

 

import math

 

import pylab

 

y_values = []

 

x_values = []

 

num = 0.0

 

#collect both num and the sine of num in a list

 

while num < math.pi * 4:

 

    y_values.append(math.sin(num))

 

    x_values.append(num)

 

    num += 0.1

 

 

 

pylab.plot(x_values,y_values,'r*')

 

pylab.show()

 

 

 

1.2.2   实例

 

http://blog.csdn.net/xiaowei_cqu/article/details/8272367

 

 

 

"""

 

Thanks to Tony Yu <tsyu80@gmail.com> for the logo design

 

"""

 

 

 

import numpy as np

 

import matplotlib as mpl

 

import matplotlib.pyplot as plt

 

import matplotlib.cm as cm

 

 

 

mpl.rcParams['xtick.labelsize'] = 10

 

mpl.rcParams['ytick.labelsize'] = 12

 

mpl.rcParams['axes.edgecolor'] = 'gray'

 

 

 

 

 

axalpha = 0.05

 

#figcolor = '#EFEFEF'

 

figcolor = 'white'

 

dpi = 80

 

fig = plt.figure(figsize=(6, 1.1),dpi=dpi)

 

fig.figurePatch.set_edgecolor(figcolor)

 

fig.figurePatch.set_facecolor(figcolor)

 

 

 

 

 

def add_math_background():

 

    ax = fig.add_axes([0., 0., 1., 1.])

 

 

 

    text = []

 

    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.7, 0.2), 20))

 

    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}$",

 

                (0.35, 0.9), 20))

 

    text.append((r"$\int_{-\infty}^\infty e^{-x^2}dx=\sqrt{\pi}$",

 

                (0.15, 0.3), 25))

 

    #text.append((r"$E = mc^2 = \sqrt{{m_0}^2c^4 + p^2c^2}$",

 

    #            (0.7, 0.42), 30))

 

    text.append((r"$F_G = G\frac{m_1m_2}{r^2}$",

 

                (0.85, 0.7), 30))

 

    for eq, (x, y), size in text:

 

        ax.text(x, y, eq, ha='center', va='center', color="#11557c", alpha=0.25,

 

                transform=ax.transAxes, fontsize=size)

 

    ax.set_axis_off()

 

    return ax

 

 

 

def add_matplotlib_text(ax):

 

    ax.text(0.95, 0.5, 'matplotlib', color='#11557c', fontsize=65,

 

               ha='right', va='center', alpha=1.0, transform=ax.transAxes)

 

 

 

def add_polar_bar():

 

    ax = fig.add_axes([0.025, 0.075, 0.2, 0.85], polar=True)

 

 

 

 

 

    ax.axesPatch.set_alpha(axalpha)

 

    ax.set_axisbelow(True)

 

    N = 7

 

    arc = 2. * np.pi

 

    theta = np.arange(0.0, arc, arc/N)

 

    radii = 10 * np.array([0.2, 0.6, 0.8, 0.7, 0.4, 0.5, 0.8])

 

    width = np.pi / 4 * np.array([0.4, 0.4, 0.6, 0.8, 0.2, 0.5, 0.3])

 

    bars = ax.bar(theta, radii, width=width, bottom=0.0)

 

    for r, bar in zip(radii, bars):

 

        bar.set_facecolor(cm.jet(r/10.))

 

        bar.set_alpha(0.6)

 

 

 

    for label in ax.get_xticklabels() + ax.get_yticklabels():

 

        label.set_visible(False)

 

 

 

    for line in ax.get_ygridlines() + ax.get_xgridlines():

 

        line.set_lw(0.8)

 

        line.set_alpha(0.9)

 

        line.set_ls('-')

 

        line.set_color('0.5')

 

 

 

    ax.set_yticks(np.arange(1, 9, 2))

 

    ax.set_rmax(9)

 

 

 

if __name__ == '__main__':

 

    main_axes = add_math_background()

 

    add_polar_bar()

 

    add_matplotlib_text(main_axes)

 

    plt.show()

 

 

 

1.3   三维绘图

 

https://pypi.python.org/pypi/mayavi

 

安装:

 

Prerequisites

 

You must have the following libraries installed before installing the Mayavi project:

 

·         Numpy version 1.1.1 or later

 

·         VTK version 5.0 or later

 

·         wxPython version 2.8 or later

 

·         configobj

 

 

 

http://www.vtk.org/VTK/resources/software.html

 

 

 

 

 

1.4   参考资料

 

 

 

 

 

 

 

http://www.ibm.com/developerworks/cn/linux/l-matplotlib/index.html

 

http://www.cnblogs.com/petali/archive/2009/07/30/1417410.html

 

非常好的资料:2.7下运行有错误提示

 

http://www.ibm.com/developerworks/cn/linux/l-matplotlib/

 

 

 

 

posted @ 2015-12-31 14:55  jiu~  阅读(428)  评论(0编辑  收藏  举报