脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - python绘制云雨图raincloud plot

python绘制云雨图raincloud plot

2022-08-04 17:20LIAN_U Python

这篇文章主要介绍了python绘制云雨图raincloud plot,Raincloud的Python实现是一个名为PtitPrince的包,它写在seaborn之上,这是一个Python绘图库,用于从pandas数据帧中获取漂亮的绘图

官方github: https://github.com/RainCloudPlots/RainCloudPlots

Raincloud 的 Python 实现是一个名为 PtitPrince 的包,它写在 seaborn 之上,这是一个 Python 绘图库,用于从 pandas 数据帧中获取漂亮的绘图。

import pandas as pd
import seaborn as sns
import os
import matplotlib.pyplot as plt
#sns.set(style="darkgrid")
#sns.set(style="whitegrid")
#sns.set_style("white")
sns.set(style="whitegrid",font_scale=2)
import matplotlib.collections as clt
import ptitprince as pt
#图片保存及输出设置
savefigs = True
figs_dir = "../figs/tutorial_python"
if savefigs:
    # Make the figures folder if it doesn"t yet exist
    #如果没有找到文件夹,先创建此文件夹
    if not os.path.isdir("../figs/tutorial_python"):
        os.makedirs("../figs/tutorial_python")

def export_fig(axis,text, fname):
    if savefigs:
        axis.text()
        axis.savefig(fname, bbox_inches="tight")     
df = pd.read_csv ("simdat.csv", sep= ",")
df.head()

python绘制云雨图raincloud plot

该图可以让读者初步了解数据集:哪个组的平均值更大,这种差异是否可能显着。 此图中仅显示每组分数的平均值和标准差。

f, ax = plt.subplots(figsize=(7, 7))
sns.barplot(x = "group", y = "score", data = df, capsize= .1)
plt.title("Figure P1
 Bar Plot")
if savefigs:
    plt.savefig(".figs	utorial_pythonfigureP01.png", bbox_inches="tight")

python绘制云雨图raincloud plot

为了了解我们的数据集的分布,我们可以绘制一个“云”,即直方图的平滑版本:

# plotting the clouds
f, ax = plt.subplots(figsize=(7, 5))
dy="group" 
dx="score"
ort="h"
pal = sns.color_palette(n_colors=1)
ax=pt.half_violinplot(x=dx, y=dy, data=df, palette=pal, bw=.2, cut=0., scale="area", width=.6, inner=None, orient=ort)
plt.title("Figure P2
 Basic Rainclouds")
if savefigs:
    plt.savefig(".figs	utorial_pythonfigureP02.png", bbox_inches="tight")

python绘制云雨图raincloud plot

为了更精确地了解分布并说明数据中的潜在异常值或其他模式,我们现在添加“雨”,即数据点的简单单维表示:

# adding the rain
f, ax=plt.subplots(figsize=(7, 5))
ax=pt.half_violinplot(x=dx, y=dy, data=df, palette=pal, bw=.2, cut=0., scale="area", width=.6, inner=None, orient=ort)
ax=sns.stripplot(x=dx, y=dy, data=df, palette=pal, edgecolor="white", size=3, jitter=0, zorder=0, orient=ort)
plt.title("Figure P3
 Raincloud Without Jitter")
if savefigs:
    plt.savefig(".figs	utorial_pythonfigureP03.png", bbox_inches="tight")

python绘制云雨图raincloud plot

# adding jitter to the rain
f, ax =plt.subplots(figsize=(7, 5))
ax=pt.half_violinplot(x=dx, y=dy, data=df, palette=pal, bw=.2, cut=0., scale="area", width=.6, inner=None, orient=ort)
ax=sns.stripplot(x=dx, y=dy, data=df, palette=pal, edgecolor="white", size=3, jitter=1, zorder=0, orient=ort)
plt.title("Figure P4
 Raincloud with Jittered Data")
if savefigs:
    plt.savefig(".figs	utorial_pythonfigureP04.png", bbox_inches="tight")

python绘制云雨图raincloud plot

这样可以很好地了解数据点的分布情况,但中位数和四分位数并不明显,很难一目了然地确定统计差异。 因此,我们添加了一个“空”箱线图来显示中位数、四分位数和异常值:

#adding the boxplot with quartiles
f, ax=plt.subplots(figsize=(7, 5))
ax=pt.half_violinplot(x=dx, y=dy, data=df, palette=pal, bw=.2, cut=0.,
                      scale="area", width=.6, inner=None, orient=ort)
ax=sns.stripplot(x=dx, y=dy, data=df, palette=pal, edgecolor="white",
                 size=3, jitter=1, zorder=0, orient=ort)
ax=sns.boxplot(x=dx, y=dy, data=df, color="black", width=.15, zorder=10,
               showcaps=True, boxprops={"facecolor":"none","zorder":10},
               showfliers=True, whiskerprops{"linewidth":2,"zorder":10},
               saturation=1, orient=ort)
plt.title("Figure P5
 Raincloud with Boxplot")
if savefigs:
    plt.savefig("../figs/tutorial_python/figureP05.png", bbox_inches="tight")

python绘制云雨图raincloud plot

现在我们可以设置一个调色板来表征两组:

#adding color
pal="Set2"
f, ax=plt.subplots(figsize=(7, 5))
ax=pt.half_violinplot(x=dx, y=dy, data=df, palette=pal, bw=.2, cut=0.,
                      scale="area", width=.6, inner=None, orient=ort)
ax=sns.stripplot(x=dx, y=dy, data=df, palette=pal, edgecolor="white",
                 size=3, jitter=1, zorder=0, orient=ort)
ax=sns.boxplot(x=dx, y=dy, data=df, color="black", width=.15, zorder=10,
              showcaps=True, boxprops={"facecolor":"none","zorder":10},
              showfliers=True, whiskerprops={"linewidth":2,"zorder":10},
              saturation=1, orient=ort)
plt.title("Figure P6
 Tweaking the Colour of Your Raincloud")

python绘制云雨图raincloud plot

我们可以使用函数 pt.Raincloud 来添加一些自动化:

#same thing with a single command: now x **must** be the categorical value
dx="group"; dy="score"; ort="h"; pal="Set2"; sigma=.2
f, ax=plt.subplots(figsize=(7, 5))
pt.RainCloud(x=dx, y=dy, data=df, palette=pal, bw=sigma,
             width_viol = .6, ax = ax, orient = ort)
plt.title("Figure P7
 Using the pt.Raincloud function")
if savefigs:
    plt.savefig("../figs/tutorial_python/figureP07.png", bbox_inches="tight")

python绘制云雨图raincloud plot

‘move’ 参数可用于移动箱线图下方的雨量,在某些情况下提供更好的原始数据可见性:

#moving the rain below the boxplot
dx="group"; dy="score"; ort="h"; pal="Set2"; sigma=.2
f,ax=plt.subplots(figsize=(7, 5))
ax=pt.RainCloud(x=dx, y=dy, data=df, palette=pal, bw=sigma,
                 width_viol=.6, ax=ax, orient=ort, move=.2)
plt.title("Figure P8
 Rainclouds with Shifted Rain")

python绘制云雨图raincloud plot

此外,raincloud 函数同样适用于列表或 np.array,如果您更喜欢使用它们而不是数据框输入:

# Usage with a list/np.array input
dx=list(df["group"]); dy=list(df["score"])
f, ax=plt.subplots(figsize=(7, 5))
ax=pt.RainCloud(x=dx, y=dy, palette=pal, bw=sigma,
                 width_viol=.6, ax=ax, orient=ort)
plt.title("Figure P9
 Rainclouds with List/Array Inputs")

python绘制云雨图raincloud plot

对于某些数据,您可能希望将雨云的方向翻转为“petit prince”图。 您可以使用 pt.RainCloud 函数中的 ‘orient’ 标志来执行此操作:

# Changing orientation
dx="group"; dy="score"; ort="v"; pal="Set2"; sigma=.2
f, ax=plt.subplots(figsize=(7, 5))
ax=pt.RainCloud(x=dx, y=dy, data=df, palette=pal, bw=sigma,
                 width_viol=.5, ax=ax, orient=ort)
plt.title("Figure P10
 Flipping your Rainclouds")

python绘制云雨图raincloud plot

还可以更改用于生成数据概率分布函数的平滑核。 为此,您调整 sigma 参数:

#changing cloud smoothness
dx="group"; dy="score"; ort="h"; pal="Set2"; sigma=.05
f, ax=plt.subplots(figsize=(7, 5))
ax=pt.RainCloud(x=dx, y=dy, data=df, palette=pal, bw=sigma,
                 width_viol=.6, ax=ax, orient=ort)
plt.title("Figure P11
 Customizing Raincloud Smoothness")

python绘制云雨图raincloud plot

最后,使用 pointplot 标志,您可以添加一条连接组平均值的线。 这对于更复杂的数据集很有用,例如重复测量或因子数据。 下面我们通过改变各个图的色调、不透明度或闪避元素来说明使用雨云绘制此类数据的几种不同方法:

#adding a red line connecting the groups" mean value (useful for longitudinal data)
dx="group"; dy="score"; ort="h"; pal="Set2"; sigma=.2
f, ax=plt.subplots(figsize=(7, 5))
ax=pt.RainCloud(x=dx, y=dy, data=df, palette=pal, bw=sigma,
                 width_viol=.6, ax=ax, orient=ort, pointplot=True)
plt.title("Figure P12
 Adding Lineplots to Emphasize Factorial Effects")

python绘制云雨图raincloud plot

另一个灵活的选择是使用 Facet Grids 来分隔不同的组或因子水平,

如下所示:

# Rainclouds with FacetGrid
g=sns.FacetGrid(df, col="gr2", height=6)
g=g.map_dataframe(pt.RainCloud, x="group", y="score", data=df, orient="h")
g.fig.subplots_adjust(top=0.75)
g.fig.suptitle("Figure P13
 Using FacetGrid for More Complex Designs",  fontsize=26)

python绘制云雨图raincloud plot

作为一种替代方法,可以使用色调输入将不同的子组直接绘制在彼此之上,从而促进它们的比较:

# Hue Input for Subgroups
dx="group"; dy="score"; dhue="gr2"; ort="h"; pal="Set2"; sigma=.2
f, ax=plt.subplots(figsize=(12, 5))
ax=pt.RainCloud(x=dx, y=dy, hue=dhue, data=df, palette=pal, bw=sigma,
                 width_viol=.7, ax=ax, orient=ort)
plt.title("Figure P14
 Rainclouds with Subgroups")

python绘制云雨图raincloud plot

为了提高该图的可读性,我们使用相关标志(0-1 alpha 强度)调整 alpha 级别:

# Setting alpha level
f, ax=plt.subplots(figsize=(12, 5))
ax=pt.RainCloud(x=dx, y=dy, hue=dhue, data=df, palette=pal, bw=sigma,
                 width_viol=.7, ax=ax, orient=ort , alpha=.65)
plt.title("Figure P15
 Adjusting Raincloud Alpha Level")

python绘制云雨图raincloud plot

我们可以将 dodge 标志设置为 true,而不是让两个箱线图相互混淆,从而增加交互性:

#The Doge Flag
f, ax=plt.subplots(figsize=(12, 5))
ax=pt.RainCloud(x=dx, y=dy, hue=dhue, data=df, palette=pal, bw=sigma,
                 width_viol=.7, ax=ax, orient=ort , alpha=.65, dodge=True)
plt.title("Figure P16
 The Boxplot Dodge Flag")

python绘制云雨图raincloud plot

最后,我们可能希望在我们的图表中添加一个传统的线图,以帮助检测因子主效应和交互作用。

例如,我们在每个箱线图中绘制了平均值:

#same, with dodging and line
f, ax=plt.subplots(figsize=(12, 5))
ax=pt.RainCloud(x=dx, y=dy, hue=dhue, data=df, palette=pal, bw=sigma, 
                width_viol=.7, ax=ax, orient=ort , alpha=.65, 
                dodge=True, pointplot=True)
plt.title("Figure P17
 Dodged Boxplots with Lineplots")

python绘制云雨图raincloud plot

这是相同的图,但现在使用“移动”参数再次将单个观测值移动到箱线图下方:

#moving the rain under the boxplot
f, ax=plt.subplots(figsize=(12, 5))
ax=pt.RainCloud(x=dx, y=dy, hue=dhue, data=df, palette=pal, bw=sigma, 
               width_viol=.7, ax=ax, orient=ort , alpha=.65, dodge=True, 
               pointplot=True, move=.2)
plt.title("Figure P18
 Shifting the Rain with the Move Parameter")

python绘制云雨图raincloud plot

作为我们的最后一个示例,我们将考虑具有两组和三个时间点的复杂重复测量设计。 目标是说明我们复杂的相互作用和主要影响,同时保持雨云图的透明性:

# Load in the repeated data
df_rep=pd.read_csv("repeated_measures_data.csv", sep=",")
df_rep.columns=["score",  "timepoint", "group"]
df_rep.head()

python绘制云雨图raincloud plot

# Plot the repeated measures data
dx="group"; dy="score"; dhue="timepoint"; ort="h"; pal="Set2"; sigma=.2
f, ax=plt.subplots(figsize=(12, 5))
ax=pt.RainCloud(x=dx, y=dy, hue=dhue, data=df_rep, palette=pal, bw=sigma, width_viol=.7,
               ax=ax, orient=ort , alpha=.65, dodge=True, pointplot=True, move=.2)
plt.title("Figure P19
 Repeated Measures Data - Example 1")

python绘制云雨图raincloud plot

# Now with the group as hue
dx="timepoint"; dy="score"; dhue="group"
f, ax=plt.subplots(figsize=(12, 5))
ax=pt.RainCloud(x=dx, y=dy, hue=dhue, data=df_rep, palette=pal, bw=sigma, width_viol=.7,
                ax=ax, orient=ort , alpha=.65, dodge=True, pointplot=True, move=.2)
plt.title("Figure P20
  Repeated Measures Data - Example 2")

python绘制云雨图raincloud plot

到此这篇关于python绘制云雨图raincloud plot的文章就介绍到这了,更多相关python绘制云雨图内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文地址:https://blog.csdn.net/weixin_43779943/article/details/126133122

延伸 · 阅读

精彩推荐
  • PythonPython3.9.0 a1安装pygame出错解决全过程(小结)

    Python3.9.0 a1安装pygame出错解决全过程(小结)

    这篇文章主要介绍了Python3.9.0 a1安装pygame出错解决全过程(小结),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要...

    唐都工作室5602021-09-01
  • Python浅谈python中真正关闭socket的方法

    浅谈python中真正关闭socket的方法

    今天小编就为大家分享一篇浅谈python中真正关闭socket的方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...

    To be a cool guy9882021-05-03
  • PythonPython协程知多少

    Python协程知多少

    从概念上来说,我们都知道多进程和多线程,而协程其实是在单线程中实现多并发。从句法上看,协程与生成器类似,都是定义体中包含yield关键字的函数...

    dongfanger12052021-12-10
  • Pythonpython3+PyQt5实现文档打印功能

    python3+PyQt5实现文档打印功能

    这篇文章主要为大家详细介绍了python3+PyQt5实现文档打印功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    basisworker12142021-02-05
  • PythonCentos下实现安装Python3.6和Python2共存

    Centos下实现安装Python3.6和Python2共存

    这篇文章主要介绍了Centos下实现安装Python3.6和Python2共存,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧...

    tech10244692021-03-27
  • Pythonpython实现AI聊天机器人详解流程

    python实现AI聊天机器人详解流程

    事情是这样的,最近认识的一位小姐姐有每天早晨看天气预报的习惯。在我看来,很多人起床第一件事情就是看微信消息,既然这样,我就勉为其难每天早...

    编程简单学4462022-03-02
  • PythonPython实现购物车程序

    Python实现购物车程序

    这篇文章主要为大家详细介绍了Python实现购物车程序,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    Hongory8682021-01-31
  • Pythonpython实现图片彩色转化为素描

    python实现图片彩色转化为素描

    这篇文章主要为大家详细介绍了python实现图片彩色转化为素描,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    zh_2504022021-05-15