python繪制云雨圖raincloud?plot
官方github: https://github.com/RainCloudPlots/RainCloudPlots
Raincloud 的 Python 實現(xiàn)是一個名為 PtitPrince 的包,它寫在 seaborn 之上,這是一個 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
#圖片保存及輸出設置 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ù)集:哪個組的平均值更大,這種差異是否可能顯著。 此圖中僅顯示每組分數(shù)的平均值和標準差。
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ù)集的分布,我們可以繪制一個“云”,即直方圖的平滑版本:
# 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ù)點的簡單單維表示:
# 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ù)點的分布情況,但中位數(shù)和四分位數(shù)并不明顯,很難一目了然地確定統(tǒng)計差異。 因此,我們添加了一個“空”箱線圖來顯示中位數(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)在我們可以設置一個調(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 來添加一些自動化:
#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ù)可用于移動箱線圖下方的雨量,在某些情況下提供更好的原始數(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")
對于某些數(shù)據(jù),您可能希望將雨云的方向翻轉(zhuǎn)為“petit prince”圖。 您可以使用 pt.RainCloud 函數(shù)中的 ‘orient’ 標志來執(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 標志,您可以添加一條連接組平均值的線。 這對于更復雜的數(shù)據(jù)集很有用,例如重復測量或因子數(shù)據(jù)。 下面我們通過改變各個圖的色調(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")
另一個靈活的選擇是使用 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)輸入將不同的子組直接繪制在彼此之上,從而促進它們的比較:
# 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")
為了提高該圖的可讀性,我們使用相關標志(0-1 alpha 強度)調(diào)整 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\n Adjusting Raincloud Alpha Level")
我們可以將 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\n The Boxplot Dodge Flag")
最后,我們可能希望在我們的圖表中添加一個傳統(tǒng)的線圖,以幫助檢測因子主效應和交互作用。
例如,我們在每個箱線圖中繪制了平均值:
#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)在使用“移動”參數(shù)再次將單個觀測值移動到箱線圖下方:
#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")
作為我們的最后一個示例,我們將考慮具有兩組和三個時間點的復雜重復測量設計。 目標是說明我們復雜的相互作用和主要影響,同時保持雨云圖的透明性:
# 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")
到此這篇關于python繪制云雨圖raincloud plot的文章就介紹到這了,更多相關python繪制云雨圖內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
python 禁止函數(shù)修改列表的實現(xiàn)方法
下面小編就為大家?guī)硪黄猵ython 禁止函數(shù)修改列表的實現(xiàn)方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-08-08

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

Python Flask請求擴展與中間件相關知識總結(jié)