Python 中 ggplot 的 geom_bar() 将所有条形图显示为具有统一高度
geom_bar() of ggplot in Python displaying all bars as having heights of unity
我希望使用 Python 中的 ggplot
在 2020 年针对 disturbance_code
绘制以下数据框 median_area
。
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()
简单替换为 geom_line
会得到:
任何关于为什么会这样的帮助将不胜感激。
相同的问题here。简而言之,答案是将 y = 'median_area'
替换为 weight = 'median_area'
.
我希望使用 Python 中的 ggplot
在 2020 年针对 disturbance_code
绘制以下数据框 median_area
。
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()
简单替换为 geom_line
会得到:
任何关于为什么会这样的帮助将不胜感激。
相同的问题here。简而言之,答案是将 y = 'median_area'
替换为 weight = 'median_area'
.