Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plotly: Boxplot doesn't show horizontal boxes with both x and y are given a list/array

Totally new to Plotly. Found a problem while I was playing around with the plotly boxplot. I was trying to draw two horizontal boxes by specifying x and y with an array and a list, respectively. One lies at y=1, the other lies at y=2. The code is the below:

trace1=go.Box(x=np.random.randn(100),y=[1]*100,boxpoints='all')
trace2=go.Box(x=np.random.randn(100),y=[2]*100,boxpoints='all')
figure=go.Figure(data=[trace1,trace2])
figure.show()

But I got no boxes, only has data points. I tried the same code only with x and y switched, plotted two vertical boxes no problem. Not sure where the problem is, any help is appreciated. Thanks!

like image 686
Claire Avatar asked Feb 24 '26 16:02

Claire


1 Answers

I was a bit puzzled by this myself, and I can't tell you why it happens, but I can tell you how to fix it. Just add:

figure.update_traces(orientation='h')

And you'll get this plot:

enter image description here

Complete code:

import plotly.graph_objects as go
import numpy as np
trace1=go.Box(x=np.random.randn(100),y=[1]*100, boxpoints = 'all')
trace2=go.Box(x=np.random.randn(100),y=[2]*100,boxpoints= 'all')
figure=go.Figure(data=[trace1,trace2])
figure.update_traces(orientation='h')
figure.show()
like image 115
vestland Avatar answered Feb 26 '26 06:02

vestland



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!