Reputation: 229
I'm stuck with this silly problem and can't solve it. I need for several graphs to have the same x axis range (because they are shown on top of each other and have timestamp as x axis). My first guess was to simply find the lowest and the highest of x values for all the plots and use them as x axis range for all of them. It kinda works, but now the graphs look bad and the marker doesn't fit (see the picture).
import plotly.graph_objs as go
x1 = [1, 2, 3]
y1 = [1, 2, 1]
x2 = [1, 2]
y2 = [2, 1]
xmin = min(min(x1), min(x2))
xmax = max(max(x1), max(x2))
figure1=go.Figure(
data=[
go.Scatter(x=x1,
y=y1,
marker={'size': 20})
],
layout={'xaxis': {'range': [xmin, xmax]}}
)
figure2=go.Figure(
data=[
go.Scatter(x=x2,
y=y2,
marker={'size': 20})
],
layout={'xaxis': {'range': [xmin, xmax]}}
)
figure1.show()
figure2.show()
Is it possible to automatically adjust axis limits just enough for the marker to fit? Or maybe there is another solution for this task?
Thanks!
BTW, if I could just read what are the axis limits that plotly sets automatically, that would be very helpful
Upvotes: 1
Views: 1726
Reputation: 914
You just need to add
figure1.update_layout(xaxis = dict(range=[0.9, 3.1]))
figure2.update_layout(xaxis = dict(range=[0.9, 2.1]))
before figure1.show() and figure2.show()
Upvotes: 0
Reputation: 61084
In many cases you only need to add a little adjustments to your calculated xmin
and xmax
:
You can create more space between the plotting area and the axis lines using the pad
argument in go.layout.Margin()
. You can adjust the setup further using the l, r, t, b
arguments too.
# imports
import plotly.graph_objs as go
x1 = [1, 2, 3]
y1 = [1, 2, 1]
x2 = [1, 2]
y2 = [2, 1]
xmin = min(min(x1), min(x2))
xmax = max(max(x1), max(x2))
xmin_adjust = 0.5
xmax_adjust = 0.5
figure1=go.Figure(
data=[go.Scatter(x=x1,
y=y1,
marker={'size': 20})],
layout={'xaxis': {'range': [xmin-xmin_adjust, xmax+xmax_adjust]},
'margin':go.layout.Margin(l=50, r=50, b=50, t=50, pad=50)})
figure2=go.Figure(
data=[go.Scatter(x=x2,
y=y2,
marker={'size': 20})],
layout={'xaxis': {'range': [xmin-xmin_adjust, xmax+xmax_adjust]},
'margin':go.layout.Margin(l=50, r=50, b=50, t=50, pad=50)})
figure1.show()
figure2.show()
Upvotes: 1