Python中ggplot的geom_bar将所有条形显示为具有统一的高度

问题描述

我希望在2020年使用Python中的median_area来绘制下面的数据框disturbance_codeggplot的关系图。

scenario | year | disturbance_code | median_area
------------------------------------------------
Base     | 2020 | 1001             | 14264
Base     | 2020 | 1002             | 6637
Base     | 2020 | 1003             | 18190
Base     | 2021 | 1001             | 15000
Base     | 2021 | 1002             | 3615
Base     | 2021 | 1003             | 3132

我的代码非常简单:

import pandas as pd
import numpy as np
from ggplot import *

data # this is the dataframe above - it comes from elsewhere in my actual code

ggplot(aes(x = 'disturbance_code',y = 'median_area',fill = 'scenario'),data = 
    data_frame[data_frame['year'] == int(2020)]) +\
    theme_bw() +\
    geom_bar(stat = 'identity',position = 'dodge') +\
    labs(x = 'Severity',y = 'Area burned (ha)')

这给出了以下图解:

enter image description here

但是,非常奇怪的是,用geom_bar()替换geom_line可以得到:

enter image description here

对于为什么这样做的任何帮助,将不胜感激。

解决方法

相同的问题here。简而言之,答案是将y = 'median_area'替换为weight = 'median_area'