# 使用 Matplotlib 中的数字的动态子图

[英]Dynamic subplot using Figures in Matplotlib

1. 我的子图 (matplotlib) 中始终最多有 10 行和 1 列
2. 所以假设我在制作 1 plot 之后调用“plt.show()” 我的结果应该只有一个 plot 那一刻占据了整个数字
3. 现在我再添加一个 plot，然后调用“plt.show()”，现在它应该有 2 个地块平均占据无花果，这对于 3、4、5...10 个地块来说是一样的，但所有这些地块都应该有一个单个共享轴，以底部为准
4. 还有一种在任何时间删除 plot 的方法，例如我制作一个 plot 说 axis1 然后我制作一个 plot axis2 现在我想要删除 axis1 之后只有我的 axis2 plot 会占据整个数字

reff1在创建轴后更改 matplotlib 子图大小/位置reff2在 matplotlib 中动态添加/创建子图

``````import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec

number_of_plots = 2
fig = plt.figure()

gs = gridspec.GridSpec(number_of_plots + 1, 1)
plot_space = gs[0:number_of_plots].get_position(fig)
print(plot_space,
[plot_space.x0, plot_space.y0, plot_space.width, plot_space.height])
ax.set_position(plot_space)
ax.set_subplotspec(gs[0:number_of_plots])              # only necessary if using tight_layout()
fig.tight_layout()                # not strictly part of the question

plt.show()
``````

``````import numpy as np
import matplotlib.pyplot as plt
from matplotlib.gridspec import GridSpec
from functools import lru_cache

plt.ion()

def main():

# Data that will be selected
datas = {
"data1": np.random.randn(100),
"data2": np.random.randn(100),
"data3": np.random.randn(100),
"data4": np.random.randn(100),
}

# Emulate data selection over time
states = (
["data1"],
["data1", "data3"],
["data2", "data3"],
["data1", "data2", "data3"],
["data2"],
)

fig = plt.figure(figsize=(6, 8))

# Create a new ax on fig with corresponding data plotted
@lru_cache
def get_data_ax(data_id):
ax.plot(datas[data_id])
ax.set_title(data_id)
return ax

# Emulate data selection over time
for data_state in states:

# Remove all axes, add relevant ones later
for ax in fig.axes:
fig.delaxes(ax)

# Create a GridSpec for the axes to be plot
num_plots = len(data_state)
# but give it at least 2 rows so a single ax will not be stretched
num_rows = max(2, num_plots)
gs = GridSpec(num_rows, 1)

# Get all axes to be plotted
# (thanks to @lru_cache, axes are computed only once)
axes = [get_data_ax(data_id) for data_id in data_state]

# Add each ax one by one
for ax_idx, ax in enumerate(axes):
# Choose the right location for the given ax
ax.set_subplotspec(gs[ax_idx])

plt.pause(2)

if __name__ == "__main__":
main()
``````

[英]how to resize figures within a subplot using matplotlib

2015-02-02 14:01:10 2 3472   subplot
Matplotlib - Subplot中的2个数字 - 1是动画

[英]Matplotlib - 2 Figures in Subplot - 1 is Animation

[英]Inserting data in to matplotlib subplot Figures

2015-04-01 14:06:59 1 44   pandas

[英]subplot error in Matplotlib using seaborn

2016-06-03 01:52:22 1 1363   seaborn
Matplotlib 子图使用嵌套 for 循环

[英]Matplotlib subplot using nested for loop

2021-05-11 21:35:09 1 77   subplot

[英]Draggable Matplotlib Subplot using wxPython

Matplotlib：子图

[英]Matplotlib: subplot

2016-02-19 16:31:40 1 2108   subplot
matplotlib：结合不同的数字并将它们放在一个共同的图例中

[英]matplotlib: combine different figures and put them in a single subplot sharing a common legend

[英]Showing legend for only one subplot using matplotlib