Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plotly: How to set the range of the y axis?

I have the following code to create the line plot with Plotly. How can I set the range of Y axis to always have it in [0; 10]?

layout = go.Layout(         title=go.layout.Title(             text="Test",             xref='paper',             x=0         ),         xaxis=go.layout.XAxis(             tickmode='linear',             tickfont=dict(                 size=10             ),             title=go.layout.xaxis.Title(                 font=dict(                     size=14,                     color='#7f7f7f'                 )             )         ),         yaxis=go.layout.YAxis(             title=go.layout.yaxis.Title(                 text=y,                 font=dict(                     size=14,                     color='#7f7f7f'                 )             )         )     )      data = [go.Scatter(x=x1, y=y1)] 
like image 913
Tatik Avatar asked Apr 16 '19 08:04

Tatik


People also ask

How do you set the range of the Y axis in Plotly?

Scatter() function to make a scatter plot. The go. Figure() function takes in data as input where we set the mode as 'lines' using mode='lines'. We have used the magic underscore notation i.e layout_yaxis_range=[-8,8] to set the y-axis range from -8 to 8.

How do you set Y axis limits?

Set y-Axis Limits for Specific AxesCall the tiledlayout function to create a 2-by-1 tiled chart layout. Call the nexttile function to create the axes objects ax1 and ax2 . Plot data into each axes. Then set the y-axis limits for the bottom plot by specifying ax2 as the first input argument to ylim .

How do you set an axes range in Python?

Import matplotlib.To set x-axis scale to log, use xscale() function and pass log to it. To plot the graph, use plot() function. To set the limits of the x-axis, use xlim() function and pass max and min value to it. To set the limits of the y-axis, use ylim() function and pass top and bottom value to it.


1 Answers

Update for newer versions

When setting up a figure you can use plotly's magic underscore notation and specify layout_yaxis_range=[<from_value>, <to_value>] like this:

fig = go.Figure(data=go.Scatter(x=x, y=y, mode='lines'), layout_yaxis_range=[-4,4]) 

Or if you've already got a figure named fig, you can use:

fig.update_layout(yaxis_range=[-4,4]) 

And:

fig.update(layout_yaxis_range = [-4,4]) 

Or:

fig.update_yaxes(range = [-4,4]) 

Figure:

enter image description here

Complete code:

# imports import pandas as pd import plotly.graph_objs as go import numpy as np  # data np.random.seed(4) x = np.linspace(0, 1, 50) y = np.cumsum(np.random.randn(50))  # plotly line chart fig = go.Figure(data=go.Scatter(x=x, y=y, mode='lines'), layout_yaxis_range=[-4,4])                 fig.update_layout(yaxis_range=[-4,4]) fig.show() 

Original answer using plotly.offline, iplot and no magic underscore notation:

When setting up a figure, use:

layout = go.Layout(yaxis=dict(range=[fromValue, toValue]) 

Or if you've already got a figure named fig, you can use:

fig.update_layout(yaxis=dict(range=[fromValue,toValue])) 

Plot:

enter image description here

Complete code for Jupyter Notebook:

# imports from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot import pandas as pd import plotly.graph_objs as go import numpy as np  # setup init_notebook_mode(connected=True)  # data np.random.seed(4) x = np.linspace(0, 1, 50) y = np.cumsum(np.random.randn(50))  # line trace = go.Scatter(     x=x,     y=y, )  # layout layout = go.Layout(yaxis=dict(range=[-4,4]) )  # Plot fig = go.Figure(data=[trace], layout=layout) iplot(fig) 

Some important details:

With this setup, you can easily add an y axis title like this:

# layout layout = go.Layout(yaxis=dict(range=[-4,4]), title='y Axis') ) 

It's a little more tricky if you'd like to format that title further. I find it easiest to actually add another element with title = go.layout.yaxis.Title(text='y Axis', font=dict(size=14, color='#7f7f7f'). As long as you do it the right way, you should not experience the situation in your comment above:

Thanks. I tried it. But then I have 2 definitions of yaxis in the Layout: yaxis=dict(range=[0, 10]) and yaxis=go.layout.YAxis. Therefore an error appears.

Take a look at this:

Plot:

enter image description here

Complete code with y-axis text formatting:

# imports from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot import pandas as pd import plotly.graph_objs as go import numpy as np  # setup init_notebook_mode(connected=True)  # data np.random.seed(4) x = np.linspace(0, 1, 50) y = np.cumsum(np.random.randn(50))  # line trace = go.Scatter(     x=x,     y=y, )  # layout layout = go.Layout(     yaxis=dict(range=[-4,4],     title = go.layout.yaxis.Title(text='y Axis', font=dict(size=14, color='#7f7f7f'))) )  # Plot fig = go.Figure(data=[trace], layout=layout) iplot(fig) 
like image 181
vestland Avatar answered Sep 29 '22 08:09

vestland