我有一个应用程序,其中我有一个图形有九个线图子图(3x3),我想让用户选择一个图表并打开一个小的wx Python应用程序以允许编辑和缩放指定的子图。
是否可以从选定的子图中获取所有信息,即轴标签,轴格式,线条,刻度尺寸,刻度标签等,并在wx应用程序的画布上快速绘制它?
我目前的解决方案太长而且笨重,因为我只是重新做用户选择的情节。我在想这样的事情,但它不能正常运作。
#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)
我觉得这是不可能的,但我想我还是会问。
答案 0 :(得分:4)
不幸的是,在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 is 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 is 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()
答案 1 :(得分:0)
这是一个使用类存储子图(轴)的示例 由用户选择(缩放轴)。从存储的轴开始 您可以获得所需的所有信息。例如,演示 显示了如何缩放和恢复子图(轴)。那你可以 在类中编写其他可以访问所选方法的方法 轴以满足您的需求。
import matplotlib.pyplot as plt
class Demo(object):
""" Demo class for interacting with matplotlib subplots """
def __init__(self):
""" Constructor for Demo """
self.zoomed_axis = None
self.orig_axis_pos = None
def on_click(self, event):
""" Zoom or restore the selected subplot (axis) """
# Note: event_axis is the event in the *un-zoomed* figure
event_axis = event.inaxes
if event_axis is None: # Clicked outside of any subplot
return
if event.button == 1: # Left mouse click in a subplot
if not self.zoomed_axis:
self.zoomed_axis = event_axis
self.orig_axis_pos = event_axis.get_position()
event_axis.set_position([0.1, 0.1, 0.85, 0.85])
# Hide all other axes
for axis in event.canvas.figure.axes:
if axis is not event_axis:
axis.set_visible(False)
else:
self.zoomed_axis.set_position(self.orig_axis_pos)
# Restore all axes
for axis in event.canvas.figure.axes:
axis.set_visible(True)
self.zoomed_axis = None
self.orig_axis_pos = None
else: # any other event in a subplot
return
event.canvas.draw()
def run(self):
""" Run the demo """
fig, axes = plt.subplots(nrows=2, ncols=2)
for axis, color in zip(axes.flat, ['r', 'g', 'b', 'c']):
axis.plot(range(10), color=color)
fig.canvas.mpl_connect('button_press_event', self.on_click)
plt.show()
def main():
""" Main driver """
demo = Demo()
demo.run()
if __name__ == "__main__":
main()