# Add Horizontal & Vertical Line to plotly Graph in Python (Examples)

Hello again! It’s nice to be back with another interesting plotly in Python programming tutorial of Statistics Globe. In this tutorial, I will show you how to add horizontal and vertical lines to your plotly graphs using the Python programming language. As you will soon see, it is very easy and simple to do.

But first, let’s take a quick look at how we shall go about it:

Looks like we are ready to go!

## Install & Import plotly

`pip install plotly`

Now that we have downloaded and installed plotly, we need to import it into our Python programming environment. To do so, we need to run the line of code below.

`import plotly.express as px`

The above line of code will import plotly and give us access to all of its plot-building functions, one of which we will make use of in this tutorial to build a simple scatter plot.

## Build Simple Plotly Plot

We will be making use of the popular iris dataset. Fortunately, the iris dataset comes preloaded in the Python plotly library. Therefore, we can call it by running:

`df = px.data.iris()`

You can print out the first 10 rows of the DataFrame by running:

`print(df.head(10))`
```#   sepal_length  sepal_width  petal_length  petal_width species  species_id
#0           5.1          3.5           1.4          0.2  setosa           1
#1           4.9          3.0           1.4          0.2  setosa           1
#2           4.7          3.2           1.3          0.2  setosa           1
#3           4.6          3.1           1.5          0.2  setosa           1
#4           5.0          3.6           1.4          0.2  setosa           1
#5           5.4          3.9           1.7          0.4  setosa           1
#6           4.6          3.4           1.4          0.3  setosa           1
#7           5.0          3.4           1.5          0.2  setosa           1
#8           4.4          2.9           1.4          0.2  setosa           1
#9           4.9          3.1           1.5          0.1  setosa           1```

We are now going to build a simple scatter plot visualizing the Petal Length against the Sepal Width. Please run the lines of code below.

```fig = px.scatter(df, x = "petal_length", y = "sepal_width", color = "species")
fig.show()```

The above plot is a simple scatter plot. In the next section, we will demonstrate how to add lines to our plot.

## Add Horizontal Line to plotly Plot

Lines are typically added to a graph when we want to highlight a particular point or value on the graph. Here, we are going to add a horizontal line to our plot above at point 3 on the Y axis to highlight that point.

```fig = px.scatter(df, x = "petal_length", y = "sepal_width", color = "species")
fig.show()```

We have used the `add_hline()` function to add a horizontal line to our plot above, and we specified what point on the Y axis we wanted to highlight as well. Let’s now see how to add a vertical line to our plotly plot.

## Add Vertical Line to plotly Plot

Adding a vertical line to our scatter plot is as simple as adding a horizontal line. This time, we will make use of the `add_vline()` function as demonstrated below.

```fig = px.scatter(df, x = "petal_length", y = "sepal_width", color = "species")
fig.show()```

As you can see, we highlighted point 5 on the X axis of the plot above via a vertical line.

## Add Horizontal and Vertical Lines to plotly Plot

Now, let us add both horizontal and vertical lines to our scatter plot:

```fig = px.scatter(df, x = "petal_length", y = "sepal_width", color = "species")
fig.show()```

That is how to add either a horizontal or vertical line, or both, to your plotly graph in Python. Very easy and simple to do.

## Video, Further Resources & Summary

Do you need more explanations on how to add horizontal and vertical lines to plotly graphs in Python? Then you should have a look at the following YouTube video of the Statistics Globe YouTube channel.

In the video, we explain in some more detail how to add horizontal and vertical lines to plotly graphs in Python.

Believing you have learned something new in this tutorial, I urge you to check out other interesting plotly in Python tutorials on Statistics Globe:

This post has shown how to add horizontal and vertical lines to plotly graphs in Python. In case you have further questions, you may leave a comment below. Thank you for reading, and I will see you at the next one!

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