python matplotlib畫圖


本文摘自:http://blog.sina.com.cn/s/blog_4b5039210100ie6a.html

Matplotlib.pyplot是用來畫圖的方法,類似於matlab中plot命令,用法基本相同。

一.最基本的:

例如:

In [1]: import matplotlib.pyplot as plt

In [2]: plt.plot([1,2,3])

Out[2]: [<matplotlib.lines.Line2D object at 0x06518A10>]

In [3]: plt.ylabel('some numbers')

Out[3]: <matplotlib.text.Text object at 0x0652D550>

In [4]: plt.show()

結果如圖1

matplotlib.pyplot

    從圖中可以看到,如果我們給plot()參數是一個list或者array,那么畫圖默認是作為Y軸來顯示,x軸是自動生成的數值范圍。

    其實plot可以帶一些參數,和matlab類似。如:plt.plot([1,2,3],[1,4,9])

則會按(1,1),(2,4),(3,9)來划線。

    當然和matlab類似,我們也可以指定線的類型和顏色,如果默認,則為’b-‘,即藍色的實線(如上圖)。

>>> import matplotlib.pyplot as plt

>>> plt.plot([1,2,3,4], [1,4,9,16], 'ro')

[<matplotlib.lines.Line2D object at 0x00D62150>]

>>> plt.axis([0, 6, 0, 20])

[0, 6, 0, 20]

>>> plt.show()

結果如圖2:

matplotlib.pyplot

 

'ro'代表線形為紅色圈。plt.axis([0, 6, 0, 20])是指定xy坐標的起始范圍,它的參數是列表[xmin, xmax, ymin, ymax]。

二,統一圖上畫多條曲線

下面看看如何在同一張圖畫多條曲線,我們用numpy生成的array

>>> import numpy as np

>>> t = np.arange(0.,5.,0.2)

>>> t

array([ 0. ,  0.2,  0.4,  0.6,  0.8,  1. ,  1.2,  1.4,  1.6,  1.8,  2. , 2.2,  2.4,  2.6,  2.8, 3. ,  3.2,  3.4,  3.6,  3.8,  4. ,  4.2, 4.4,  4.6,  4.8])

>>> plt.plot(t,t,'r--',t,t**2,'bs',t,t**3,'g^')

>>> plt.show()

結果如圖3:

 

matplotlib.pyplot

對於線的屬性,我們也可以如下設定:

lines = plt.plot(x1, y1, x2, y2)

# use keyword args

plt.setp(lines, color='r', linewidth=2.0)

# or matlab style string value pairs

plt.setp(lines, 'color', 'r', 'linewidth', 2.0)

 

三,subplot命令

Matplotlib也有和matlab中一樣的subplot命令

>>> import numpy as np

>>> import matplotlib.pyplot as plt

>>> def f(t):

    return np.exp(-t)*np.cos(2*np.pi*t)

 

>>> t1 = np.arange(0.0,5.0,0.1)

>>> t2 = np.arange(0.0,5.0,0.02)

>>> plt.figure(1)

<matplotlib.figure.Figure object at 0x0433FF50>

>>> plt.subplot(211)

<matplotlib.axes.AxesSubplot object at 0x02700830>

>>> plt.plot(t1,f(t1),'bo',t2,f(t2),'k')

[<matplotlib.lines.Line2D object at 0x04463310>, <matplotlib.lines.Line2D object at 0x0447E570>]

>>> plt.subplot(212)

<matplotlib.axes.AxesSubplot object at 0x0447E450>

>>> plt.plot(t2,np.cos(2*np.pi*t2),'r--')

[<matplotlib.lines.Line2D object at 0x04530510>]

>>> plt.show()

結果如圖4:

 

 matplotlib.pyplot

    當然,也可以用figure來畫多個圖。

import matplotlib.pyplot as plt

plt.figure(1) # the first figure

plt.subplot(211) # the first subplot in the first figure

plt.plot([1,2,3])

plt.subplot(212) # the second subplot in the first figure

plt.plot([4,5,6])

plt.figure(2) # a second figure

plt.plot([4,5,6]) # creates a subplot(111) by default

plt.figure(1) # figure 1 current; subplot(212) still current

plt.subplot(211) # make subplot(211) in figure1 current

plt.title('Easy as 1,2,3') # subplot 211 title

 

四,在圖片上標上text。

如:

import numpy as np

import matplotlib.pyplot as plt

 

mu,sigma = 100,15

x = mu + sigma*np.random.randn(10000)

# the histogram of the data

n,bins,patches = plt.hist(x,50,normed=1,facecolor='g',alpha=0.75)

plt.xlabel('Smarts')

plt.ylabel('Probability')

plt.title('Histogram of IQ')

plt.text(60,.025,r'$\mu=100,\ \sigma=15$')

plt.axis([40,160,0,0.03])

plt.grid(True)

plt.show()

結果如圖5:

matplotlib.pyplot

可以看到matplotlib接受Tex的數學公式模式‘$$‘. 如此借助latex,可以在圖形中顯示復雜的數學公式了。

原文鏈接:http://blog.sina.com.cn/s/blog_4b5039210100ie6a.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM