gpt4 book ai didi

python - Matplotlib:从多个子图中抓取单个子图

转载 作者:太空狗 更新时间:2023-10-29 21:53:33 47 4
gpt4 key购买 nike

我有一个应用程序,其中有一个图形和九个线图子图 (3x3),我想让用户选择其中一个图表并打开一个小型 wx Python 应用程序以允许编辑和缩放指定的子图。

是否可以从选定的子图中获取所有信息,即轴标签、轴格式、线条、刻度大小、刻度标签等,并在 wx 应用程序的 Canvas 上快速绘制出来?

我目前的解决方案太长太笨重,因为我只是重做用户选择的情节。我在想这样的事情,但它不太正确。

#ax is a dictionary containing each instance of the axis sub-plot
selected_ax = ax[6]
wx_fig = plt.figure(**kwargs)
ax = wx_fig.add_subplots(111)
ax = selected_ax
plt.show()

有没有一种方法可以将 getp(ax) 中的属性保存到一个变量中,并使用该变量的选定属性和 setp(ax) 来构建一个新图表?考虑到调用 getp(ax) 时打印的速度,我觉得必须以某种方式访问​​这些数据,但我什至无法让以下代码在具有两个 y 轴的轴上工作:

label = ax1.yaxis.get_label()
ax2.yaxis.set_label(label)

我觉得这不可能,但我还是想问问。

最佳答案

不幸的是,在 matplotlib 中很难克隆轴或在多个轴之间共享艺术家。 (也不是完全不可能,但是重做剧情会更简单。)

但是,像下面这样的东西呢?

当您左键单击一个子图时,它将占据整个图形,而当您右键单击时,您将“缩小”以显示其余的子图...

import matplotlib.pyplot as plt

def main():
fig, axes = plt.subplots(nrows=2, ncols=2)
for ax, color in zip(axes.flat, ['r', 'g', 'b', 'c']):
ax.plot(range(10), color=color)
fig.canvas.mpl_connect('button_press_event', on_click)
plt.show()

def on_click(event):
"""Enlarge or restore the selected axis."""
ax = event.inaxes
if ax is None:
# Occurs when a region not in an axis is clicked...
return
if event.button == 1:
# On left click, zoom the selected axes
ax._orig_position = ax.get_position()
ax.set_position([0.1, 0.1, 0.85, 0.85])
for axis in event.canvas.figure.axes:
# Hide all the other axes...
if axis is not ax:
axis.set_visible(False)
elif event.button == 3:
# On right click, restore the axes
try:
ax.set_position(ax._orig_position)
for axis in event.canvas.figure.axes:
axis.set_visible(True)
except AttributeError:
# If we haven't zoomed, ignore...
pass
else:
# No need to re-draw the canvas if it's not a left or right click
return
event.canvas.draw()

main()

关于python - Matplotlib:从多个子图中抓取单个子图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9012081/

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