gpt4 book ai didi

python - 自动使 matplotlib 图像与其他子图标签齐平

转载 作者:行者123 更新时间:2023-12-01 09:05:14 25 4
gpt4 key购买 nike

问题

我正在尝试在一些数据旁边绘制图像。但是,我希望图像扩展为与绘图标签齐平。例如,以下代码(使用 this tutorial image ):

# make the incorrect figure
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
fig = plt.figure(figsize=(8,3))
ax_image = plt.subplot(1,2,1)
plt.imshow(mpimg.imread('stinkbug.png'))
plt.subplot(1,2,2)
plt.plot([0,1],[2,3])
plt.ylabel("y")
plt.xlabel("want image flush with bottom of this label")
fig.tight_layout()
ax_image.axis('off')
fig.savefig("incorrect.png")

产生这个图,带有额外的空白:

incorrect image

对解决方案的黑客尝试

我想要一个不浪费空白的情节。下面的 hacky 代码(类似于 this SO link )可以实现这一点:

# make the correct figure with manually changing the size
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
fig = plt.figure(figsize=(8,3))
ax_image = fig.add_axes([0.02,0,0.45,1.0])
plt.imshow(mpimg.imread('stinkbug.png'))
plt.subplot(1,2,2)
plt.plot([0,1],[2,3])
plt.ylabel("y")
plt.xlabel("want image flush with bottom of this label")
fig.tight_layout()
ax_image.axis('off')
fig.savefig("correct.png")

产生下图:

correct image

问题

有没有什么方法可以绘制与其他子图齐平的图像,而不必手动调整图形大小?

最佳答案

您可以获取右轴的边界框及其标签的并集,并设置左轴的位置,使其从并集边界框的垂直位置开始。以下假设图像具有某种自动方面,即图像向一个方向倾斜。

import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import matplotlib.transforms as mtrans

fig, (ax_image, ax) = plt.subplots(ncols=2, figsize=(8,3))

ax_image.imshow(mpimg.imread('https://matplotlib.org/_images/stinkbug.png'))
ax_image.set_aspect("auto")
ax.plot([0,1],[2,3])
ax.set_ylabel("y")
xlabel = ax.set_xlabel("want image flush with bottom of this label")
fig.tight_layout()
ax_image.axis('off')

fig.canvas.draw()
xlabel_bbox = ax.xaxis.get_tightbbox(fig.canvas.get_renderer())
xlabel_bbox = xlabel_bbox.transformed(fig.transFigure.inverted())
bbox1 = ax.get_position().union((ax.get_position(),xlabel_bbox))
bbox2 = ax_image.get_position()
bbox3 = mtrans.Bbox.from_bounds(bbox2.x0, bbox1.y0, bbox2.width, bbox1.height)
ax_image.set_position(bbox3)

plt.show()

enter image description here

在保持宽高比不变的情况下,您可以让图像在宽度方向上放大。这样做的缺点是它可能会覆盖右轴或超出左图。

import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import matplotlib.transforms as mtrans

fig, (ax_image, ax) = plt.subplots(ncols=2, figsize=(8,3))

ax_image.imshow(mpimg.imread('https://matplotlib.org/_images/stinkbug.png'))
ax.plot([0,1],[2,3])
ax.set_ylabel("y")
xlabel = ax.set_xlabel("want image flush with bottom of this label")
fig.tight_layout()
ax_image.axis('off')

fig.canvas.draw()
xlabel_bbox = ax.xaxis.get_tightbbox(fig.canvas.get_renderer())
xlabel_bbox = xlabel_bbox.transformed(fig.transFigure.inverted())
bbox1 = ax.get_position().union((ax.get_position(),xlabel_bbox))
bbox2 = ax_image.get_position()
aspect=bbox2.height/bbox2.width
bbox3 = mtrans.Bbox.from_bounds(bbox2.x0-(bbox1.height/aspect-bbox2.width)/2.,
bbox1.y0, bbox1.height/aspect, bbox1.height)
ax_image.set_position(bbox3)

plt.show()

enter image description here

关于python - 自动使 matplotlib 图像与其他子图标签齐平,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52118454/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com