情节?如何在Plotly Python中同时更改晶须长度和彩色单个盒子图?

发布于 2025-02-10 14:15:35 字数 725 浏览 0 评论 0原文

这是网页中的代码: https://plotly.com/python/box-plots/

我想能有能力在控制四分位数和围栏的同时更改每个单独的箱形的颜色:

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

Here is the code from the webpage:
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()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

倾城花音 2025-02-17 14:15:35

要更改Q1,Q3等的颜色和值,您可以使用以下更新的代码。更改myq1myq3和其他数组中的值以更改值。可以使用Marker_Color更改颜色。但是,基础数据点不可见。希望这会有所帮助。

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

”在此处输入图像说明”

To change the colors and the values for q1, q3, etc., you can use the below updated code. Change the values in the myq1, myq3 and other array to change the values. The colors can be changed using the 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

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文