我正在寻找在python(matplotlib)中使用R(ggplot)创建的以下堆叠条形图的等效项:
虚拟数据(在R中)如下所示:
seasons <- c("Winter", "Winter", "Winter", "Spring", "Spring", "Spring", "Summer", "Summer", "Summer", "Fall", "Fall", "Fall")
feelings <- c("Cold", "Cold", "Cold", "Warm", "Warm", "Cold", "Warm", "Warm", "Warm", "Warm", "Cold", "Cold")
survey <- data.frame(seasons, feelings)
在R中,我可以使用以下一行创建我要查找的图表:
ggplot(survey, aes(x=seasons, fill=feelings)) + geom_bar(position = "fill")
看起来是这样的:
如何用python(最好使用matplotlib)以简单紧凑的方式创建此图表
我找到了一些(几乎)合适的解决方案,但它们都相当复杂,而且远离一条直线。或者这在python(matplotlib)中是不可能的
如果您不喜欢matplotlib,并且确实喜欢ggplot,那么您可以使用
plotnine
库,它是Python中ggplot的克隆。语法几乎相同:结果如下:
第1步。准备数据
第2步。绘制数据
相关问题 更多 >
编程相关推荐