1 回答

TA貢獻(xiàn)1804條經(jīng)驗(yàn) 獲得超7個(gè)贊
問題是
plt.subplots(2, 3, figsize=(24, 10))
創(chuàng)建兩組 3 個(gè)子圖,而不是一組 6 個(gè)子圖。
array([[<AxesSubplot:xlabel='radians'>,?<AxesSubplot:xlabel='radians'>,?<AxesSubplot:xlabel='radians'>], ???????[<AxesSubplot:xlabel='radians'>,?<AxesSubplot:xlabel='radians'>,?<AxesSubplot:xlabel='radians'>]],?dtype=object)
axes
使用解壓 中的所有子圖數(shù)組axes.ravel()
。numpy.ravel
,它返回一個(gè)展平的數(shù)組。列表理解也可以工作,
axe = [sub for x in axes for sub in x]
實(shí)際上,可以類似地使用
axes.ravel()
、axes.flat
、 和。axes.flatten()
請(qǐng)參閱numpy 中的 flatten 和 ravel 函數(shù)有什么區(qū)別?&?numpy 之間的 flat 和 ravel() 之間的區(qū)別。
將每個(gè)圖分配給 中的子圖之一
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])
添加回答
舉報(bào)