如何绘制imshow的横截面?

时间:2018-03-11 04:14:38

标签: python python-3.x matplotlib imshow

我在python 3.6中进行数值模拟,并尝试检查2d imshow的横截面。我进行了水平检查,想要垂直,但遇到了一些困难。蓝色检查线对应于“底部”(水平)和“左”(垂直)子图。示例代码(我不允许附加matplotlib图像):

from mpl_toolkits.axes_grid1 import make_axes_locatable
import matplotlib.pyplot as plt
import numpy as np

Array = np.random.rand(100, 100)
grid_points = 100

fig_mpl, ax = plt.subplots(figsize = (10, 10), facecolor = 'white')

line = ax.imshow(Array, cmap = 'hot')
divider = make_axes_locatable(ax)
cax = divider.append_axes("right", size = "5%", pad = 0.05)
caxb = divider.append_axes("bottom", size = "10%", pad = 0.05)
caxl = divider.append_axes("left", size = "10%", pad = 0.05)

bar = fig_mpl.colorbar(line, cax = cax, orientation = 'vertical') 
ax.axhline(grid_points/2)
ax.axvline(grid_points/2)      

X = np.linspace(0, grid_points - 1, grid_points)      
projb, = caxb.plot(X, Array[int(grid_points/2)], color = 'red')
projl, = caxl.plot(X, Array[:, int(grid_points/2)], color = 'red')

caxb.set_ylim(-0.1*np.max(Array), 1.1*np.max(Array))
caxb.set_xlim(0, grid_points - 1)
caxl.set_xlim(-0.1*np.max(Array), 1.1*np.max(Array))
caxl.set_ylim(0, grid_points - 1)

ax.set_xticks([])
ax.set_yticks([])
caxb.set_xticks([])
caxl.set_yticks([])
caxb.set_yticks([np.min(Array), np.max(Array)])
caxl.set_xticks([np.min(Array), np.max(Array)])
caxb.yaxis.tick_right()

for tick in caxl.get_xticklabels():
    tick.set_rotation(-90)

caxb.grid(color = 'black', marker = 8)
caxl.grid(color = 'black', marker = 8)

fig_mpl.subplots_adjust(wspace = 0)
fig_mpl.tight_layout()

我希望 projl 在垂直 caxl 中绘制 Array 的横截面。 有没有正确的方法来做这件事?

1 个答案:

答案 0 :(得分:0)

而不是

projl, = caxl.plot(X, Array[:, int(grid_points/2)], color = 'red')

你需要

projl, = caxl.plot(Array[:, int(grid_points/2)], X, color = 'red')

因为振幅应沿水平(x-)轴和网格索引(X)沿垂直(y-)轴显示。

enter image description here