我对订购条形图有疑问。例如:

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/

10-10 20:04