gpt4 book ai didi

python - 使用 matplotlib GridSpec 绘制缩略图

转载 作者:太空宇宙 更新时间:2023-11-03 15:53:45 29 4
gpt4 key购买 nike

a number of questions关于使用 matplotlib 创建“缩略图”图(即较大图的较小版本,其中缩略图图覆盖在原始图上)。

但是,我找不到使用 GridSpec 绘图来实现此目的的方法。据我所知,这是因为 GridSpec 中的轴无法转换(即调整大小和翻译)。

这是重现问题的完整脚本:

import matplotlib
from matplotlib import gridspec, pyplot
from matplotlib.backends.backend_pdf import PdfPages

def add_inset_to_axis(figure, axis, rect):
left, bottom, width, height = rect
def transform(coord):
return figure.transFigure.inverted().transform(
axis.transAxes.transform(coord))
fig_left, fig_bottom = transform((left, bottom))
fig_width, fig_height = transform([width, height]) - transform([0, 0])
return figure.add_axes([fig_left, fig_bottom, fig_width, fig_height])

def main():
pdf = PdfPages('example.pdf')
fig = pyplot.figure()
n_rows, n_cols = 2, 2
x_range = (-100, 100)
outer_grid = gridspec.GridSpec(n_rows, n_cols)
index, row, col = 0, 0, 0
while index < n_rows * n_cols:
data = [x for x in xrange(*x_range)]
grid_cell = outer_grid[row, col]
axis = pyplot.subplot(grid_cell)
axis.plot(range(*x_range), data)
inset = add_inset_to_axis(fig, grid_cell, (0.675, 0.82, 0.3, 0.15))
inset.plot(range(0, 10), data[0:10])
col += 1
if col == 2:
col = 0
row += 1
index = row * 2 + col
pdf.savefig(fig)
pdf.close()

if __name__ == '__main__':
print('Using matplotlib version %s' % matplotlib.__version__)
main()

输出:

Using matplotlib version 1.5.1
Traceback (most recent call last):
File "stackoverflow_inset.py", line 38, in <module>
main()
File "stackoverflow_inset.py", line 26, in main
inset = add_inset_to_axis(fig, grid_cell, (0.675, 0.82, 0.3, 0.15))
File "stackoverflow_inset.py", line 10, in add_inset_to_axis
fig_left, fig_bottom = transform((left, bottom))
File "stackoverflow_inset.py", line 9, in transform
axis.transAxes.transform(coord))
AttributeError: 'SubplotSpec' object has no attribute 'transAxes'

有办法解决这个问题吗?

最佳答案

从函数定义 add_inset_to_axis(figure, axis, rect) 来看,第二个参数实际上是一个 matplotlib.axes 实例。

因此,不应将 grid_cell 作为参数,而是应该使用 axis

inset = add_inset_to_axis(fig, axis, (0.675, 0.82, 0.3, 0.15))

关于python - 使用 matplotlib GridSpec 绘制缩略图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40994070/

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