第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

繪制分位數(shù)線并連接兩個(gè)小提琴圖

繪制分位數(shù)線并連接兩個(gè)小提琴圖

撒科打諢 2023-06-06 16:31:46
如何在 Python 中繪制分位數(shù)線并連接兩個(gè)小提琴圖?例如,R ( https://github.com/GRousselet/rogme ) 中有一個(gè)庫(kù)可以執(zhí)行此操作。當(dāng)有兩個(gè)以上的組時(shí),提供的庫(kù)不一定有效。
查看完整描述

1 回答

?
LEATH

TA貢獻(xiàn)1936條經(jīng)驗(yàn) 獲得超7個(gè)贊

在 Plotly 中絕對(duì)沒有內(nèi)置方法來做這個(gè)特定的事情。你能做的最好的可能是畫一些線,如果你需要為不同分位數(shù)的多組數(shù)據(jù)做這件事,可以考慮寫一個(gè)函數(shù)或一些循環(huán)。

這是我將如何開始。如果要連接分組小提琴圖中的相同分位數(shù),則可以創(chuàng)建一個(gè)列表或數(shù)組來存儲(chǔ)線條的所有坐標(biāo)。我承認(rèn)我現(xiàn)在所擁有的是 hacky,因?yàn)樗蕾囉?Plotly 中的組,其 y 坐標(biāo)從 0 開始并增加 1??赡苡幸环N方法可以訪問分組小提琴圖的 y 坐標(biāo),我建議查看文檔。

如果您想添加文本框來指示分位數(shù)的值,則需要做更多的工作。

import numpy as np

import pandas as pd


import plotly.express as px

import plotly.graph_objects as go


# generate some random data that is normally distributed

np.random.seed(42)

y1 = np.random.normal(0, 1, 1000) * 1.5 + 6

y2 = np.random.normal(0, 5, 1000) + 6


# group the data together and combine into one dataframe

df1 = pd.DataFrame({'Group': 'Group1', 'Values': y1})

df2 = pd.DataFrame({'Group': 'Group2', 'Values': y2})

df_final = pd.concat([df1, df2])


fig = px.strip(df_final, x='Values', y='Group', color_discrete_sequence=['grey'])


quantiles_list = [0.05, 0.10, 0.25, 0.50, 0.75, 0.90, 0.95]


## this is a bit hacky and relies on y coordinates for groups starting from 0 and increasing by 1

y_diff = 0

## these store the coordinates in order to connect the quantile lines

lower_coordinates, upper_coordinates = [], []

for group_name in df_final.Group.unique():

? ? for quantile in quantiles_list:

? ? ? ? quantile_value = np.quantile(df_final[df_final['Group'] == group_name].Values, quantile)

? ? ? ? if group_name == 'Group1':

? ? ? ? ? ? lower_coordinates.append((quantile_value, 0.2+1*y_diff))

? ? ? ? if group_name == 'Group2':

? ? ? ? ? ? upper_coordinates.append((quantile_value, -0.2+1*y_diff))

? ? ? ? fig.add_shape(

? ? ? ? ? ? ? ? # Vertical Line for Group1

? ? ? ? ? ? ? ? dict(

? ? ? ? ? ? ? ? ? ? type="line",

? ? ? ? ? ? ? ? ? ? x0=quantile_value,

? ? ? ? ? ? ? ? ? ? y0=-0.2+1*y_diff,

? ? ? ? ? ? ? ? ? ? x1=quantile_value,

? ? ? ? ? ? ? ? ? ? y1=0.2+1*y_diff,

? ? ? ? ? ? ? ? ? ? line=dict(

? ? ? ? ? ? ? ? ? ? ? ? color="black",

? ? ? ? ? ? ? ? ? ? ? ? width=4

? ? ? ? ? ? ? ? ? ? )

? ? ? ? ? ? ? ? ),

? ? ? ? )

? ? y_diff += 1


## draw connecting lines

for idx in range(len(upper_coordinates)):

? ? fig.add_shape(

? ? ? ? ? ? dict(

? ? ? ? ? ? ? ? type="line",

? ? ? ? ? ? ? ? x0=lower_coordinates[idx][0],

? ? ? ? ? ? ? ? y0=lower_coordinates[idx][1],

? ? ? ? ? ? ? ? x1=upper_coordinates[idx][0],

? ? ? ? ? ? ? ? y1=upper_coordinates[idx][1],

? ? ? ? ? ? ? ? line=dict(

? ? ? ? ? ? ? ? ? ? color="chocolate",

? ? ? ? ? ? ? ? ? ? width=4

? ? ? ? ? ? ? ? ? ? )

? ? ? ? ? ? ? ? ),

? ? )

fig.show()

http://img1.sycdn.imooc.com/647eef2a0001eb8725601166.jpg

查看完整回答
反對(duì) 回復(fù) 2023-06-06
  • 1 回答
  • 0 關(guān)注
  • 174 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)