Plotnine 绘图九条按变量的绘图顺序

Plotnine 绘图九条按变量的绘图顺序,plotnine,Plotnine,我有一个关于排列条形图的问题。例如: 如何按“mpg”订购?感谢has2k1: 如果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 th

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

如何按“mpg”订购?

感谢has2k1:

如果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')
)
当时我发现:

更改图例中项目的顺序功能比例x离散 可用于将项目顺序更改为“2”、“0.5”、“1”:

我的目标是维护国防军的秩序,所以我做到了:

scale_x_discrete(limits=df[xColumn].tolist())
然后我意识到第一个条形图项目在末尾,所以我切换到:

scale_x_discrete(limits=df[xColumn].tolist()[::-1])
我无法使用
reverse()

scale_x_discrete(limits=df[xColumn].tolist())
scale_x_discrete(limits=df[xColumn].tolist()[::-1])