• 在python中使用ggplot2


    python的ggplot2库:plotnine
    >

    一.安装方法:

    pip install plotnine
    
    • 1

    使用的编译器: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'))
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    如果你和我一样使用的是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)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    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)
    
    • 1
    • 2
    • 3
    • 4
    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)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    image-20221028210455149

    先绘制一个小提琴图:

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

    image-20221028211248392

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

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

    image-20221028211446051

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

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

    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)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    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)
    
    
    • 1
    • 2
    • 3
    • 4

    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))
     )
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    image-20221028213215123

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

  • 相关阅读:
    Vue学习笔记16:自定义事件内容定义
    Latex | 公式编辑
    基于springboot的智慧学习(在线学习考试)系统
    云计算:shell脚本
    KMP&拓展KMP 复习笔记
    SpringBoot项目--电脑商城【获取省市区列表】
    Java单例模式
    云服务器部署Neo4j
    Windows 安装 Python 环境&PyCharm
    rust字符串
  • 原文地址:https://blog.csdn.net/qq_54423921/article/details/127579488