In [1]:
from lec_utils import *
This notebook just serves to show you even more examples of visualizations you can create using plotly
.
Historical examples¶
William Playfair is known as the "father of data visualization", and is the creator of line charts, bar charts, and pie charts, among other things.
In this first section, we'll create some of his historical charts using plotly
!
Imports and exports from Scotland¶
First, we'll recreate the very first known example of a bar chart, which depicts the imports and exports of Scotland to various countries in 1781.
In [2]:
scotland = pd.read_csv('data/playfair-scotland.csv')
scotland
Out[2]:
country | imports | exports | |
---|---|---|---|
0 | Ireland | 195685 | 305167 |
1 | America | 49826 | 183620 |
2 | West Indies | 169375 | 141220 |
... | ... | ... | ... |
13 | Greenland | 8291 | 0 |
14 | Isle of Man & Jersey | 802 | 1818 |
15 | Denmark and Norway | 28118 | 35011 |
16 rows × 3 columns
Let's see how we can make an interactive version of this plot. The library plotly
will come in handy here.
In [3]:
fig = px.bar(scotland.sort_values('imports', ascending=False),
x=['exports', 'imports'],
y='country',
barmode='group',
orientation='h',
color_discrete_map={
'exports': '#151EA6',
'imports': '#FCB305',
},
title='Exports and Imports of <b>Scotland</b> to and from different parts for one Year'
)
fig.update_layout(
font_family="Arial",
title_font_family="Arial",
paper_bgcolor='#FFFFFF',
plot_bgcolor='#FFFFFF',
legend = {
'title': '',
'orientation': 'h'
}
)
fig.add_annotation( # add a text callout with arrow
text="no exports to Greenland!", x=10000, y="Greenland", ax=125,
arrowhead=2, showarrow=True
)
fig.update_xaxes(title_text='',
side='top',
showline=True,
linewidth=2,
linecolor='black',
mirror=True,
showgrid=True,
gridwidth=1,
gridcolor='#EEEEEE',
tick0=0,
dtick=25000,
tickangle=0)
fig.update_yaxes(title_text='',
side='right',
showline=True,
linewidth=2,
linecolor='black',
mirror=True,
showgrid=True,
gridwidth=1,
gridcolor='#EEEEEE',
tickson='boundaries')