【问题标题】:Plot categorical data with matplotlib - transposed pandas dataframe使用 matplotlib 绘制分类数据 - 转置 pandas 数据框
【发布时间】:2018-08-17 07:32:01
【问题描述】:

我在 pandas 数据框中有两个组的数据,每个组的 3 个不同项目的平均值:

        item1       item2       item3   
group                   
1       2.807692    3.115385    3.923077    
2       2.909091    2.454545    3.909091    

我想在条形图中绘制两组的均值。我找到了一些代码来做到这一点 here 具有以下功能:

def groupedbarplot(x_data, y_data_list, y_data_names, colors, x_label, y_label, title):
_, ax = plt.subplots()
# Total width for all bars at one x location
total_width = 0.5
# Width of each individual bar
ind_width = total_width / len(y_data_list)
# This centers each cluster of bars about the x tick mark
alteration = np.arange(-(total_width/2), total_width/2, ind_width)
    # Draw bars, one category at a time
for i in range(0, len(y_data_list)):
    # Move the bar to the right on the x-axis so it doesn't
    # overlap with previously drawn ones
    ax.bar(x_data + alteration[i], y_data_list[i], color = colors[i], label = y_data_names[i], width = ind_width)
ax.set_ylabel(y_label)
ax.set_xlabel(x_label)
ax.set_title(title)
ax.legend(loc = 'upper right')

这对于提到的数据框来说工作得非常好,但是,我不想将所有项目的栏按组分组在一起,而是希望将这些栏按项目分组,这样我就可以看到每个项目的每个组的差异。因此,我已经转置了数据框,但这在绘图时给了我错误:

groupedbarplot(x_data = data.index.values
           , y_data_list = [data[1],data[2]]
           , y_data_names = ['group1', 'group2']
           , colors = ['blue', 'orange']
           , x_label = 'Scale'
           , y_label = 'Score'
           , title = 'title')

      ---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-111-b910d304a19e> in <module>()
    5                , x_label = 'Verloning scale'
    6                , y_label = 'Score'
----> 7                , title = 'Score op elke item voor werknemer en oud-werknemers')

<ipython-input-66-9fa4a515d5e9> in groupedbarplot(x_data, y_data_list, y_data_names, colors, x_label, y_label, title)
      11         # Move the bar to the right on the x-axis so it doesn't
      12         # overlap with previously drawn ones
 ---> 13         ax.bar(x_data + alteration[i], y_data_list[i], color = colors[i], label = y_data_names[i], width = ind_width)
      14     ax.set_ylabel(y_label)
      15     ax.set_xlabel(x_label)

TypeError: Can't convert 'float' object to str implicitly

我检查了所有变量以查看差异在哪里,但似乎找不到。有什么想法吗?

【问题讨论】:

标签: python pandas matplotlib transpose


【解决方案1】:

当我转置您提供的数据框时,结果如下所示

          1         2
item1  2.807692  2.909091
item2  3.115385  2.454545
item3  3.923077  3.909091

因此data.index.values 返回array(['item1', 'item2', 'item3'], dtype=object),我怀疑你的错误是由x_data + alteration[i] 导致的,它试图向包含字符串的数组添加一个浮点数。

【讨论】:

    猜你喜欢
    • 2015-09-10
    • 2019-08-14
    • 2016-06-04
    • 2019-11-05
    • 2014-04-11
    • 2017-06-28
    • 1970-01-01
    • 1970-01-01
    • 2017-11-14
    相关资源
    最近更新 更多