【问题标题】:histogram plot issue for numeric features in dataset [duplicate]数据集中数值特征的直方图问题[重复]
【发布时间】:2021-12-21 01:26:51
【问题描述】:

我正在尝试在这里绘制直方图,但我不断收到错误

numeric_features = ['tenure', 'MonthlyCharges', 'TotalCharges']

cols = 3
rows = int( np.ceil( len(numeric_features)/ cols))

fig, axes = plt.subplots(rows, cols, figsize=(16,16))

for i in range(len(numeric_features)):
    this_row = int(np.floor(i/cols))
    this_col = int(i%cols)
    feat = numeric_features[i]
    this_ax = axes[this_row][this_col]
    this_ax.hist(df_train[feat], bins=25)
    this_ax.set_title(feat)
 
plt.grid()
plt.show()

它会抛出以下错误:

我哪里错了?我只是想绘制数据集的数值。

【问题讨论】:

  • 对于子图中的多个行号,格式不是axes[this_row, this_col]吗?
  • 查看此answer:axes = axes.ravel(),然后查看4.关于将轴压缩到数据,或此answer,它特定于数据帧。跨度>

标签: python pandas dataframe matplotlib


【解决方案1】:

这里发生的情况是,当您确定行值时,您会得到1,因此,轴不是您期望的列表数组,而是轴列表。

打印出你的坐标轴,自己看看。这可能是调试此类问题的最佳方式。查看发生错误的实际变量,以验证您对结构或类型的假设实际上是否正确。你的方法只适用于rows > 1

rows = 1 时,我会让你想出一个方法来解释这个案例,但请随时回复,我(或其他任何人)可以提供更多建议。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-24
    • 1970-01-01
    • 2014-03-13
    • 1970-01-01
    • 1970-01-01
    • 2021-12-19
    相关资源
    最近更新 更多