# Change plotly Axis Range in Python (Example)

Hello again! Hope you are doing great. In this tutorial, I will show you how to change the axis range of your plotly graph in the Python programming language. It is super simple and easy to do. Just one line of code is all it takes, as we shall soon see.

But first, let’s get a summary of the steps we will follow to accomplish our goal:

Having seen what we are going to do in this tutorial, if you are ready, then I am ready. Let’s go!

## Install Python plotly Library

To build a graph using plotly in Python, we will need to first of all install the Python plotly library. To do so, please run the code below in your Python IDE:

`pip install plotly`

## Import Python plotly Library

With the Python plotly library installed, let us now import it so that we can use its graphing functions. In your IDE, please run the lines of code below:

```import plotly.express as px
import plotly.graph_objs as go```

## Create Dataset

Before we can build a graph, we must have a dataset. Therefore, we are going to create our dataset for this project using both the pandas library and the numpy library. Just as we did for plotly, you can install both libraries with the code below:

```pip install pandas
pip install numpy```

Numpy is a Python library that is used for scientific computing, and we are going to use it to generate random numbers for our project. Pandas, on the other hand, is a Python library that is used for data manipulation and organization. We are going to use pandas to organize our data into a DataFrame so that we can easily see its structure. Please note that you can use any dataset of your choice for this learning exercise.

Now, let us import both libraries:

```import pandas as pd
import numpy as np```

With the libraries imported, let us now create our dataset:

```x = list(range(0,15)
y = np.random.randn(15)
df = pd.DataFrame({"X":x,"Y":y})```

You can take a look at the data by running `print(df)`, which will print out the DataFrame.

```#     X     Y
#0    0  1.118393
#1    1 -0.592305
#2    2  0.082789
#3    3  1.715652
#4    4 -0.749003
#5    5  0.121628
#6    6 -0.363539
#7    7 -0.366433
#8    8  0.048584
#9    9 -0.655344
#10  10 -0.024707
#11  11 -0.210073
#12  12 -2.635815
#13  13 -0.074841
#14  14 -1.771990```

Now, we are ready to build our visualization.

## Build a plotly Line Graph

We shall be building a basic plotly line plot with our created dataset. In your IDE, please run the lines of code below to build and visualize a basic line plot in plotly:

```fig = px.line(df,"X","Y")
fig.show()```

Please note that since we are using a randomly generated dataset, your graph may look slightly different from the graph in this tutorial. But not to worry, you are still on course. Now that we have created our line plot, let us now change the range of the Y axis.

## Modify the Y Axis Range of the Line Graph

With just one line of code, we are going to make the Y axis of our line plot to range from -6 to 6. Please run the code below:

```fig = go.Figure(fig,layout_yaxis_range = [-6,6])
fig.show()```

And that is how to change the axis interval of your plotly graph in Python. As you can see, it is very simple and easy to do.

By the way: In case you would like to change the x-axis range of a plotly graphic, you may use layout_xaxis_range instead of layout_yaxis_range.

I hope you enjoyed this tutorial, and I can’t wait to see you in the next one. Take care!

## Video, Further Resources & Summary

Do you need more explanations on how to draw interactive graphs in Python? Then you should have a look at the following YouTube video of the Statistics Globe YouTube channel.

Furthermore, you could have a look at some of the other tutorials on Statistics Globe:

This post has shown how to change the axis range of a plotly graph in Python. In case you have further questions, you may leave a comment below.

This page was created in collaboration with Ifeanyi Idiaye. You might check out Ifeanyi’s personal author page to read more about his academic background and the other articles he has written for the Statistics Globe website.

Subscribe to the Statistics Globe Newsletter