我有个问题要点酒吧的情节。例如:
http://pythonplot.com/#bar-counts
(ggplot(mpg) +
aes(x='manufacturer') +
geom_bar(size=20) +
coord_flip() +
ggtitle('Number of Cars by Make')
)如何按"mpg“订购?
发布于 2017-12-05 20:35:05
感谢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')
)发布于 2020-08-25 12:09:35
在STHDA,我发现:
更改图例中项的顺序函数scale_x_discrete可用于将项的顺序更改为“2”、“0.5”、“1”:
p + scale_x_discrete(limits=c("D2", "D0.5", "D1"))我的目标是维护df的顺序,所以我做到了:
scale_x_discrete(limits=df[xColumn].tolist())然后,我意识到,第一个酒吧项目是在最后,所以我切换到:
scale_x_discrete(limits=df[xColumn].tolist()[::-1])我不能使用reverse(),因为它在适当的地方工作,并且没有返回列表,所以limits似乎没有看到效果。
https://stackoverflow.com/questions/47662234
复制相似问题