有一种方法可以将散点图覆盖分组的盒子图上的散点图,因此它们不会使用Plotly Graph_Objects来抵消它们?

问题描述 投票:0回答:1

    

可以通过退出框模式并使每个标签与众不同,从而获得所需的输出。这是因为盒子和旋风和散点图的X轴将相同。 Boxplot with incorrectly aligned scatterplot pointsimport plotly.graph_objects as go def create_multiple_boxplots(summary_stats_list, labels, types, title="Multiple Boxplots"): fig = go.Figure() color_map = {"Rainy": "blue", "Sunny": "green"} i=0 for stats, label, type_ in zip(summary_stats_list, labels, types): fig.add_trace(go.Box( name=type_, q1=[stats['Q1']], median=[stats['Median']], q3=[stats['Q3']], lowerfence=[stats['Min']], upperfence=[stats['Max']], mean=[stats['Mean']], boxpoints='all' if 'Outliers' in stats else False, jitter=0.3, pointpos=-1.8, marker=dict(color=color_map[type_]), # Assign color based on type legendgroup=type_, showlegend=True if i < 2 else False, x=[label], # y=stats.get('Outliers', []) )) # Add outlier points separately fig.add_trace(go.Scatter( x=[label] * len(stats['Outliers']), y=stats['Outliers'], mode='markers', marker=dict(color=color_map[type_], size=8, symbol='circle-open'), name=f"Outliers - {type_}", legendgroup=type_, showlegend=False )) i+=1 fig.update_layout(title=title, yaxis_title="Value", boxmode='group') fig.show() # Example summary statistics data_summaries = [ {"Min": 5, "Q1": 10, "Median": 15, "Q3": 20, "Max": 25, "Mean": 16, "Outliers": [2, 27]}, {"Min": 6, "Q1": 11, "Median": 16, "Q3": 21, "Max": 26, "Mean": 17, "Outliers": [3, 28]}, {"Min": 4, "Q1": 9, "Median": 14, "Q3": 19, "Max": 24, "Mean": 15, "Outliers": [1, 26]}, {"Min": 7, "Q1": 12, "Median": 17, "Q3": 22, "Max": 27, "Mean": 18, "Outliers": [4, 29]} ] labels = ["Happy", "Happy", "Sad", "Sad"] types = ["Rainy", "Sunny", "Rainy", "Sunny"] create_multiple_boxplots(data_summaries, labels, types)

python matplotlib plotly seaborn plotly.graph-objects
1个回答
0
投票

要在仍处于盒子模式的情况下进行散点图,请添加一个偏移组,该组将绘制盒子和旋风图中心的散点图。
import plotly.graph_objects as go def create_multiple_boxplots(summary_stats_list, labels, types, title="Multiple Boxplots"): fig = go.Figure() color_map = {"Rainy": "blue", "Sunny": "green"} i=0 for stats, label, type_ in zip(summary_stats_list, labels, types): fig.add_trace(go.Box( name=type_, q1=[stats['Q1']], median=[stats['Median']], q3=[stats['Q3']], lowerfence=[stats['Min']], upperfence=[stats['Max']], mean=[stats['Mean']], boxpoints='all' if 'Outliers' in stats else False, jitter=0.3, pointpos=-1.8, marker=dict(color=color_map[type_]), # Assign color based on type legendgroup=type_, showlegend=True if i < 2 else False, x=[label], )) fig.add_trace(go.Scatter( x=[label] * len(stats['Outliers']), y=stats['Outliers'], mode='markers', marker=dict(color=color_map[type_], size=8, symbol='circle-open'), name=f"Outliers - {type_}", legendgroup=type_, showlegend=False )) i+=1 fig.update_layout(title=title, yaxis_title="Value")#, boxmode='group') # update fig.show() # Example summary statistics data_summaries = [ {"Min": 5, "Q1": 10, "Median": 15, "Q3": 20, "Max": 25, "Mean": 16, "Outliers": [2, 27]}, {"Min": 6, "Q1": 11, "Median": 16, "Q3": 21, "Max": 26, "Mean": 17, "Outliers": [3, 28]}, {"Min": 4, "Q1": 9, "Median": 14, "Q3": 19, "Max": 24, "Mean": 15, "Outliers": [1, 26]}, {"Min": 7, "Q1": 12, "Median": 17, "Q3": 22, "Max": 27, "Mean": 18, "Outliers": [4, 29]} ] labels = ["Happy", "Happy_", "Sad", "Sad_"] # update types = ["Rainy", "Sunny", "Rainy", "Sunny"] create_multiple_boxplots(data_summaries, labels, types)

enter image description here


最新问题
© www.soinside.com 2019 - 2025. All rights reserved.