This post extends the post on area charts in matplotlib. Area chart is a type of visualization used to present data with different groups and show change between the groups over time. It’s similar to line graph but the area under the line is shaded with colour. Area charts come in different kinds such as step-area chart, spline-area chart and streamgraphs. In this post we will look at what area charts are, when to use them and how to use area charts in plotly. Download the data for this post from here.
When to Use Area chart
Area charts are used to show trends over time among related attributes.
How to Use Area Chart
- Use fewer attributes to make the visualization clear and easy to read.
- Select colours that blend well for visual clarity.
- Order the areas appropriately. Usually start with large areas at the bottom and move upwards as the size keep decreasing.
Area Charts in Plotly
Load Required Libraries
import numpy as np
import pandas as pd
import plotly.express as px
import plotly.graph_objects as go
Load Data
ex_rate_df=pd.read_csv('Exchange_Rates.csv')
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)
ex_rate_df.head()
Simple Area Chart
fig=go.Figure()
fig = px.area(ex_rate_df[ex_rate_df['Country'].isin(['FRA'])], x="Year", y="Rate", color="Country",line_group="Country")
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()
Stacked Area Chart
fig=go.Figure()
fig.add_trace(go.Scatter(x=ex_rate_df['Year'], y=ex_rate_df[ex_rate_df['Country'].isin(['FRA'])]['Rate'],stackgroup='one',
name='FRA',line=dict(color='orange', width=4,shape='linear')))
fig.add_trace(go.Scatter(x=ex_rate_df['Year'], y=ex_rate_df[ex_rate_df['Country'].isin(['AUS'])]['Rate'],stackgroup='one',
name='AUS',line=dict(color='lightgreen', width=4,shape='linear')))
fig.add_trace(go.Scatter(x=ex_rate_df['Year'], y=ex_rate_df[ex_rate_df['Country'].isin(['CAN'])]['Rate'],stackgroup='one',
name='CAN',line=dict(color='skyblue', 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.99,xanchor="right",x=0.5,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()
100% Stacked Area Chart
fig=go.Figure()
fig.add_trace(go.Scatter(x=ex_rate_df['Year'], y=ex_rate_df[ex_rate_df['Country'].isin(['FRA'])]['Rate'],stackgroup='one',
groupnorm='percent', name='FRA',line=dict(color='orange', width=4,shape='linear')))
fig.add_trace(go.Scatter(x=ex_rate_df['Year'], y=ex_rate_df[ex_rate_df['Country'].isin(['AUS'])]['Rate'],stackgroup='one',
name='AUS',line=dict(color='lightgreen', width=4,shape='linear')))
fig.add_trace(go.Scatter(x=ex_rate_df['Year'], y=ex_rate_df[ex_rate_df['Country'].isin(['CAN'])]['Rate'],stackgroup='one',
name='CAN',line=dict(color='skyblue', 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.99,xanchor="right",x=0.5,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'),
yaxis=dict(range=[1, 100],ticksuffix='%'))
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 the jupyter notebook here
Conclusion
In this post we have looked at area chart, when to use it and how to use it in plotly. In the next post we will look at Boxplot and how to use it in plotly. To learn about scatter plot in plotly check our previous post here.