## 在循环中添加动态大小的子图内容来源于 Stack Overflow，并遵循CC BY-SA 3.0许可协议进行翻译与使用

• 回答 (1)
• 关注 (0)
• 查看 (9)

``````        ax = plt.subplot(i)
ax.scatter(y_df['y_pred'], y_df['y_test'])
``````

“三位数，不是{}”。format（args [0]）） ValueError：整数子图规范必须是三位数，而不是1

``````  fig = plt.figure()

tscv = TimeSeriesSplit(n_splits=self.no_splits)
for train_index, test_index in tqdm(tscv.split(X)):
X_train, X_test = X.iloc[train_index], X.iloc[test_index]
y_train, y_test = y.iloc[train_index], y.iloc[test_index]

self.regressor.fit(X_train, y_train.ravel())

# predict y values
y_pred = self.regressor.predict(X_test)

# plot y_pred vs y_test
y_df = pd.DataFrame()
y_pred = y_pred.reshape(len(y_pred), )
y_test = y_test.reshape(len(y_test), )
y_df['y_pred'] = y_pred
y_df['y_test'] = y_test

ax = plt.subplot(i)
ax.scatter(y_df['y_pred'], y_df['y_test'])
``````

``````fig.add_subplot(nrows, ncols, index)
``````

``````for i,ax in enumerate(fig.axes):
if isinstance(ax,matplotlib.axes.SubplotBase):
ax.change_geometry(len(fig.axes),1,i)
``````

``````import matplotlib.pyplot as plt
from matplotlib.widgets import Slider
import numpy as np
import random

def run(val):
n_axes = int(val)
ax_names = random.sample(range(max_size),n_axes)

i=0
for ax_name in range(1,max_size):
# Delete outdated axes
if ax_name not in ax_names and ax_name in my_axes.keys():
fig.delaxes(my_axes[ax_name])
del my_axes[ax_name]
if ax_name in ax_names:
i+=1
#Plot new data on new axes
if ax_name not in my_axes.keys():
print(i,n_axes)
y = np.random.rand(x.shape[0])
ax.plot(x,y)
# Relocate "old" ax to new position
else:
my_axes[ax_name].change_geometry(n_axes,1,i)

fig = plt.figure()
my_axes = {}
x = np.linspace(0,1,100)
max_size=81

ax = plt.axes([0.1, 0.03, 0.8, 0.03], facecolor='#cccc00')
sNum = Slider(ax, '#plots', 1, max_size, valinit=1, valstep=1)
sNum.on_changed(run)
``````

Hyman.W