首页 > 解决方案 > 在 2D 变换后调整 Matplotlib 中的限制

问题描述

我想在从图像执行转换后更改绘图轴的值。没有任何转换,xy 限制只是图像的尺寸。首先,我应用 extent 来更改imshow的限制,但在那之后,我需要使用 matplotlib 进行第二次转换(一个表示缩放和平移的转换矩阵。

我按照以下教程中的步骤进行操作,但我得到的只是一个完全白色的图。如果我不在 imshow 中添加范围部分,我得到的是一个以像素为单位的 xy 坐标图(不需要),但数据绘制在正确的位置(x 轴接近 2e-3 和 4e- 4 为 y 轴)。

xmin, ymin, xmax, ymax = limits # limits are calculated before
fig, ax = plt.subplots(figsize=(12, 8))
ims = ax.imshow(data, clip_on = True, extent = [xmin, xmax, ymin, ymax],  norm = LogNorm(), cmap = colourmap)
trans_data = transform + ax.transData
ims.set_transform(trans_data)
x1, x2, y1, y2 = ims.get_extent()
ax.plot([x1, x2, x2, x1, x1], [y1, y1, y2, y2, y1], "y--", transform = trans_data)

ax.set_xlim(x1,x2)
ax.set_ylim(y1,y2)
plt.xlabel(xlabel)
plt.ylabel(ylabel)
fig.colorbar(ims, ax = ax)
plt.tight_layout()
plt.savefig(plot_title + '.png')
plt.show()

标签: pythonimagematplotlibcgaffinetransform

解决方案


推荐阅读