Hi there
I’ve been having some trouble switching the color bar on a subplot (of graphobject.scatter objects) from vertical to horizontal.
I’ve tried fig.update_coloraxes(colorbar_orientation = “h”) , fig.update_layout(coloraxis= dict(orientation = ‘h’)) and a few others but nothing seems to work.
I also came across this post https://github.com/plotly/plotly.js/issues/1244 and was wondering if what I’m trying to do is even possible in plotly
any help is much appreciated
AIMPED
October 9, 2022, 6:13pm
2
Hi @dashingpy ,
I am not sure if there is a diffenrence when updating sublots, but did you try this?
import plotly.graph_objects as go
data = go.Bar(y=[1,2,3], orientation='v')
fig = go.Figure(data=data)
fig.update_traces({'orientation':'h'})
Just tried it, unfortunately it didn’t work either
AIMPED
October 10, 2022, 6:05am
4
Strange, could you post your code (or an MRE) so that we can reproduce this behavior?
Hi thanks for your help, Here’s the code I’m using to create the subplots
fig = make_subplots(rows=rows, cols=cols,
vertical_spacing=0.05,
subplot_titles=attributes
)
if n:
selectedData = None
clickdata = None
fig.data = []
pastselectedData = pd.DataFrame()
if clickdata is not None:
x = clickdata['points'][0]['x']
dfclick = dff[dff['TWS'] == x]
if len(pastselectedData) == 0:
pastselectedData = dfclick
print('saved first time')
if len(pastselectedData) >0:
pastselectedData = pastselectedData.append(dfclick)
print('updated.')
row =1
col = 1
for attr in attributes:
if col > 3:
row += 1
col = 1
fig.add_trace(go.Scatter(x=dff['TWS'], y=dff[attr],mode='markers', name= attr,
marker=dict(color=dff[color], colorscale='Bluered',showscale=True),
marker_size = abs(dff[size]/2))#,customdata= [dff[attr]])
,row=row,col=col)
if len(pastselectedData) >0:
y = pastselectedData[attr].tolist()
x = pastselectedData['TWS']
fig.add_trace(go.Scatter(x=x, y=y,marker_size=10,mode='markers', marker_color="yellow",name=
f'{attr}',showlegend=False),
row=row, col=col)
# if clickdata is not None:
# # print(clickdata)
# y = dfclick[attr].to_list()
# x = x
# fig.add_trace(go.Scatter(x=[x], y=y,marker_size=10, marker_color="yellow",name= f'{attr}',showlegend=False),
# row=row, col=col)
if selectedData is not None:
xlist =[]
ylist =[]
for point in selectedData['points']:
xlist.append(point['x'])
ylist.append(point['y'])
dfclick = dff[dff['TWS'].isin(xlist)]
y = dfclick[attr].to_list()
x = xlist
fig.add_trace(go.Scatter(x=x, y=y,marker_size=10,mode='markers', marker_color="yellow",name=
f'{attr}',showlegend=False),
row=row, col=col)
col+=1
fig.layout.coloraxis.colorbar.orientation = 'h'
# print(fig.layout.coloraxis.colorbar.orientation)
fig.update_layout(height=2300,showlegend=False)
fig.update_traces({'orientation':'h'})
# fig.update_traces(colorbar_orientation='h')
# fig.update_layout(coloraxis= dict(orientation = 'h'))
# fig.update_coloraxes(colorbar_orientation = "h")
[details="Summary"]
This text will be hidden
[/details]
AIMPED
October 10, 2022, 2:59pm
6
Hi @dashingpy ,
Oh man, I just realized that I misunderstood your question. I thought you had a bar plot and wanted to change the orientation of the bars from vertical to horizontal, sorry for that.
Try the following, at least in my example it creates a horizontal colorbar:
import plotly.express as px
from plotly.subplots import make_subplots
df = px.data.iris()
fig = make_subplots(rows=1,cols=2, shared_yaxes=True)
bar1 = px.scatter(df, x="sepal_width", y="sepal_length", color='petal_length')
bar2 = px.scatter(df, x="sepal_length", y="sepal_width", color='petal_length')
fig.add_traces(bar1.data, rows=1, cols=1)
fig.add_traces(bar2.data, rows=1, cols=2)
fig.update_coloraxes(colorbar={'orientation':'h', 'thickness':20, 'y': -1.0})
1 Like
Thanks for following up. Unfortunately still no luck. I think it might be because I’m using a plotly graph object scatter instead of a plotly express scatter.
AIMPED
October 10, 2022, 4:11pm
8
try this:
fig.for_each_coloraxis(lambda x: x['colorbar'].update({'orientation':'h', 'thickness':20, 'y': -1.0}))
AIMPED
October 10, 2022, 4:24pm
9
Yes, unfortunately you are right. I tried with go. Scatter() and my solution did not work anymore.
AIMPED
October 11, 2022, 12:48pm
10
Hi @dashingpy ,
I think I figured it out:
import plotly.graph_objects as go
from plotly.subplots import make_subplots
# create base figure
fig = make_subplots(rows=1,cols=2, shared_yaxes=True)
# create traces using plotly.graph_objects
b1 = go.Scatter(x=[1,2,3], y=[1,2,3], mode='markers' ,marker={'color':[35,67,89], 'colorscale': 'Viridis','showscale':True})
b2 = go.Scatter(x=[5,2,6], y=[9,2,4], mode='markers' ,marker={'color':[22,5,111]})
# add traces to figure
fig.add_traces(b1, rows=1, cols=1)
fig.add_traces(b2, rows=1, cols=2)
# hide legend
fig.update_layout(showlegend = False)
which creates
NOW
fig.update_traces({'marker': {'colorbar': {'orientation': 'h', 'y': -1.0}, 'colorscale': 'Viridis'}})
changes to this:
Please tell me, this works for you
1 Like
YES!!! you are my hero.
Thank you so much for saving the day, this was defeating me
1 Like
AIMPED
January 24, 2023, 11:08am
13
Hi @SkepticalFlapJack welcome to the forums. Might be due to the plotly version, did you try updating to the current version?