gpt4 book ai didi

Plotnine 条形图按变量顺序

转载 作者:行者123 更新时间:2023-12-02 19:19:11 25 4
gpt4 key购买 nike

我有一个关于订购条形图的问题。例如:

http://pythonplot.com/#bar-counts

(ggplot(mpg) +
aes(x='manufacturer') +
geom_bar(size=20) +
coord_flip() +
ggtitle('Number of Cars by Make')
)

如何按“mpg”排序?

最佳答案

感谢has2k1:https://github.com/has2k1/plotnine/issues/94

如果 x 映射是有序分类,则受到尊重。

from plydata import *
from plotnine import *
from plotnine.data import mpg

# count the manufacturer and sort by the count (see, plydata documentation
# or find out how to do the same thing using raw pandas)
m_categories = (
mpg
>> count('manufacturer', sort=True)
>> pull('manufacturer')
)

df = mpg.copy()
df['manufacturer'] = pd.Categorical(df['manufacturer'], categories=m_categories, ordered=True)

(ggplot(df) +
aes(x='manufacturer') +
geom_bar(size=20) +
coord_flip() +
ggtitle('Number of Cars by Make')
)

关于Plotnine 条形图按变量顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47662234/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com