我看到了一篇有关从Seaborn调色板(Selecting colors from Seaborn palette)更改颜色的帖子,并试图从答案中理解代码。以下是从@ImportanceOfBeingErnest复制的代码:
import matplotlib.pyplot as plt
import seaborn as sns; sns.set(style="white")
import pandas as pd
import numpy as np
df = pd.DataFrame({"cost" : np.random.randn(600),
"rating" : np.random.choice(np.arange(1,6), size=600)})
ratings = np.unique(df.rating.values)
palette = iter(sns.husl_palette(len(ratings)))
f, axes = plt.subplots(ncols=len(ratings), figsize=(15, 4))
sns.despine(left=True)
for (n, rat), ax in zip(df.groupby("rating"), axes):
sns.distplot(rat["cost"], kde=False, color=next(palette), ax=ax, axlabel=f"Rating of {n}")
plt.setp(axes, yticks=[])
plt.tight_layout()
plt.show()
但是,当我尝试通过将plt.subplots行替换为2行来绘制图形时,
f, axes = plt.subplots(ncols=3, nrows=2, figsize=(15, 8))
我有一个错误:
AttributeError: 'numpy.ndarray' object has no attribute 'hist'
我已经阅读了一些书,试图弄清楚plt.subplots()的工作原理,并从以下示例中找到了一些很好的解释:Why do many examples use `fig, ax = plt.subplots()` in Matplotlib/pyplot/python。但是,我仍然不明白该错误。与“ axes”(ax = ax)有关吗?