- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to set the range of Y-axis in Python Plotly?
Plotly supports to the range on both X and Y axis. Let us understand how to set the range of Y-axis in Plotly.
plotly.graph_objects is used to generate figures. It contains a lot of methods to customize charts and render a chart in HTML format.
Create a numpy module and generate random ranges for both X and Y axis.
Create Figure() method to plot X and Y axis with mode as lines
Create update_layout() method and set the Y-axis range.
Follow the steps given to set the range of Y-axis in Plotly.
Step 1 − Import plotly
Import plotly.graphs_objs module and alias as go
import plotly.graphs_objs as go
Step 2 − Import numpy
Import numpy module and alias as np and set the random seed value.
import numpy as np np.random.seed(3)
Step 3 − Generate random number on X-axis
Let us generate a list of random range of numbers on X-axis.
x = list(range(0,20,2))
Step 4 − Generate random number on Y-axis
Generate random number on Y-axis as follows −
y = np.random.randn(10)
Step 5 − Generate the scatter plot
Let us generate the scatter plot with the following coordinates −
fig = go.Figure(data=go.Scatter(x=x, y=y, mode='lines'))
Step 6 − Set the Y-axis range
Set the Y-axis range using update_layout() method.
fig.update_layout(yaxis_range=[-3,3])
Step 7 − Display the figure
Display the figure using show() method.
fig.show()
Example
The complete code to set the range of Y-axis in Python Plotly is as follows −
# Importing Libraries import plotly.graph_objs as go import numpy as np np.random.seed(3) # generating numbers ranging from 0 to 18 on X-axis x = list(range(0,20,2)) # generating random numbers on y-axis y = np.random.randn(10) # plotting scatter plot on x and y data with # 'lines' as mode fig = go.Figure(data=go.Scatter(x=x, y=y, mode='lines')) # setting the y-axis range from -3 to 3 fig.update_layout(yaxis_range=[-3,3]) # to display the figure in the output screen fig.show()
Output
It will show the following output on the browser −
- Related Articles
- How to plot on secondary Y-Axis with Python Plotly?
- How to plot multiple lines on the same Y-axis in Python Plotly?
- How to hide the Y-axis tick labels on a chart in Python Plotly?
- How to set the range of Y-axis for a Seaborn boxplot using Matplotlib?
- How to change the range of the X-axis and Y-axis in Matplotlib?
- How to set the Y-axis in radians in a Python plot?
- How to set the line color in Python Plotly?
- How to show all the X-axis tick values in Python Plotly?
- How to set the font style to Bold in Python Plotly?
- Python Plotly – How to manually set the color of points in a scatter plot?
- How to set the Y-axis tick marks using ggplot2 in R?
- How to set the alignment of the JLabel content along the Y axis in Java?
- How to set the angle of skew on Y-axis of Ellipse using FabricJS?
- How to shade a chart above a specific Y value in Python Plotly?
- How to set X-axis values in Matplotlib Python?
