【问题标题】:How can I create a Stacked Bar plot in Python where the y axis is NOT based on counts如何在 Python 中创建堆积条形图,其中 y 轴不基于计数
【发布时间】:2022-08-09 19:01:06
【问题描述】:

我有以下Pandas DataFrame(此处缩写):

df = pd.DataFrame([
(\"Distal Lung AT2\", 0.4269588779192778, 20),
(\"Lung Ciliated epithelial cells\", 0.28642167657082035, 20),
(\"Distal Lung AT2\",0.4488207834077291,15), 
(\"Lung Ciliated epithelial cells\", 0.27546336897259094, 15),
(\"Distal Lung AT2\", 0.45502553604960105, 10),
(\"Lung Ciliated epithelial cells\", 0.29080413886147555, 10),
(\"Distal Lung AT2\", 0.48481604554028446, 5),
(\"Lung Ciliated epithelial cells\", 0.3178232409599174, 5)],
 columns = [\"features\", \"importance\", \"num_features\"])

我想创建一个堆积条形图,其中 x 轴代表num_features(因此应将具有相同num_features 的行组合在一起),y 轴代表importance,条形图中的每个条形情节有由features着色的块

我为此尝试使用plotnine,如下所示:

plot = (
        ggplot(df, aes(x=\"num_features\", y=\"importance\", fill=\"features\"))
              + geom_bar(stat=\"identity\")
              + xlab(\"Number of Features\")
              + ylab(\"\")
        )

但是,当我尝试保存绘图以便查看它ggsave(plot, os.path.join(figure_path, \"stacked_feature_importances.png\")) 时,我得到:

Traceback (most recent call last):
  File \"/home/mdanb/plot_top_features_iteratively.py\", line 94, in <module>
    plot_stacked_bar_plots(backwards_elim_dirs)
  File \"/home/mdanb/plot_top_features_iteratively.py\", line 87, in plot_stacked_bar_plots
    ggsave(plot, os.path.join(figure_path, \"stacked_feature_importances.png\"))
  File \"/home/mdanb/.local/lib/python3.8/site-packages/plotnine/ggplot.py\", line 736, in ggsave
    return plot.save(*arg, **kwargs)
  File \"/home/mdanb/.local/lib/python3.8/site-packages/plotnine/ggplot.py\", line 724, in save
    fig, p = self.draw(return_ggplot=True)
  File \"/home/mdanb/.local/lib/python3.8/site-packages/plotnine/ggplot.py\", line 203, in draw
    self._build()
  File \"/home/mdanb/.local/lib/python3.8/site-packages/plotnine/ggplot.py\", line 311, in _build
    layers.compute_position(layout)
  File \"/home/mdanb/.local/lib/python3.8/site-packages/plotnine/layer.py\", line 79, in compute_position
    l.compute_position(layout)
  File \"/home/mdanb/.local/lib/python3.8/site-packages/plotnine/layer.py\", line 393, in compute_position
    data = self.position.compute_layer(data, params, layout)
  File \"/home/mdanb/.local/lib/python3.8/site-packages/plotnine/positions/position.py\", line 56, in compute_layer
    return groupby_apply(data, \'PANEL\', fn)
  File \"/home/mdanb/.local/lib/python3.8/site-packages/plotnine/utils.py\", line 638, in groupby_apply
    lst.append(func(d, *args, **kwargs))
  File \"/home/mdanb/.local/lib/python3.8/site-packages/plotnine/positions/position.py\", line 54, in fn
    return cls.compute_panel(pdata, scales, params)
  File \"/home/mdanb/.local/lib/python3.8/site-packages/plotnine/positions/position_stack.py\", line 85, in compute_panel
    trans = scales.y.trans
AttributeError: \'scale_y_discrete\' object has no attribute \'trans\'

基于this 帖子,我还研究了直接使用Pandas 而不使用plotnine。但是,它并没有完全解决我的问题,因为条形图是根据计数堆叠的,而我特别想根据列的值堆叠它 (importance)

  • 如果您不打算显示总列并将其拆分为不适用的部分,则堆叠图在您的情况下没有意义。例如,如果您想要重要性的总和,并且想要将它们相应地划分为特征标签。然后在这里应用堆积条形图。我认为您正在寻找的是带有相应标签的多个并排条形图

标签: python pandas stacked-bar-chart


【解决方案1】:

问题是您使用的是geom_bar,它不期望y 美学,它会根据您指定的x 美学自动为您计算计数。

如果您想手动指定y,您应该使用geom_col,它需要xy 美学。如果您包含fill 美学,默认行为将是堆叠列,您可以通过指定position='dodge' 进行更改。

使用您的示例:

import plotnine as p9

(p9.ggplot(df)
 + p9.aes(x='num_features', y='importance', fill='features')
 + p9.geom_col())

Output

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-27
    • 2018-12-15
    • 1970-01-01
    • 1970-01-01
    • 2020-10-10
    • 2020-11-02
    相关资源
    最近更新 更多