Reputation: 5278
I'm setting the width and height of my plots with
fig.update_layout(width=500, height=500)
Unfortunately, this produces differently sized plots, depending on the width of the labels, as seen below.
However, I do not find a way to produce equally sized plots.
fig.add_trace(go.Scatter(...))
Neither add_trace
nor go.Scatter
allows setting the width. Is there a way I'm overlooking?
MVE:
fig = go.Figure()
fig.update_layout(width=500, height=500)
for i, data in enumerate(data_list):
fig.add_trace(go.Scatter(x=data['x'], y=data['y'], name='some_label'))
Upvotes: 5
Views: 2535
Reputation: 2762
A workaround is specifying a position inside the limits of the plot in the layout, like this:
fig.update_layout(legend=dict(
yanchor="bottom",
y=0.8,
xanchor="right",
x=1
))
Where y starts in 0.8
With this complete code I get the images shown below:
import numpy as np
import pandas as pd
import plotly.graph_objects as go
from plotly.subplots import make_subplots
df = pd.DataFrame({
'x':np.arange(1,10),
'y':np.array([1,2,4,8,16,32,64,128,256]),
'y2':np.random.randint(0,100,9)
})
fig1 = go.Figure()
fig1.add_trace(go.Scatter(x=df['x'], y=df['y2'], name='short_legend'))
fig1.add_trace(go.Scatter(x=df['x'], y=df['y'], name='short_legend_B'))
fig1.update_layout(width=500, height=500)
fig2 = go.Figure()
fig2.add_trace(go.Scatter(x=df['x'], y=df['y2'], name='loooooooooooooooooong_legend'))
fig2.add_trace(go.Scatter(x=df['x'], y=df['y'], name='loooooolololololololoong_legend_B'))
fig2.update_layout(width=500, height=500)
First row is without updating the layout as mentioned above. Below is updating the layout.
Upvotes: 3