猿问

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

我正在寻找使用Pythonmedian_area绘制disturbance_code2020 年以下数据框的图。ggplot


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)')

这给出了以下情节:

然而,非常奇怪的是,简单替换geom_bar()withgeom_line会得到:

https://img.mukewang.com/64db42a80001551807440489.jpg


交互式爱情
浏览 118回答 1
1回答

皈依舞

简而言之,答案是替换y = 'median_area'为weight = 'median_area'。
随时随地看视频慕课网APP

相关分类

Python
我要回答