python中的ggplot:打印大小和颜色

python中的ggplot:打印大小和颜色,python,ggplot2,python-ggplot,Python,Ggplot2,Python Ggplot,各位 我正在尝试在python中使用ggplot from ggplot import * ggplot(diamonds, aes(x='price', fill='cut')) + geom_density(alpha=0.25) + facet_wrap("clarity") 我想做的几件事: 1) 我希望颜色是填充和线条,但正如你所看到的,颜色都是灰色的 2) 我正试图调整情节的大小。在R中,我将在绘图之前运行此命令: options(repr.plot.width=12, repr.

各位

我正在尝试在python中使用ggplot

from ggplot import *
ggplot(diamonds, aes(x='price', fill='cut')) + geom_density(alpha=0.25) + facet_wrap("clarity")
我想做的几件事:

1) 我希望颜色是填充和线条,但正如你所看到的,颜色都是灰色的

2) 我正试图调整情节的大小。在R中,我将在绘图之前运行此命令:

options(repr.plot.width=12, repr.plot.height=4)
然而,这在这里不起作用

有人知道我如何在分布中着色以及如何更改绘图大小吗

多谢各位。连接电流输出

颜色

使用
颜色
代替填充。 e、 g

大小

有几种方法可以做到这一点

最简单的方法是使用
ggsave
——查阅文档

或者,将
theme
plot\u margin
参数一起使用:

ggplot(...) ... + theme(plot_margin = dict(right = 12, top=8))
或者,使用matplotlib设置:

import matplotlib as mpl
mpl.rcParams["figure.figsize"] = "11, 8"
ggplot(...) + ...

希望有帮助

另一种解决方案,如果不想更改matplotlib的配置:

从ggplot导入*
从matplotlib导入pyplot作为plt
p=(ggplot)(菱形,
aes(x='x',y='y',color='cut',fill='cut'))+
几何点()+
面_包裹(x='cut'))
#此命令“渲染”图形,并在p对象上创建属性“fig”
p、 make()
#然后你可以改变它的属性
p、 图设置尺寸英寸(15,5,向前=真)
p、 图集_dpi(100)
p、 无花果
#并显示最终图形
plt.show()

这是谷歌在搜索如何增加
plotnine
plot的大小时给出的最佳答案。没有一个答案对我有效,但增加了主题:

from plotnine import *
from plotnine.data import diamonds
ggplot(diamonds, aes(x='price', color='cut')) + geom_density(alpha=0.25) + facet_wrap("clarity") + theme(figure_size = (10, 10))

为Python使用最新的ggplot2:plotnine

要重塑打印大小,请使用此主题参数:地物大小(宽度、高度)。宽度和高度以英寸为单位

请参阅此库文档:

请参见以下示例:

from plotnine import *

(ggplot(df) 
 + aes(x='column_X', y='column_Y', color = 'column_for_collor')    
 + geom_line()
 + theme(axis_text_x = element_text(angle = 45, hjust = 1))
 + facet_wrap('~column_to_facet', ncol = 3)   # ncol to define 3 facets per line
 + theme(figure_size=(16, 8))  # here you define the plot size
)

颜色当然有帮助,尺寸需要稍加修改,但这解决了我的问题。我不配!我不配!我不配
mpl.rcParams[“figure.figsize”]=“11,8”
对我不起作用。我在jupyer笔记本电脑中工作。@MartaKaras我喜欢下面Rodolfo的解决方案,使用plotnine/ggplot将人物大小添加到主题中。这在Jupyter笔记本中对我有用。我假设您已经为jupyter笔记本声明了以下内容:%matplotlib inline
from plotnine import *

(ggplot(df) 
 + aes(x='column_X', y='column_Y', color = 'column_for_collor')    
 + geom_line()
 + theme(axis_text_x = element_text(angle = 45, hjust = 1))
 + facet_wrap('~column_to_facet', ncol = 3)   # ncol to define 3 facets per line
 + theme(figure_size=(16, 8))  # here you define the plot size
)