Matplotlib繪圖是最著名的Python繪圖庫,主要用于二維繪圖,畫圖質(zhì)量高,方便快捷的繪圖模塊;
<1>繪圖API--pyplot模塊
<2>集成庫---pylab模塊(包含Numpy和pyplot中常用的函數(shù))
我們可以打開matplotlib的官網(wǎng)點(diǎn)擊打開鏈接點(diǎn)擊任意一個(gè)圖下面均有代碼,例如:
效果圖如下:
怎么樣很漂亮吧!
2、使用matplotlib畫折線圖使用pyplot:
- import numpy as np
- import matplotlib.pyplot as plt
- x=np.arange(1,4,0.1)
- plt.plot(x,x**2,x,x+2)
很簡單,和matlab很像,plt.plot(x,y)即可;下面是畫出上面圖像的散點(diǎn)圖,加‘o’即可:
- <span style="font-size:18px;">import numpy as np
- import matplotlib.pyplot as plt
- x=np.arange(1,4,0.1)
- plt.plot(x,x**2,'o',x,x+2,'o')</span>
下面我們?cè)佼嫵銎渲幸粋€(gè)的柱狀圖,也特別簡單將plt.plot變?yōu)閜lt.bar即可:
- <span style="font-size:18px;">import numpy as np
- import matplotlib.pyplot as plt
- x=np.arange(1,4,0.1)
- plt.bar(x,x**2)</span>
3、圖的屬性設(shè)置
<1>繪圖顏色和線條類型的改變
- <span style="font-size:18px;">import numpy as np
- import matplotlib.pyplot as plt
- x=np.arange(1,4,0.1)
- plt.plot(x,x**2,'g--',x,x+2,'rD')</span>
- import numpy as np
- import matplotlib.pyplot as plt
- x=np.arange(1,4,0.1)
- plt.plot(x,x**2,'rv',x,x+2,'gp')
可以在shell通過:
import matplotlib.pyplot as plt
help(plt.plot)查找相應(yīng)的顏色和線型
<2>增加文字,橫軸、縱軸、圖
- <span style="font-size:18px;">import numpy as np
- import matplotlib.pyplot as plt
- x=np.arange(1,4,0.1)
- plt.plot(x,x**2,'rv',x,x+2,'gp')
- plt.title('x^2 && x+2')
- plt.xlabel('variable x')
- plt.ylabel('dependent variable y')</span>
<3>其他屬性;圖的大小,圖例
- <span style="font-size:18px;">import numpy as np
- import matplotlib.pyplot as plt
- x=np.arange(1,4,0.1)
- plt.figure(figsize=(8,6),dpi=100)#大小、精度
- plt.plot(x,x**2,color='red',linestyle='--',linewidth=3,label='line1')
- plt.plot(x,x+2,color='blue',linestyle='',marker='*',linewidth=3,label='line2')
- plt.legend(loc='upper left')
- plt.show()</span>
<4>子窗口
- import numpy as np
- import matplotlib.pyplot as plt
- x=np.arange(1,4,0.1)
- plt.figure(figsize=(8,6),dpi=100)
- plt.subplot(211)
- plt.plot(x,x**2,color='red',linestyle='--',linewidth=3,label='line1')
- plt.legend(loc='upper left')
- plt.subplot(212)
- plt.plot(x,x+2,color='blue',linestyle='',marker='*',linewidth=3,label='line2')
- plt.legend(loc='upper left')
- plt.show()
- #這里subplot(211)代表2行1列第1個(gè)圖
- import numpy as np
- import matplotlib.pyplot as plt
- x=np.arange(1,4,0.1)
- plt.figure(figsize=(8,6),dpi=100)
- plt.subplot(121)
- plt.plot(x,x**2,color='red',linestyle='--',linewidth=3,label='line1')
- plt.legend(loc='upper left')
- plt.subplot(122)
- plt.plot(x,x+2,color='blue',linestyle='',marker='*',linewidth=3,label='line2')
- plt.legend(loc='upper left')
- plt.show()
- import numpy as np
- import matplotlib.pyplot as plt
- x=np.arange(1,4,0.1)
- plt.figure(figsize=(8,6),dpi=100)
- plt.subplot(221)
- plt.plot(x,x**2,color='red',linestyle='--',linewidth=3,label='line1')
- plt.legend(loc='upper left')
- plt.subplot(222)
- plt.plot(x,x+2,color='blue',linestyle='',marker='*',linewidth=3,label='line2')
- plt.legend(loc='upper left')
- plt.subplot(223)
- plt.plot(x,x**3,color='black',linestyle='',marker='+',linewidth=3,label='line3')
- plt.legend(loc='upper left')
- plt.subplot(224)
- plt.plot(x,x+9,color='green',linestyle='',marker='p',linewidth=3,label='line4')
- plt.legend(loc='upper left')
- plt.show()
<5>另外一種繪制子圖的方式-axes
- import numpy as np
- import matplotlib.pyplot as plt
- plt.axes([0.1,0.1,0.8,0.8])
- x=np.arange(1,4,0.1)
- plt.plot(x,x**2,color='red',linestyle='--',linewidth=3,label='line1')
- plt.legend(loc='upper left')
- plt.axes([0.55,0.15,0.3,0.3])
- plt.plot(x,x+2,color='blue',linestyle='',marker='*',linewidth=3,label='line2')
- plt.legend(loc='upper left')
- plt.show()
其中plt.axes(距左邊的距離,距底部的距離,圖的寬,圖的高)
|