问题是
plt.subplots(2, 3, figsize=(24, 10))
创建两组3个子图,而不是一组6个子图。array([[, , ],
[, , ]], dtype=object)打开所有插曲阵列的
axes
使用axes.ravel()
。numpy.ravel
,它返回一个展平的数组。- 列表理解也可以,
axe = [sub for x in axes for sub in x]
- 将每个图分配给中的一个子图
axe
。 如何解决AttributeError:当绘制子图时,“ numpy.ndarray”对象没有属性“ get_figure”是一个类似的问题。
import pandas as pd
import numpy as npsinusoidal sample data
sample_length = range(1, 6+1)
rads = np.arange(0, 2np.pi, 0.01)
data = np.array([np.sin(trads) 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])



