Home >Backend Development >Python Tutorial >How to set the range of Y-axis in Python Plotly?
Plotly supports setting ranges on the X-axis 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.
CreateFigure() method to draw the X-axis and Y-axis in line mode
Create update_layout() method and set the Y-axis range.
Follow the given steps to set the range of the Y-axis in Plotly.
Step 1 - Import plotly
Import the plotly.graph_objs module and alias it to go
<span class="kwd">import</span><span class="pln"> plotly</span><span class="pun">.</span><span class="pln">graphs_objs </span><span class="kwd">as</span><span class="pln"> go</span>
Step 2 − Import numpy
Import the numpy module and set its alias to np, and set a random seed value.
<span class="kwd">import</span><span class="pln"> numpy </span><span class="kwd">as</span><span class="pln"> np np</span><span class="pun">.</span><span class="pln">random</span><span class="pun">.</span><span class="pln">seed</span><span class="pun">(</span><span class="lit">3</span><span class="pun">)</span>
Step 3 − Generate random number on X-axis
Let us generate a list of random range of numbers on X-axis.
<span class="pln">x </span><span class="pun">=</span><span class="pln"> list</span><span class="pun">(</span><span class="pln">range</span><span class="pun">(</span><span class="lit">0</span><span class="pun">,</span><span class="lit">20</span><span class="pun">,</span><span class="lit">2</span><span class="pun">))</span>
Step 4 - Generate random numbers on the Y-axis
Generate random numbers on the Y-axis as follows -
<span class="pln">y </span><span class="pun">=</span><span class="pln"> np</span><span class="pun">.</span><span class="pln">random</span><span class="pun">.</span><span class="pln">randn</span><span class="pun">(</span><span class="lit">10</span><span class="pun">)</span>
Step 5 − Generate the scatter plot
Let us generate the scatter plot with the following coordinates −
<span class="pln">fig </span><span class="pun">=</span><span class="pln"> go</span><span class="pun">.</span><span class="typ">Figure</span><span class="pun">(</span><span class="pln">data</span><span class="pun">=</span><span class="pln">go</span><span class="pun">.</span><span class="typ">Scatter</span><span class="pun">(</span><span class="pln">x</span><span class="pun">=</span><span class="pln">x</span><span class="pun">,</span><span class="pln"> y</span><span class="pun">=</span><span class="pln">y</span><span class="pun">,</span><span class="pln"> mode</span><span class="pun">=</span><span class="str">'lines'</span><span class="pun">))</span>
Step 6 − Set the Y-axis range
Use the update_layout() method to set the Y-axis range.
<span class="pln">fig</span><span class="pun">.</span><span class="pln">update_layout</span><span class="pun">(</span><span class="pln">yaxis_range</span><span class="pun">=[-</span><span class="lit">3</span><span class="pun">,</span><span class="lit">3</span><span class="pun">])</span>
Step 7 - Display Image
Use the show() method to display the chart.
<span class="pln">fig</span><span class="pun">.</span><span class="pln">show</span><span class="pun">()</span>The Chinese translation of
The complete code for setting the Y-axis range in Python Plotly is as follows:
<span class="com"># Importing Libraries</span><span class="pln"> </span><span class="kwd">import</span><span class="pln"> plotly</span><span class="pun">.</span><span class="pln">graph_objs </span><span class="kwd">as</span><span class="pln"> go </span><span class="kwd">import</span><span class="pln"> numpy </span><span class="kwd">as</span><span class="pln"> np np</span><span class="pun">.</span><span class="pln">random</span><span class="pun">.</span><span class="pln">seed</span><span class="pun">(</span><span class="lit">3</span><span class="pun">)</span><span class="pln"> </span><span class="com"># generating numbers ranging from 0 to 18 on X-axis</span><span class="pln"> x </span><span class="pun">=</span><span class="pln"> list</span><span class="pun">(</span><span class="pln">range</span><span class="pun">(</span><span class="lit">0</span><span class="pun">,</span><span class="lit">20</span><span class="pun">,</span><span class="lit">2</span><span class="pun">))</span><span class="pln"> </span><span class="com"># generating random numbers on y-axis</span><span class="pln"> y </span><span class="pun">=</span><span class="pln"> np</span><span class="pun">.</span><span class="pln">random</span><span class="pun">.</span><span class="pln">randn</span><span class="pun">(</span><span class="lit">10</span><span class="pun">)</span><span class="pln"> </span><span class="com"># plotting scatter plot on x and y data with</span><span class="pln"> </span><span class="com"># 'lines' as mode</span><span class="pln"> fig </span><span class="pun">=</span><span class="pln"> go</span><span class="pun">.</span><span class="typ">Figure</span><span class="pun">(</span><span class="pln">data</span><span class="pun">=</span><span class="pln">go</span><span class="pun">.</span><span class="typ">Scatter</span><span class="pun">(</span><span class="pln">x</span><span class="pun">=</span><span class="pln">x</span><span class="pun">,</span><span class="pln"> y</span><span class="pun">=</span><span class="pln">y</span><span class="pun">,</span><span class="pln"> mode</span><span class="pun">=</span><span class="str">'lines'</span><span class="pun">))</span><span class="pln"> </span><span class="com"># setting the y-axis range from -3 to 3</span><span class="pln"> fig</span><span class="pun">.</span><span class="pln">update_layout</span><span class="pun">(</span><span class="pln">yaxis_range</span><span class="pun">=[-</span><span class="lit">3</span><span class="pun">,</span><span class="lit">3</span><span class="pun">])</span><span class="pln"> </span><span class="com"># to display the figure in the output screen</span><span class="pln"> fig</span><span class="pun">.</span><span class="pln">show</span><span class="pun">()</span>
It will display the following output on the browser −
The above is the detailed content of How to set the range of Y-axis in Python Plotly?. For more information, please follow other related articles on the PHP Chinese website!