This post extends the post for line graph in seaborn and matplotlib. Line graph also known as line chart or line plot is used to track changes over a period of time. It’s ideal for clearly shows the trend especially when the changes are smaller. Line graphs are usually used for time series analysis. One axis usually x-axis is used to show time intervals and the other axis usually y-axis is used to show the continuous values. We can have one line or multiple lines in one graph each comparing the changes of individual measure over time. Plotly provides us with functions to create interactive line graphs. In this post we will see when to use line graphs and how to create line graphs in plotly. Download the Exchange Rates dataset for this post. ## Line Graph Use-Cases

Line chart is one of the most common and basic type of visualization used for visualizing continuous values against continuous variable such as time, distance, speed etc. It’s best suited for visualizing data with chronological trends. It can be used to compare the change between two or more groups.

## How to Use Line Graph

1. Your data should has a chronological variable such as time, distance, etc.
2. Select a good interval that will clearly show all data points.
3. Use as fewer lines as possible when necessary to avoid cluttering the visualization.
4. Ensure that the line/s cover almost 90% of the canvas.
5. Show data points in the line.
6. Interpolate the gaps in the line where necessary unless you want to detect missing data.
7. When measuring two different metrics in the same graph select appropriate scales for dual axis.

## Line Graphs in Plotly

Import Required Libraries

```                    ```
import numpy as np
import pandas as pd
import plotly.express as px
import plotly.graph_objects as go
```
```

```                    ```
ex_rate_df['Country']=ex_rate_df['LOCATION']
ex_rate_df['Year']=ex_rate_df['TIME']
ex_rate_df['Rate']=ex_rate_df['Value'].round(2)
```
``` Simple line graph with plotly express

```                    ```
fig = px.line(ex_rate_df[ex_rate_df['Country'].isin(['FRA'])], x="Year", y="Rate", color='Country',
title='Exchange Rate for FRA')

fig.update_layout(title={'text': 'Exchange Rate for FRA','y':0.95,'x':0.5, 'xanchor': 'center','yanchor': 'top'},
legend=dict(yanchor="top",y=0.95,xanchor="right",x=0.95),
autosize=True,margin=dict(t=70,b=0,l=0,r=0), xaxis_title='Year', yaxis_title='Rate',
font=dict(size=20, family='Times New Romans', color='brown') )

fig.update_xaxes(showline=True, linewidth=1, linecolor='white', gridwidth=3, gridcolor='white', mirror=True)
fig.update_yaxes(showline=True, linewidth=1, linecolor='white', gridwidth=3, gridcolor='white', mirror=True)

fig.show()
```
``` Multiple line graph with plotly express

```                    ```
fig = px.line(ex_rate_df[ex_rate_df['Country'].isin(['FRA','CAN','AUS'])], x="Year", y="Rate",
color='Country',  title='Exchange Rate for AUS, CAN and FRA')

fig.update_layout(title={'text': 'Exchange Rate for FRA','y':0.95,'x':0.5, 'xanchor': 'center','yanchor': 'top'},
legend=dict(yanchor="top",y=0.95,xanchor="right",x=0.95),
autosize=True,margin=dict(t=70,b=0,l=0,r=0), xaxis_title='Year', yaxis_title='Rate',
font=dict(size=20, family='Times New Romans', color='brown') )

fig.update_xaxes(showline=True, linewidth=1, linecolor='white', gridwidth=3, gridcolor='white', mirror=True)
fig.update_yaxes(showline=True, linewidth=1, linecolor='white', gridwidth=3, gridcolor='white', mirror=True)

fig.show()
```
``` Simple line graph with plotly graph object

```                    ```
fig=go.Figure()
name='FRA',text='Rate',line=dict(color='skyblue', width=4,shape='linear')))

fig.update_layout(title={'text': 'Exchange Rate for FRA','y':0.95,'x':0.5, 'xanchor': 'center','yanchor': 'top'},
legend=dict(yanchor="top",y=0.95,xanchor="right",x=0.95),
autosize=True,margin=dict(t=70,b=0,l=0,r=0), xaxis_title='Year', yaxis_title='Rate',
font=dict(size=20, family='Times New Romans', color='brown') )

fig.update_xaxes(showline=True, linewidth=1, linecolor='white', gridwidth=3, gridcolor='white', mirror=True)
fig.update_yaxes(showline=True, linewidth=1, linecolor='white', gridwidth=3, gridcolor='white', mirror=True)

fig.show()
```
``` Multi-line graph with plotly graph object

```                    ```
fig=go.Figure()
name='FRA',line=dict(color='skyblue', width=4,shape='linear')))

name='AUS',line=dict(color='orange', width=4,shape='linear')))

name='CAN',line=dict(color='green', width=4,shape='linear')))

fig.update_layout(title={'text': 'Exchange Rate for FRA, AUS and CAN','y':0.95,'x':0.5, 'xanchor': 'center','yanchor': 'top'},
legend=dict(yanchor="top",y=0.9,xanchor="right",x=0.95,title='Currency'),
autosize=True,margin=dict(t=70,b=0,l=0,r=0), xaxis_title='Year', yaxis_title='Rate',
font=dict(size=20, family='Times New Romans', color='brown') )

fig.update_xaxes(showline=True, linewidth=1, linecolor='white', gridwidth=3, gridcolor='white', mirror=True)
fig.update_yaxes(showline=True, linewidth=1, linecolor='white', gridwidth=3, gridcolor='white', mirror=True)

fig.show()
```
``` For complete code check jupyter notebook here

## Conclusion

In this post we have looked at what’s line chart, when and to use it. Line charts are commonly used for visualizing time series data. Plotly provides us with easy and simple functions for creating line chart. In the next post we will learn about Bar charts when and how to use them in plotly. To learn about creating line graph in seaborn check our post here. To learn about plotly and why it’s a good choice for creating interactive data visualizations check our previous post on introduction to plotly.

Line Graphs in Plotly 