简体   繁体   English

阴谋? python - 如何在plotly Python中同时更改胡须长度和颜色单个箱线图?

[英]Plotly? How to change the whisker length and color individual boxplots simultaneously in plotly Python?

Here is the code from the webpage: https://plotly.com/python/box-plots/这是来自网页的代码: https ://plotly.com/python/box-plots/

I want to be able to change the color of every individual boxplot while keeping control of quartiles and fences:我希望能够更改每个单独的箱线图的颜色,同时保持对四分位数和栅栏的控制:

import plotly.graph_objects as go

fig = go.Figure()

fig.add_trace(go.Box(y=[
        [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ],
        [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ],
        [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ]
      ], name="Precompiled Quartiles"))

fig.update_traces(q1=[ 1, 2, 3 ], median=[ 4, 5, 6 ],
                  q3=[ 7, 8, 9 ], lowerfence=[-1, 0, 1],
                  upperfence=[5, 6, 7], mean=[ 2.2, 2.8, 3.2 ],
                  sd=[ 0.2, 0.4, 0.6 ], notchspan=[ 0.2, 0.4, 0.6 ] )

fig.show()

To change the colors and the values for q1, q3, etc., you can use the below updated code.要更改 q1、q3 等的颜色和值,您可以使用以下更新的代码。 Change the values in the myq1 , myq3 and other array to change the values.更改myq1myq3和其他数组中的值以更改值。 The colors can be changed using the marker_color .可以使用marker_color更改颜色。 The underlying data points, however are not visible.然而,底层数据点是不可见的。 Hope this helps.希望这可以帮助。

from plotly.subplots import make_subplots
import plotly.graph_objects as go

#fig = go.Figure()
myq1=[ 2, 3, 4 ]
myq3=[ 8, 9, 10 ]
mymedian=[ 4, 5, 6 ]
mymean=[ 2.2, 2.8, 3.2 ]
mylowerfence=[ 0, 1, 2 ]
mynotchspan=[ 0.2, 0.4, 0.6 ]
mysd=[ 0.2, 0.4, 0.6 ]
mylowerfence=[ 0, 1, 2 ]
myupperfence=[9, 10, 11]


trace0 = go.Box( y=[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ], name="Quartile1", marker_color='#3D9970', q1=[myq1[0]], 
                median=[mymedian[0]], q3 = [myq3[0]], lowerfence=[mylowerfence[0]], upperfence=[myupperfence[0]], 
                mean=[mymean[0]],sd=[mysd[0]], notchspan=[mynotchspan[0]])

trace1 = go.Box( y=[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ], name="Quartile2", marker_color='#FF4136', q1=[myq1[1]], 
                median=[mymedian[1]], q3 = [myq3[1]], lowerfence=[mylowerfence[1]], upperfence=[myupperfence[1]], 
                mean=[mymean[1]],sd=[mysd[1]], notchspan=[mynotchspan[1]])

trace2 = go.Box( y=[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ], name="Quartile3", marker_color='#FF851B', q1=[myq1[2]], 
                median=[mymedian[2]], q3 = [myq3[2]], lowerfence=[mylowerfence[2]],upperfence=[myupperfence[2]], 
                mean=[mymean[2]],sd=[mysd[2]], notchspan=[mynotchspan[2]])

fig=go.Figure(data=[trace0, trace1, trace2])

fig.update_traces(orientation='v')
fig.update_layout(boxmode='group')                  
fig.show()

Output输出

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM