最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

python繪制云雨圖raincloud?plot

 更新時間:2022年08月04日 14:42:33   作者:LIAN_U  
這篇文章主要介紹了python繪制云雨圖raincloud?plot,Raincloud的Python實現(xiàn)是一個名為PtitPrince的包,它寫在seaborn之上,這是一個Python繪圖庫,用于從pandas數(shù)據(jù)幀中獲取漂亮的繪圖

官方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
#圖片保存及輸出設(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ù)集:哪個組的平均值更大,這種差異是否可能顯著。 此圖中僅顯示每組分數(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)在我們可以設(shè)置一個調(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")

為了提高該圖的可讀性,我們使用相關(guān)標志(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 標志設(shè)置為 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)的線圖,以幫助檢測因子主效應(yī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")

作為我們的最后一個示例,我們將考慮具有兩組和三個時間點的復雜重復測量設(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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 梅爾倒譜系數(shù)(MFCC)實現(xiàn)

    梅爾倒譜系數(shù)(MFCC)實現(xiàn)

    這篇文章主要為大家詳細介紹了梅爾倒譜系數(shù)(MFCC)實現(xiàn),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-06-06
  • python之glob的用法詳解

    python之glob的用法詳解

    glob?是?Python?中用于文件模式匹配的一個模塊,本文主要介紹了python之glob的用法詳解,具有一定的參考價值,感興趣的可以來了解一下
    2023-12-12
  • Python 時間戳之獲取整點凌晨時間戳的操作方法

    Python 時間戳之獲取整點凌晨時間戳的操作方法

    這篇文章主要介紹了Python 時間戳之獲取整點凌晨時間戳的操作方法,本文給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友參考下吧
    2020-01-01
  • VsCode中超好用的8個python插件推薦

    VsCode中超好用的8個python插件推薦

    本人日常使用vscode進行開發(fā),并且比較喜歡折騰vscode,會到處找這一些好玩的插件,于是越攢越多,下面這篇文章主要給大家介紹了關(guān)于VsCode中超好用的8個python擴展插件的相關(guān)資料,需要的朋友可以參考下
    2022-12-12
  • python多線程編程中的join函數(shù)使用心得

    python多線程編程中的join函數(shù)使用心得

    這篇文章主要介紹了python多線程編程中的join函數(shù)使用心得,本文先是給出了join函數(shù)使用例子,并對join函數(shù)的使用作了總結(jié),需要的朋友可以參考下
    2014-09-09
  • Django 請求Request的具體使用方法

    Django 請求Request的具體使用方法

    這篇文章主要介紹了Django 請求Request的具體使用方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-11-11
  • python 調(diào)用有道api接口的方法

    python 調(diào)用有道api接口的方法

    今天小編就為大家分享一篇python 調(diào)用有道api接口的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-01-01
  • Python DES加密實現(xiàn)原理及實例解析

    Python DES加密實現(xiàn)原理及實例解析

    這篇文章主要介紹了Python DES加密實現(xiàn)原理及實例解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-07-07
  • python生成九宮格圖片

    python生成九宮格圖片

    這篇文章主要為大家詳細介紹了python生成九宮格圖片,利用Image類將一張圖片分割成9張,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-11-11
  • 使用pandas將numpy中的數(shù)組數(shù)據(jù)保存到csv文件的方法

    使用pandas將numpy中的數(shù)組數(shù)據(jù)保存到csv文件的方法

    今天小編就為大家分享一篇使用pandas將numpy中的數(shù)組數(shù)據(jù)保存到csv文件的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-06-06

最新評論

凤台县| 朝阳区| 达尔| 宁强县| 新蔡县| 武宁县| 雷州市| 镇巴县| 桑植县| 定南县| 法库县| 台湾省| 盐亭县| 文登市| 时尚| 林周县| 鄢陵县| 丽水市| 友谊县| 乐亭县| 曲靖市| 赤壁市| 金山区| 钦州市| 萍乡市| 青海省| 安新县| 方城县| 宁明县| 隆林| 旬阳县| 清流县| 射阳县| 米脂县| 出国| 鹰潭市| 平果县| 富宁县| 嘉义市| 高碑店市| 德令哈市|