matplotlib的一些代碼


Matplotlib Python 畫圖教程 (莫煩Python)_演講•公開課_科技_bilibili_嗶哩嗶哩 https://www.bilibili.com/video/av16378354/?from=search&seid=16336534570780842214
"""
畫3D圖""" import numpy as np import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D fig = plt.figure() ax = Axes3D(fig) # X,Y value X = np.arange(-4, 4, 0.25) Y = np.arange(-4, 4, 0.25) X, Y = np.meshgrid(X, Y) R = np.sqrt(X**2 + Y**2) # Z value Z = np.sin(R) # 畫3D,restride為3D圖上每個行寬,cstride為列寬 ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=plt.get_cmap('rainbow')) ax.contourf(X, Y, Z, zdir='z', offset=-2, cmap='rainbow') # zdir為等高線圖與Z軸垂直 ax.set_zlim(-2, 2) plt.show()

 

 

"""subplot另外三種分格方法"""
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec


##### Method 1:subplot2grid
plt.figure()
ax1 = plt.subplot2grid((3, 3), (0, 0), rowspan=1, colspan=3)  # 起點0行0列,跨度1行3列
ax1.plot([1, 2], [1, 2])
ax1.set_title('ax1 title')  # 有ax用set
ax2 = plt.subplot2grid((3, 3), (1, 0), rowspan=1, colspan=2)
ax3 = plt.subplot2grid((3, 3), (1, 2), rowspan=2, colspan=1)
ax4 = plt.subplot2grid((3, 3), (2, 0))
ax5 = plt.subplot2grid((3, 3), (2, 1))

# ##### Method 2:gridspec
plt.figure()
gs = gridspec.GridSpec(3, 3)  # 返回一個對象3行3列的對象
ax1 = plt.subplot(gs[0, :])  # 行列
ax2 = plt.subplot(gs[1, :2])
ax3 = plt.subplot(gs[1:, 2])
ax4 = plt.subplot(gs[2, 0])
ax5 = plt.subplot(gs[2, 1])


##### Method3:easy to define structure
# 返回值是figure和所有的axes.((ax11, ax12), (ax21, ax22))為格式
f, ((ax11, ax12), (ax21, ax22)) = plt.subplots(2, 2, sharex=True, sharey=True)
ax11.scatter([1, 2], [1, 2])


plt.tight_layout()  # 緊湊顯示圖片,居中顯示
plt.show()

 這是非常實用的,因為可以輕松地對axes數組進行索引,就好像是一個二維數組一樣,例如,axes[0, 1]。你還可以通過sharex和sharey指定subplot應該具有相同的X軸或Y軸。在比較相同范圍的數據時,這也是非常實用的,否則,matplotlib會自動縮放各圖表的界限。

 

 

"""圖中圖"""
import matplotlib.pyplot as plt


fig = plt.figure()
x = [1, 2, 3, 4, 5, 6, 7]
y = [1, 3, 4, 2, 5, 8, 6]

left, bottom, widht, height = 0.1, 0.1, 0.8, 0.8  # 按在figure上的比例
ax1 = fig.add_axes([left, bottom, widht, height])
ax1.plot(x, y, 'r')
ax1.set_xlabel('x')
ax1.set_ylabel('y')
ax1.set_title('title')
# inside axes1
left, bottom, widht, height = 0.2, 0.6, 0.25, 0.25
ax1 = fig.add_axes([left, bottom, widht, height])
ax1.plot(x, y, 'b')
ax1.set_xlabel('x')
ax1.set_ylabel('y')
ax1.set_title('title inside1')
# inside axes2
plt.axes([0.62, 0.2, 0.25, 0.25])
plt.plot(y[::-1], x, 'g')  # 默認跟着plt.axes
plt.xlabel('x')
plt.ylabel('y')
plt.title('title inside1')

plt.show()

 

"""次坐標軸"""
import numpy as np
import matplotlib.pyplot as plt


x = np.arange(0, 10, 0.1)
y1 = 0.05*x**2
y2 = -1*y1

fig, ax1 = plt.subplots()
ax2 = ax1.twinx()  # Create a twin Axes sharing the xaxis 共用x軸
ax1.plot(x, y1, 'r-')
ax2.plot(x, y2, 'b--')

ax1.set_xlabel('X data')
ax1.set_ylabel('Y1', color='r', rotation='horizontal')
ax2.set_ylabel('Y2', color='b', rotation='horizontal')

plt.show()

 

"""動畫"""
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import animation

fig, ax = plt.subplots()
x = np.arange(0, 2*np.pi, 0.01)
line, = ax.plot(x, np.sin(x))


def animate(i):
    line.set_ydata(np.sin(x+i/10))
    return line,


def init():
    line.set_ydata(np.sin(x))
    return line,


ani = animation.FuncAnimation(fig=fig, func=animate, frames=100, init_func=init, interval=20, blit=True)
plt.show()

 

科學網—[轉載]利用Python進行數據分析——繪圖和可視化(八)(1) - 郭大龍的博文 http://blog.sciencenet.cn/blog-251664-800766.html

調整subplot周圍的間距

默認情況下,matplotlib會在subplot外圍留下一定的邊距,並在subplot之間留下一定的間距。間距跟圖像的高度和寬度有關,因此,如果你調整了圖像的大小(不管是編程還是手工),間距也會自動調整。利用Figure的subplots_adjust方法可以輕而易舉地修改間距,此外,它也是個頂級函數:

 

  1. In [15]: subplots_adjust(left=None, bottom=None, right=None, top=None, wspace=None, hspace=None)  

wspace和hspace用於控制寬度和高度的百分比,可以用作subplot之間的間距。下面是一個簡單的例子,我們將間距收縮到了0:

 

不難看出,其中的軸標簽重疊了。matplotlib不會檢查標簽是否重疊,所以對於這種情況,你只能自己設定刻度位置和刻度標簽。

 

在線型圖中,非實際數據點默認是按線性方式插值的。可以通過drawstyle選項修改:

  1. In [18]: plt.plot(randn(30).cumsum(), 'ko--')  

  2. Out[18]: [<matplotlib.lines.Line2D at 0xb86924c>]  

  3.  

  4. In [19]: data = randn(30).cumsum()  

  5.  

  6. In [20]: plt.plot(data, 'k--', label='Default')  

  7. Out[20]: [<matplotlib.lines.Line2D at 0xba62c8c>]  

  8.  

  9. In [21]: plt.plot(data, 'k--', drawstyle='steps-post', label='steps-post')  

  10. Out[21]: [<matplotlib.lines.Line2D at 0xba758ac>]  

  11.  

  12. In [22]: plt.legend(loc='best')  

  13. Out[22]: <matplotlib.legend.Legend at 0xba75bcc>  


免責聲明!

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



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