Python Matplotlib-用户更改子批次的数量
在我的代码中,用户应该能够更改图中的子批次数量。因此,首先有两个子批次: 我使用以下代码:Python Matplotlib-用户更改子批次的数量,python,matplotlib,position,axis,figure,Python,Matplotlib,Position,Axis,Figure,在我的代码中,用户应该能够更改图中的子批次数量。因此,首先有两个子批次: 我使用以下代码: ax1 = figure.add_sublots(2,1,1) ax2 = figure.add_sublots(2,1,2) 如果按下加号按钮,则应添加一个子批次: 我该怎么做?有这样的命令吗 ax1.change_subplot(3,1,1) ax2.change_subplot(3,1,2) ax3 = figure.add_sublots(3,1,3) 或者我必须删除所有子批次并重新绘制它
ax1 = figure.add_sublots(2,1,1)
ax2 = figure.add_sublots(2,1,2)
如果按下加号按钮,则应添加一个子批次:
我该怎么做?有这样的命令吗
ax1.change_subplot(3,1,1)
ax2.change_subplot(3,1,2)
ax3 = figure.add_sublots(3,1,3)
或者我必须删除所有子批次并重新绘制它们(我希望避免)?这里有一个选项。可以为每个要显示的子地块创建新的GridSpec,并根据该GridSpec设置轴的位置
import matplotlib.pyplot as plt
from matplotlib.gridspec import GridSpec
from matplotlib.widgets import Button
class VariableGrid():
def __init__(self,fig):
self.fig = fig
self.axes = []
self.gs = None
self.n = 0
def update(self):
if self.n > 0:
for i,ax in zip(range(self.n), self.axes):
ax.set_position(self.gs[i-1].get_position(self.fig))
ax.set_visible(True)
for j in range(len(self.axes),self.n,-1 ):
print(self.n, j)
self.axes[j-1].set_visible(False)
else:
for ax in self.axes:
ax.set_visible(False)
self.fig.canvas.draw_idle()
def add(self, evt=None):
self.n += 1
self.gs= GridSpec(self.n,1)
if self.n > len(self.axes):
ax = fig.add_subplot(self.gs[self.n-1])
self.axes.append(ax)
self.update()
def sub(self, evt=None):
self.n = max(self.n-1,0)
if self.n > 0:
self.gs= GridSpec(self.n,1)
self.update()
fig = plt.figure()
btn_ax1 = fig.add_axes([.8,.02,.05,.05])
btn_ax2 = fig.add_axes([.855,.02,.05,.05])
button_add =Button(btn_ax1, "+")
button_sub =Button(btn_ax2, "-")
grid = VariableGrid(fig)
button_add.on_clicked(grid.add)
button_sub.on_clicked(grid.sub)
plt.show()
我要找的命令是:
ax1.change_geometry(3,1,1)
使用此命令可以重新排列子批次。
我在这里找到了这个解决方案:
此文档可能有助于我了解此文档。它没有写在那里,或者我不明白。可以添加绘图,但不能更改现有绘图的位置。