Tre*_*eha 4 python ggplot2 python-ggplot
各位,
我正在尝试在 python 中使用 ggplot。
from ggplot import *
ggplot(diamonds, aes(x='price', fill='cut')) + geom_density(alpha=0.25) + facet_wrap("clarity")
Run Code Online (Sandbox Code Playgroud)
我正在尝试做的几件事:
1)我希望颜色既填充又线条,但正如你所看到的颜色都是灰色的
2)我正在尝试调整绘图的大小。在 RI 中,会在情节之前运行:
options(repr.plot.width=12, repr.plot.height=4)
Run Code Online (Sandbox Code Playgroud)
但是,这在这里不起作用。
有谁知道我如何在分布中着色并改变绘图大小?
谢谢你。附上电流输出。
为 Python 使用最新的 ggplot2:plotnine。
为了重塑绘图大小,请使用此主题参数:figure_size(width, height)。宽度和高度以英寸为单位。
请参阅此库文档:https : //plotnine.readthedocs.io/en/stable/generated/plotnine.themes.themeable.figure_size.html#plotnine.themes.themeable.figure_size
请参阅以下示例:
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
)
Run Code Online (Sandbox Code Playgroud)
颜色
使用color而不是填充。例如;
from ggplot import *
ggplot(diamonds, aes(x='price', color='cut')) + geom_density(alpha=0.25) + facet_wrap("clarity")
Run Code Online (Sandbox Code Playgroud)
尺寸
有几种方法可以做到这一点。
最简单的方法是ggsave- 查找文档。
或者,theme与plot_margin参数一起使用:
ggplot(...) ... + theme(plot_margin = dict(right = 12, top=8))
Run Code Online (Sandbox Code Playgroud)
或者,使用 matplotlib 设置:
import matplotlib as mpl
mpl.rcParams["figure.figsize"] = "11, 8"
ggplot(...) + ...
Run Code Online (Sandbox Code Playgroud)
希望有帮助!
| 归档时间: |
|
| 查看次数: |
6531 次 |
| 最近记录: |