Is there a way to start a plot already zoomed on a specific area using plotly?
Question:
I have a scatter plot made with plotly (specifically offline plotly with the Python API on a Jupyter Notebook) and as you know, plotly makes it easy for the user to zoom and frame specific areas, but I’d like the plot to start already focussed on a specific area of my choosing.
I can’t find anything relevant in the documentation (maybe because I don’t know where to look or what terms to look up). Is there a way to do this, and if so, how? And how does the setting differ when using subplots rather than a Figure object?
Answers:
When you specify your Layout
, under the xaxis
and yaxis
parameters, you can specify a range
, e.g.
import plotly.graph_objs as go
# ...
layout = go.Layout(
yaxis=dict(
range=[0, 100]
),
xaxis=dict(
range=[100, 200]
)
)
fig = go.Figure(data=data, layout=layout)
Documentation for this can be found for the xaxis
here and yaxis
here.
If your axis includes dates, then make sure you specify the type as date before setting the range. Otherwise, you’ll receive unexpected results.
start_date = "2019-09-26"
start_date = "2019-10-18"
fig.update_xaxes(type="date", range=[start_date, end_date])
If the fig
consists of subplots with shared x axes (or y axes), you can set the above range to the row and column corresponding to the last shared plot.
The following is for a single-column subplot with each a graph plot in each row, totalling 7 rows.
last_row = 7
last_col = 1
fig.update_xaxes(type="date", range=[start, end], row=last_row, col=last_col)
As mentioned, range within layout works. I was unable to use
...
type = "date",
xaxis = dict[date1, date2]
but converting the (POSIXct) dates to numeric worked for me.
(not enough rep to comment, but I felt it was worth adding that conversion works if date ranges don’t).
Use aspectratio=go.layout.scene.Aspectratio(x=2, y=2, z=2)
to zoom the plot by 2x.
For example:
layout = go.Layout(
autosize=False, width=1200, height=800,
title = '3D Spherical Topography Map',
titlefont = dict(family='Courier New', color=titlecolor),
showlegend = False,
scene = dict(
xaxis = noaxis,
yaxis = noaxis,
zaxis = noaxis,
aspectmode='manual',
aspectratio=go.layout.scene.Aspectratio(
x=2, y=2, z=2)),
paper_bgcolor = bgcolor,
plot_bgcolor = bgcolor)
I have a scatter plot made with plotly (specifically offline plotly with the Python API on a Jupyter Notebook) and as you know, plotly makes it easy for the user to zoom and frame specific areas, but I’d like the plot to start already focussed on a specific area of my choosing.
I can’t find anything relevant in the documentation (maybe because I don’t know where to look or what terms to look up). Is there a way to do this, and if so, how? And how does the setting differ when using subplots rather than a Figure object?
When you specify your Layout
, under the xaxis
and yaxis
parameters, you can specify a range
, e.g.
import plotly.graph_objs as go
# ...
layout = go.Layout(
yaxis=dict(
range=[0, 100]
),
xaxis=dict(
range=[100, 200]
)
)
fig = go.Figure(data=data, layout=layout)
Documentation for this can be found for the xaxis
here and yaxis
here.
If your axis includes dates, then make sure you specify the type as date before setting the range. Otherwise, you’ll receive unexpected results.
start_date = "2019-09-26"
start_date = "2019-10-18"
fig.update_xaxes(type="date", range=[start_date, end_date])
If the fig
consists of subplots with shared x axes (or y axes), you can set the above range to the row and column corresponding to the last shared plot.
The following is for a single-column subplot with each a graph plot in each row, totalling 7 rows.
last_row = 7
last_col = 1
fig.update_xaxes(type="date", range=[start, end], row=last_row, col=last_col)
As mentioned, range within layout works. I was unable to use
...
type = "date",
xaxis = dict[date1, date2]
but converting the (POSIXct) dates to numeric worked for me.
(not enough rep to comment, but I felt it was worth adding that conversion works if date ranges don’t).
Use aspectratio=go.layout.scene.Aspectratio(x=2, y=2, z=2)
to zoom the plot by 2x.
For example:
layout = go.Layout(
autosize=False, width=1200, height=800,
title = '3D Spherical Topography Map',
titlefont = dict(family='Courier New', color=titlecolor),
showlegend = False,
scene = dict(
xaxis = noaxis,
yaxis = noaxis,
zaxis = noaxis,
aspectmode='manual',
aspectratio=go.layout.scene.Aspectratio(
x=2, y=2, z=2)),
paper_bgcolor = bgcolor,
plot_bgcolor = bgcolor)