python画剖面图_如何创建Matplotlib图形与图像和剖面图相匹配?
我無法使用subplot和gridspec生成布局,同時(shí)仍然保留(1)軸的比率和(2)對(duì)軸施加的限制。另一種解決方案是手動(dòng)將軸放置在圖形中,并相應(yīng)地控制圖形的大小(正如您在OP中所提到的)。盡管這比使用subplot和gridspec需要更多的工作,但是這種方法仍然非常簡(jiǎn)單,可以非常強(qiáng)大和靈活地生成復(fù)雜的布局,其中需要對(duì)邊距和軸的位置進(jìn)行精確控制。在
下面是一個(gè)例子,它展示了如何通過將圖形的大小設(shè)置為軸的大小來實(shí)現(xiàn)這一點(diǎn)。相反地,它也有可能在一個(gè)預(yù)定尺寸的圖形中擬合軸。然后使用圖形邊距作為緩沖區(qū)來保持軸的縱橫比。在import numpy as np
import matplotlib.pyplot as plt
plt.close('all')
# generate data
# generate grid and test data
x, y = np.linspace(-3, 3, 300), np.linspace(-1, 1, 100)
X, Y = np.meshgrid(x,y)
def f(x,y) :
return np.exp(-(x**2/4+y**2)/.2)*np.cos((x**2+y**2)*10)**2
data = f(X,Y)
# 2d image plot with profiles
h, w = data.shape
data_ratio = h / float(w)
# create figure
# - define axes lenght in inches
width_ax0 = 8.
width_ax1 = 2.
height_ax2 = 2.
height_ax0 = width_ax0 * data_ratio
# define margins size in inches
left_margin = 0.65
right_margin = 0.2
bottom_margin = 0.5
top_margin = 0.25
inter_margin = 0.5
# - calculate total figure size in inches
fwidth = left_margin + right_margin + inter_margin + width_ax0 + width_ax1
fheight = bottom_margin + top_margin + inter_margin + height_ax0 + height_ax2
fig = plt.figure(figsize=(fwidth, fheight))
fig.patch.set_facecolor('white')
# create axe
ax0 = fig.add_axes([left_margin / fwidth,
(bottom_margin + inter_margin + height_ax2) / fheight,
width_ax0 / fwidth, height_ax0 / fheight])
ax1 = fig.add_axes([(left_margin + width_ax0 + inter_margin) / fwidth,
(bottom_margin + inter_margin + height_ax2) / fheight,
width_ax1 / fwidth, height_ax0 / fheight])
ax2 = fig.add_axes([left_margin / fwidth, bottom_margin / fheight,
width_ax0 / fwidth, height_ax2 / fheight])
# plot data
bounds = [x.min(),x.max(),y.min(),y.max()]
ax0.imshow(data, cmap='gray', extent = bounds, origin='lower')
ax1.plot(data[:,w/2],Y[:,w/2],'.',data[:,w/2],Y[:,w/2])
ax1.invert_xaxis()
ax2.plot(X[h/2,:], data[h/2,:], '.', X[h/2,:], data[h/2,:])
plt.show(block=False)
fig.savefig('subplot_layout.png')
結(jié)果是:
總結(jié)
以上是生活随笔為你收集整理的python画剖面图_如何创建Matplotlib图形与图像和剖面图相匹配?的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 有效括号 python_python 有
- 下一篇: tp5数组为什么要中括号_VBA数组与字