在python中使用ggplot2

python的ggplot2库:plotnine
>

一.安装方法:

pip install plotnine

使用的编译器:pycharm

二.plotnine绘图

1.第一个图形

除了导包的操作不一致,其他类似

from plotnine import ggplot, geom_point, aes, stat_smooth, facet_wrap
from plotnine.data import mtcars

(ggplot(mtcars, aes('wt', 'mpg', color='factor(gear)'))
 + geom_point()
 + stat_smooth(method='lm')
 + facet_wrap('~gear'))

如果你和我一样使用的是pycharm这个图形可能不会显示,我们需要先对其赋值,然后print出来

from plotnine import ggplot, geom_point, aes, stat_smooth, facet_wrap
from plotnine.data import mtcars

a = (ggplot(mtcars, aes('wt', 'mpg', color='factor(gear)'))
     + geom_point()
     + stat_smooth(method='lm')
     + facet_wrap('~gear'))
print(a)

image-20221028205058855

2.小提琴图+散点图+箱线图

先来看一下语法:

geom_violin(mapping=None, data=None, stat='ydensity', position='dodge',
            na_rm=False, inherit_aes=True, show_legend=None, raster=False,
            trim=True, scale='area', width=None, style='full',
            draw_quantiles=None, **kwargs)
AestheticDefault value
x
y
alpha1
color'#333333'
fill'white'
group
linetype'solid'
size0.5
weight1

首先我们生成一组数据:

import pandas as pd
import numpy as np
import pandas.api.types as pdtypes

from plotnine import *
from plotnine.data import *

np.random.seed(123)  # 设置随机数种子
n = 20
mu = (1, 2.3)
sigma = (1, 1.6)

before = np.random.normal(loc=mu[0], scale=sigma[0], size=n)
after = np.random.normal(loc=mu[1], scale=sigma[1], size=n)

df = pd.DataFrame({
    'value': np.hstack([before, after]),
    'when': np.repeat(['before', 'after'], n),
    'id': np.hstack([range(n), range(n)])
})

df['when'] = df['when'].astype(pdtypes.CategoricalDtype(categories=['before', 'after']))
print(df)

image-20221028210455149

先绘制一个小提琴图:

a = ggplot(df, aes("when", "value")) + geom_violin(df, aes(colour="when"))
print(a)

image-20221028211248392

在小提琴图的基础上加上散点

a = ggplot(df, aes("when", "value")) + geom_violin(df, aes(colour="when")) + geom_point()
print(a)

image-20221028211446051

小提琴在垂直轴上是对称的,半把小提琴和整把小提琴具有相同的信息。我们把小提琴切成两半,第一半用左半交替,第二半用右半交替。

a = ggplot(df, aes("when", "value")) + geom_violin(df, aes(colour="when"),style="left-right") + geom_point()
print(a)

image-20221028212144779

将这些点连接起来,以便了解数据是如何移动的。

a=(ggplot(df, aes('when', 'value'))
 + geom_violin(df, style='left-right') # changed
 + geom_point()
 + geom_line(aes(group='id'))          # new
)
print(a)

image-20221028212348413

3.使用theme添加主题

a = ggplot(df, aes("when", "value")) + geom_violin(df, aes(colour="when"),style="left-right") + geom_point() + geom_line(
    aes(group="id")) + theme_classic()
print(a)

image-20221028212601426

三.最终的图形

#%%
shift = 0.1


def alt_sign(x):
    "Alternate +1/-1 if x is even/odd"
    return (-1) ** x


m1 = aes(x=stage('when', after_scale='x+shift*alt_sign(x)'))  # shift outward
m2 = aes(x=stage('when', after_scale='x-shift*alt_sign(x)'), group='id')  # shift inward
lsize = 0.65
fill_alpha = 0.7

(ggplot(df, aes('when', 'value', fill='when'))
 + geom_violin(m1, style='left-right', alpha=fill_alpha, size=lsize, show_legend=False)
 + geom_point(m2, color='none', alpha=fill_alpha, size=2, show_legend=False)
 + geom_line(m2, color='gray', size=lsize, alpha=0.6)
 + geom_boxplot(width=shift, alpha=fill_alpha, size=lsize, show_legend=False)
 + scale_fill_manual(values=['dodgerblue', 'darkorange'])
 + theme_classic()
 + theme(figure_size=(8, 6))
 )

image-20221028213215123

[参考链接]:API Reference — plotnine 0.10.1 documentation

  • 4
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值