Python 如何在Matplotlib的子图中单独绘制同一图形?
我正在用Python编写一个程序,生成许多图形。其中一些是有趣的,无论是独立的还是与其他图形相比。生成这些图非常昂贵(就运行时而言),我不想多次生成它们。是否有任何方法可以生成一次绘图,并将其作为子绘图的一部分 我基本上是在寻找一个替代方案:Python 如何在Matplotlib的子图中单独绘制同一图形?,python,matplotlib,subplot,Python,Matplotlib,Subplot,我正在用Python编写一个程序,生成许多图形。其中一些是有趣的,无论是独立的还是与其他图形相比。生成这些图非常昂贵(就运行时而言),我不想多次生成它们。是否有任何方法可以生成一次绘图,并将其作为子绘图的一部分 我基本上是在寻找一个替代方案: #generate standalone graphs pylab.figure() generate_plot0() pylab.figure() generate_plot1() #generate subplot pylab.figure() su
#generate standalone graphs
pylab.figure()
generate_plot0()
pylab.figure()
generate_plot1()
#generate subplot
pylab.figure()
subplot(121)
generate_plot0()
subplot(122)
generate_plot1()
但不调用两次generate_plot0()
和generate_plot1()
有什么好办法吗?一般来说,matplotlib美工师不能位于多个轴中,并且轴不能位于多个图形中。(在某些情况下,您可以打破其中一些规则,但通常不起作用。) 因此,简单的答案是否定的
但是,你可以考虑如下的东西。您可以将有问题的绘图作为子图,然后绑定单击/按键/任意键以隐藏所有其他子图,并使选定的轴临时填充整个图形
举个简单的例子:import numpy as np
import matplotlib.pyplot as plt
def main():
subplots = ZoomingSubplots(2, 2)
colors = ['red', 'green', 'blue', 'cyan']
for ax, color in zip(subplots.axes.flat, colors):
data = (np.random.random(200) - 0.5).cumsum()
ax.plot(data, color=color)
subplots.fig.suptitle('Click on an axes to make it fill the figure.\n'
'Click again to restore it to its original position')
plt.show()
class ZoomingSubplots(object):
def __init__(self, *args, **kwargs):
"""All parameters passed on to 'subplots`."""
self.fig, self.axes = plt.subplots(*args, **kwargs)
self._zoomed = False
self.fig.canvas.mpl_connect('button_press_event', self.on_click)
def zoom(self, selected_ax):
for ax in self.axes.flat:
ax.set_visible(False)
self._original_size = selected_ax.get_position()
selected_ax.set_position([0.125, 0.1, 0.775, 0.8])
selected_ax.set_visible(True)
self._zoomed = True
def unzoom(self, selected_ax):
selected_ax.set_position(self._original_size)
for ax in self.axes.flat:
ax.set_visible(True)
self._zoomed = False
def on_click(self, event):
if event.inaxes is None:
return
if self._zoomed:
self.unzoom(event.inaxes)
else:
self.zoom(event.inaxes)
self.fig.canvas.draw()
if __name__ == '__main__':
main()
初始状态
单击子地块后
一般来说,matplotlib美工师不能位于多个轴中,且轴不能位于多个图形中。(在某些情况下,您可以违反其中一些规则,但通常不起作用。) 因此,简单的答案是否定的
但是,你可以考虑如下的东西。您可以将有问题的绘图作为子图,然后绑定单击/按键/任意键以隐藏所有其他子图,并使选定的轴临时填充整个图形
举个简单的例子:import numpy as np
import matplotlib.pyplot as plt
def main():
subplots = ZoomingSubplots(2, 2)
colors = ['red', 'green', 'blue', 'cyan']
for ax, color in zip(subplots.axes.flat, colors):
data = (np.random.random(200) - 0.5).cumsum()
ax.plot(data, color=color)
subplots.fig.suptitle('Click on an axes to make it fill the figure.\n'
'Click again to restore it to its original position')
plt.show()
class ZoomingSubplots(object):
def __init__(self, *args, **kwargs):
"""All parameters passed on to 'subplots`."""
self.fig, self.axes = plt.subplots(*args, **kwargs)
self._zoomed = False
self.fig.canvas.mpl_connect('button_press_event', self.on_click)
def zoom(self, selected_ax):
for ax in self.axes.flat:
ax.set_visible(False)
self._original_size = selected_ax.get_position()
selected_ax.set_position([0.125, 0.1, 0.775, 0.8])
selected_ax.set_visible(True)
self._zoomed = True
def unzoom(self, selected_ax):
selected_ax.set_position(self._original_size)
for ax in self.axes.flat:
ax.set_visible(True)
self._zoomed = False
def on_click(self, event):
if event.inaxes is None:
return
if self._zoomed:
self.unzoom(event.inaxes)
else:
self.zoom(event.inaxes)
self.fig.canvas.draw()
if __name__ == '__main__':
main()
初始状态
单击子地块后
有一个,它可能会帮助您…将
轴
对象传递到生成
绘图*并使用OO接口而不是pyplot
接口。有一个,它可能会帮助您…将轴
对象传递到生成
绘图*并使用OO接口而不是pyplot
接口。