1 回答

TA貢獻1804條經驗 獲得超7個贊
問題是
plt.subplots(2, 3, figsize=(24, 10))
創建兩組 3 個子圖,而不是一組 6 個子圖。
array([[<AxesSubplot:xlabel='radians'>,?<AxesSubplot:xlabel='radians'>,?<AxesSubplot:xlabel='radians'>], ???????[<AxesSubplot:xlabel='radians'>,?<AxesSubplot:xlabel='radians'>,?<AxesSubplot:xlabel='radians'>]],?dtype=object)
axes
使用解壓 中的所有子圖數組axes.ravel()
。numpy.ravel
,它返回一個展平的數組。列表理解也可以工作,
axe = [sub for x in axes for sub in x]
實際上,可以類似地使用
axes.ravel()
、axes.flat
、 和。axes.flatten()
請參閱numpy 中的 flatten 和 ravel 函數有什么區別?&?numpy 之間的 flat 和 ravel() 之間的區別。
將每個圖分配給 中的子圖之一
axe
。
import pandas as pd
import numpy as np
# sinusoidal sample data
sample_length = range(1, 6+1)
rads = np.arange(0, 2*np.pi, 0.01)
data = np.array([np.sin(t*rads) for t in sample_length])
df = pd.DataFrame(data.T, index=pd.Series(rads.tolist(), name='radians'), columns=[f'freq: {i}x' for i in sample_length])
# crate the figure and axes
fig, axes = plt.subplots(2, 3, figsize=(24, 10))
# unpack all the axes subplots
axe = axes.ravel()
# assign the plot to each subplot in axe
for i, c in enumerate(df.columns):
? ? df[c].plot(ax=axe[i])
添加回答
舉報