python繪制云雨圖raincloud?plot
官方github: https://github.com/RainCloudPlots/RainCloudPlots
Raincloud 的 Python 實(shí)現(xiàn)是一個(gè)名為 PtitPrince 的包,它寫在 seaborn 之上,這是一個(gè) Python 繪圖庫,用于從 pandas 數(shù)據(jù)幀中獲取漂亮的繪圖。
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
#圖片保存及輸出設(shè)置 savefigs = True figs_dir = '../figs/tutorial_python' if savefigs: # Make the figures folder if it doesn't yet exist #如果沒有找到文件夾,先創(chuàng)建此文件夾 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()
該圖可以讓讀者初步了解數(shù)據(jù)集:哪個(gè)組的平均值更大,這種差異是否可能顯著。 此圖中僅顯示每組分?jǐn)?shù)的平均值和標(biāo)準(zhǔn)差。
f, ax = plt.subplots(figsize=(7, 7)) sns.barplot(x = "group", y = "score", data = df, capsize= .1) plt.title("Figure P1\n Bar Plot") if savefigs: plt.savefig('.\\figs\\tutorial_python\\figureP01.png', bbox_inches='tight')
為了了解我們的數(shù)據(jù)集的分布,我們可以繪制一個(gè)“云”,即直方圖的平滑版本:
# 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\n Basic Rainclouds") if savefigs: plt.savefig('.\\figs\\tutorial_python\\figureP02.png', bbox_inches='tight')
為了更精確地了解分布并說明數(shù)據(jù)中的潛在異常值或其他模式,我們現(xiàn)在添加“雨”,即數(shù)據(jù)點(diǎn)的簡(jiǎn)單單維表示:
# 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\n Raincloud Without Jitter") if savefigs: plt.savefig('.\\figs\\tutorial_python\\figureP03.png', bbox_inches='tight')
# 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\n Raincloud with Jittered Data") if savefigs: plt.savefig('.\\figs\\tutorial_python\\figureP04.png', bbox_inches='tight')
這樣可以很好地了解數(shù)據(jù)點(diǎn)的分布情況,但中位數(shù)和四分位數(shù)并不明顯,很難一目了然地確定統(tǒng)計(jì)差異。 因此,我們添加了一個(gè)“空”箱線圖來顯示中位數(shù)、四分位數(shù)和異常值:
#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\n Raincloud with Boxplot") if savefigs: plt.savefig('../figs/tutorial_python/figureP05.png', bbox_inches='tight')
現(xiàn)在我們可以設(shè)置一個(gè)調(diào)色板來表征兩組:
#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\n Tweaking the Colour of Your Raincloud")
我們可以使用函數(shù) pt.Raincloud 來添加一些自動(dòng)化:
#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\n Using the pt.Raincloud function") if savefigs: plt.savefig('../figs/tutorial_python/figureP07.png', bbox_inches='tight')
‘move’ 參數(shù)可用于移動(dòng)箱線圖下方的雨量,在某些情況下提供更好的原始數(shù)據(jù)可見性:
#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\n Rainclouds with Shifted Rain")
此外,raincloud 函數(shù)同樣適用于列表或 np.array,如果您更喜歡使用它們而不是數(shù)據(jù)框輸入:
# 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\n Rainclouds with List/Array Inputs")
對(duì)于某些數(shù)據(jù),您可能希望將雨云的方向翻轉(zhuǎn)為“petit prince”圖。 您可以使用 pt.RainCloud 函數(shù)中的 ‘orient’ 標(biāo)志來執(zhí)行此操作:
# 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\n Flipping your Rainclouds")
還可以更改用于生成數(shù)據(jù)概率分布函數(shù)的平滑核。 為此,您調(diào)整 sigma 參數(shù):
#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\n Customizing Raincloud Smoothness")
最后,使用 pointplot 標(biāo)志,您可以添加一條連接組平均值的線。 這對(duì)于更復(fù)雜的數(shù)據(jù)集很有用,例如重復(fù)測(cè)量或因子數(shù)據(jù)。 下面我們通過改變各個(gè)圖的色調(diào)、不透明度或閃避元素來說明使用雨云繪制此類數(shù)據(jù)的幾種不同方法:
#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\n Adding Lineplots to Emphasize Factorial Effects")
另一個(gè)靈活的選擇是使用 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\n Using FacetGrid for More Complex Designs", fontsize=26)
作為一種替代方法,可以使用色調(diào)輸入將不同的子組直接繪制在彼此之上,從而促進(jìn)它們的比較:
# 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\n Rainclouds with Subgroups")
為了提高該圖的可讀性,我們使用相關(guān)標(biāo)志(0-1 alpha 強(qiáng)度)調(diào)整 alpha 級(jí)別:
# 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\n Adjusting Raincloud Alpha Level")
我們可以將 dodge 標(biāo)志設(shè)置為 true,而不是讓兩個(gè)箱線圖相互混淆,從而增加交互性:
#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\n The Boxplot Dodge Flag")
最后,我們可能希望在我們的圖表中添加一個(gè)傳統(tǒng)的線圖,以幫助檢測(cè)因子主效應(yīng)和交互作用。
例如,我們?cè)诿總€(gè)箱線圖中繪制了平均值:
#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\n Dodged Boxplots with Lineplots")
這是相同的圖,但現(xiàn)在使用“移動(dòng)”參數(shù)再次將單個(gè)觀測(cè)值移動(dòng)到箱線圖下方:
#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\n Shifting the Rain with the Move Parameter")
作為我們的最后一個(gè)示例,我們將考慮具有兩組和三個(gè)時(shí)間點(diǎn)的復(fù)雜重復(fù)測(cè)量設(shè)計(jì)。 目標(biāo)是說明我們復(fù)雜的相互作用和主要影響,同時(shí)保持雨云圖的透明性:
# Load in the repeated data df_rep=pd.read_csv("repeated_measures_data.csv", sep=",") df_rep.columns=["score", "timepoint", "group"] df_rep.head()
# 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\n Repeated Measures Data - Example 1")
# 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\n Repeated Measures Data - Example 2")
到此這篇關(guān)于python繪制云雨圖raincloud plot的文章就介紹到這了,更多相關(guān)python繪制云雨圖內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python 禁止函數(shù)修改列表的實(shí)現(xiàn)方法
下面小編就為大家?guī)硪黄猵ython 禁止函數(shù)修改列表的實(shí)現(xiàn)方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-08-08Python定時(shí)任務(wù)APScheduler的實(shí)例實(shí)例詳解
APScheduler 支持三種調(diào)度任務(wù):固定時(shí)間間隔,固定時(shí)間點(diǎn)(日期),Linux 下的 Crontab 命令。這篇文章主要介紹了Python定時(shí)任務(wù)APScheduler的使用,需要的朋友可以參考下2019-07-07Python詳解文字轉(zhuǎn)語音的實(shí)現(xiàn)
在自然語言處理上,文字、音頻互轉(zhuǎn)是一個(gè)很關(guān)鍵的技術(shù)點(diǎn)。對(duì)于語音轉(zhuǎn)文字,個(gè)人實(shí)現(xiàn)較為困難,我們可以使用語音轉(zhuǎn)文字的軟件或借助各API(如科大訊飛等)進(jìn)行移植開發(fā)。不過文字轉(zhuǎn)語音就相對(duì)而言容易實(shí)現(xiàn)很多了2022-02-02十個(gè)Python自動(dòng)化常用操作,即拿即用
這篇文章主要介紹了十個(gè)Python自動(dòng)化常用操作,即拿即用,文中有非常詳細(xì)的代碼示例,對(duì)正在學(xué)習(xí)python的小伙伴們有很好的幫助喲,需要的朋友可以參考下2021-05-05

Pyqt實(shí)現(xiàn)簡(jiǎn)易計(jì)算器功能

Python中的time模塊與datetime模塊用法總結(jié)

Python Flask請(qǐng)求擴(kuò)展與中間件相關(guān)知識(shí)總結(jié)