首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Plotnine图按变量排序

Plotnine图按变量排序
EN

Stack Overflow用户
提问于 2017-12-05 20:35:05
回答 2查看 3.8K关注 0票数 3

我有个问题要点酒吧的情节。例如:

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

代码语言:javascript
复制
(ggplot(mpg) +
aes(x='manufacturer') +
geom_bar(size=20) +
coord_flip() +
ggtitle('Number of Cars by Make')
)

如何按"mpg“订购?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-12-05 20:35:05

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

如果x映射是有序的范畴,则它是受尊重的。

代码语言:javascript
复制
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')
)
票数 2
EN

Stack Overflow用户

发布于 2020-08-25 12:09:35

STHDA,我发现:

更改图例中项的顺序函数scale_x_discrete可用于将项的顺序更改为“2”、“0.5”、“1”:

代码语言:javascript
复制
p + scale_x_discrete(limits=c("D2", "D0.5", "D1"))

我的目标是维护df的顺序,所以我做到了:

代码语言:javascript
复制
scale_x_discrete(limits=df[xColumn].tolist())

然后,我意识到,第一个酒吧项目是在最后,所以我切换到:

代码语言:javascript
复制
scale_x_discrete(limits=df[xColumn].tolist()[::-1])

我不能使用reverse(),因为它在适当的地方工作,并且没有返回列表,所以limits似乎没有看到效果。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47662234

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档