Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 绘制分位数线并连接两个小提琴图_Python_Plotly - Fatal编程技术网

Python 绘制分位数线并连接两个小提琴图

Python 绘制分位数线并连接两个小提琴图,python,plotly,Python,Plotly,如何在Python中绘制分位数线并以plotly连接两个小提琴图 例如,在R()中有一个库可以实现这一点。当有两个以上的组时,提供的库不一定有效 在Plotly中,绝对没有内置的方法来做这种特定的事情。你可以做的最好的是画一些线,并考虑写一个函数或一些循环,如果你需要这样做为多组数据为不同的分位数。 以下是我将如何开始。如果要连接分组图中的相同分位数,可以创建一个列表或数组来存储线的所有坐标。我承认我目前所拥有的是一个很难理解的东西,因为它依赖于组,它们的y坐标从0开始,然后增加1。可能有一种方

如何在Python中绘制分位数线并以plotly连接两个小提琴图

例如,在R()中有一个库可以实现这一点。当有两个以上的组时,提供的库不一定有效


在Plotly中,绝对没有内置的方法来做这种特定的事情。你可以做的最好的是画一些线,并考虑写一个函数或一些循环,如果你需要这样做为多组数据为不同的分位数。 以下是我将如何开始。如果要连接分组图中的相同分位数,可以创建一个列表或数组来存储线的所有坐标。我承认我目前所拥有的是一个很难理解的东西,因为它依赖于组,它们的y坐标从0开始,然后增加1。可能有一种方法可以访问成组小提琴图的y坐标,我建议您深入研究

如果要添加文本框来指示分位数的值,则需要做更多的工作

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()