【问题标题】:How to plot cross-sections of imshow?如何绘制imshow的横截面?
【发布时间】:2018-08-19 08:55:40
【问题描述】:

我正在 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 的横截面。 有没有合适的方法来做这件事?

【问题讨论】:

  • “我没有被允许附加图像” - 假设你的意思是你的代码的图像,那么你不应该首先尝试附加代码的屏幕截图。由于链接中描述的原因,代码图像是一个主要的麻烦和are liable to get you downvoted。发布补充图片很好,但您的代码示例应该作为文本发布在您正在制作的问题/答案中,就像您在此处所做的那样。
  • 我的意思是 matplotlib 图像。 StackOverflow 已限制为信誉低于 10 的用户发布图片。但是感谢您澄清文字!
  • 这是有道理的 - 用括号括起来的语句以及您要发布代码的方式,听起来就像您正在尝试发布代码图像,这是新用户似乎倾向于做的事情.
  • @SergeKonstantinov - 在 cmets 中发布 matplotlib 图片链接,我会将其包含在您的问题中。请注意,如果您在写评论时@我,我只会收到通知。

标签: python python-3.x matplotlib imshow


【解决方案1】:

代替

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-) 轴显示。

【讨论】:

  • 是的,您完全正确,谢谢!我什至在发布之前就这样做了,但由于某种原因它没有帮助。
猜你喜欢
  • 2013-09-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-21
  • 1970-01-01
  • 1970-01-01
  • 2019-06-28
相关资源
最近更新 更多