全てのコードを掲載
ここでは、本記事で解説している全てのコードを掲載しています。
リスト19-1: Article146.py
# Article145 Plotly Bar Charts v20.py
# %%
### import the libraries
import numpy as np
import pandas as pd
import plotly.io as pio
import plotly.express as px
import plotly.graph_objects as go
# %%
'''
plotly.express.bar(
data_frame=None,
x=None, y=None,
color=None,
pattern_shape=None, ★
facet_row=None, facet_col=None, facet_col_wrap=0,
facet_row_spacing=None, facet_col_spacing=None,
hover_name=None, hover_data=None, custom_data=None, ★
text=None, ★
base=None,
error_x=None, error_x_minus=None,
error_y=None, error_y_minus=None,
animation_frame=None, animation_group=None,
category_orders=None,
labels=None, ★
color_discrete_sequence=None, color_discrete_map=None, color_continuous_scale=None,
pattern_shape_sequence=None, pattern_shape_map=None,
range_color=None,
color_continuous_midpoint=None,
opacity=None,
orientation=None, ★ 'h' for horizontal 'v' for vertical
barmode='relative', ★ 'relative', 'group', 'overlay'
log_x=False, log_y=False,
range_x=None, range_y=None,
text_auto=False, ★
title=None, ★
template=None, ★
width=None, height=None)
→ plotly.graph_objects._figure.Figure
'''
# %%
# 1: set a defulat template
pio.templates.default = 'plotly_dark'
pio.templates
# %%
# 2: simple bar chart
add = "Simple Bar Chart"
text = f"px.bar(x=year, y=population): <b style='color:red'>{add}</b>"
year = [1950,1960,1970,1980,1990]
population = [86459025,95831757,125956499,137065841,147467972]
fig = px.bar(x=year, y=population)
fig.add_annotation(text=text,
xref="paper", yref="paper",
x=0.0, y=1.1,
showarrow=False)
fig.show()
# %%
# 3: add a title
title = f"px.bar(df, x=, y=, <b style='color:red'>title='...'</b>)"
fig = px.bar(x=year, y=population,
title=title
)
fig.show()
# %%
# 4: add a template (theme)
add = "template='seaborn'"
title = f"px.bar(df, x=, y=, <b style='color:red'>{add}</b>)"
fig = px.bar(x=year, y=population,
template='seaborn',
title=title
)
fig.show()
# %%
# 5: load data from plotly express gapminder()
raw_df = px.data.gapminder()
# raw_df.info()
# # Column Non-Null Count Dtype
# --- ------ -------------- -----
# 0 country 1704 non-null object
# 1 continent 1704 non-null object
# 2 year 1704 non-null int64
# 3 lifeExp 1704 non-null float64
# 4 pop 1704 non-null int64
# 5 gdpPercap 1704 non-null float64
# 6 iso_alpha 1704 non-null object
# 7 iso_num 1704 non-null int64
# raw_df
# country continent year lifeExp pop gdpPercap iso_alpha iso_num
# 0 Afghanistan Asia 1952 28.801 8425333 779.445314 AFG 4
# 1 Afghanistan Asia 1957 30.332 9240934 820.853030 AFG 4
# 2 Afghanistan Asia 1962 31.997 10267083 853.100710 AFG 4
# 3 Afghanistan Asia 1967 34.020 11537966 836.197138 AFG 4
# 4 Afghanistan Asia 1972 36.088 13079460 739.981106 AFG 4
# ... ... ... ... ... ... ... ... ...
# 1699 Zimbabwe Africa 1987 62.351 9216418 706.157306 ZWE 716
# 1700 Zimbabwe Africa 1992 60.377 10704340 693.420786 ZWE 716
# 1701 Zimbabwe Africa 1997 46.809 11404948 792.449960 ZWE 716
# 1702 Zimbabwe Africa 2002 39.989 11926563 672.038623 ZWE 716
# 1703 Zimbabwe Africa 2007 43.487 12311143 469.709298 ZWE 716
# raw_df['continent'].unique()
df = raw_df.query("continent=='Asia'")
# df['country'].unique()
# array(['Afghanistan', 'Bahrain', 'Bangladesh', 'Cambodia', 'China',
# 'Hong Kong, China', 'India', 'Indonesia', 'Iran', 'Iraq', 'Israel',
# 'Japan', 'Jordan', 'Korea, Dem. Rep.', 'Korea, Rep.', 'Kuwait',
# 'Lebanon', 'Malaysia', 'Mongolia', 'Myanmar', 'Nepal', 'Oman',
# 'Pakistan', 'Philippines', 'Saudi Arabia', 'Singapore',
# 'Sri Lanka', 'Syria', 'Taiwan', 'Thailand', 'Vietnam',
# 'West Bank and Gaza', 'Yemen, Rep.'], dtype=object)
# len(df['country'].unique().tolist()) # 33
df = raw_df.query("country=='Japan'")
df
# country continent year lifeExp pop gdpPercap iso_alpha iso_num
# 792 Japan Asia 1952 63.030 86459025 3216.956347 JPN 392
# 793 Japan Asia 1957 65.500 91563009 4317.694365 JPN 392
# 794 Japan Asia 1962 68.730 95831757 6576.649461 JPN 392
# 795 Japan Asia 1967 71.430 100825279 9847.788607 JPN 392
# 796 Japan Asia 1972 73.420 107188273 14778.786360 JPN 392
# 797 Japan Asia 1977 75.380 113872473 16610.377010 JPN 392
# 798 Japan Asia 1982 77.110 118454974 19384.105710 JPN 392
# 799 Japan Asia 1987 78.670 122091325 22375.941890 JPN 392
# 800 Japan Asia 1992 79.360 124329269 26824.895110 JPN 392
# 801 Japan Asia 1997 80.690 125956499 28816.584990 JPN 392
# 802 Japan Asia 2002 82.000 127065841 28604.591900 JPN 392
# 803 Japan Asia 2007 82.603 127467972 31656.068060 JPN 392
# %%
# 6: bar chart : add a color
add = "color='year'"
title = f"px.bar(df, x=, y=, <b style='color:red'>{add}</b>)"
df = raw_df.query("country=='Japan'")
fig = px.bar(df,
x='year', y='pop',
color='year',
title=title
)
fig.show()
# %%
# 7: bar chart : add a labels
add = "labels=dict(year='Year', pop='Population')"
title = f"px.bar(, <b style='color:red'>{add}</b>)"
df = raw_df.query("country=='Japan'")
fig = px.bar(df,
x='year', y='pop',
color='year',
labels=dict(year='Year', pop='Population'),
title=title
)
fig.show()
# %%
# 8: bar chart : Asia top 6 + hove_name, hover_data
# https://plotly.com/python/hover-text-and-formatting/
add = "hover_name='country', hover_data=['lifeExp']"
title = f"px.bar(, <b style='color:red'>{add}</b>)"
df = raw_df.query("continent=='Asia'") \
.query("year==2007") \
.nlargest(6, 'pop')
df.reset_index(inplace=True)
fig = px.bar(df,
x='country', y='pop',
color='country',
hover_name='country',
hover_data=['lifeExp'],
labels=dict(country='Country', pop='Population', lifeExp='Life Expectancy'),
title=title
)
fig.show()
# %%
# 9: bar chart : Asia top 6 + custom_data, traces(hovertemplate)
add = "custom_data=['rank']"
traces = "hovertemplate=..."
title = f"px.bar(, <b style='color:red'>{add}</b>) + update_traces(<b style='color:red'>{traces}</b>)"
df = raw_df.query("continent=='Asia'") \
.query("year==2007") \
.nlargest(6, 'pop')
df.reset_index(inplace=True)
df['rank'] = df.index+1
fig = px.bar(df,
x='country', y='pop',
color='country',
custom_data=['rank'],
labels=dict(country='Country', pop='Population'),
title=title
)
fig.update_traces(
hovertemplate="%{label}<br><br>Rank: %{customdata}<br>Population: %{value}"
)
fig.show()
# %%
# 10-1: bar chart : text_auto=True
add = "text_auto=True"
title = f"px.bar(, <b style='color:red'>{add}</b>)"
filter_country = (raw_df['country'].isin(['Japan','China','India'])) & \
(raw_df['year'] > 1990)
df = raw_df[filter_country]
df
fig = px.bar(df,
x='year', y='pop',
color='country',
text_auto=True,
labels=dict(year='Year', pop='Population', country='Country'),
title=title
)
fig.show()
# %%
# 10-2: bar chart : text='country'
add = "text='country'"
title = f"px.bar(, <b style='color:red'>{add}</b>)"
filter_country = (raw_df['country'].isin(['Japan','China','India'])) & \
(raw_df['year'] > 1990)
df = raw_df[filter_country]
df
fig = px.bar(df,
x='year', y='pop',
color='country',
text='country',
labels=dict(year='Year', pop='Population', country='Country'),
title=title
)
fig.show()
# %%
# 10-3: bar chart : text_auto + textposition=inside)
add = "text_auto='.2s'"
title = f"px.bar(, <b style='color:red'>{add}</b>)"
df = raw_df.query("continent == 'Asia' and year == 2007 and pop > 70.e6")
# df = raw_df.query("continent == 'Asia' and year == 2007 and pop > 2.e6")
df = df.sort_values('pop', ascending=False).reset_index(drop=True)
df
fig = px.bar(df,
x='country', y='pop',
color='country',
text_auto='.2s',
labels=dict(year='Year', pop='Population', country='Country'),
title=title
)
fig.show()
# %%
# 10-4: bar chart : text_auto + update_traces(textposition=outside)
add = "text_auto='.2s'"
traces = "textposition='outside'"
title = f"px.bar(, <b style='color:red'>{add}</b>) + update_traces(<b style='color:red'>{traces}</b>)"
df = raw_df.query("continent == 'Asia' and year == 2007 and pop > 70.e6")
# df = raw_df.query("continent == 'Asia' and year == 2007 and pop > 2.e6")
df = df.sort_values('pop', ascending=False).reset_index(drop=True)
df
fig = px.bar(df,
x='country', y='pop',
color='country',
text_auto='.2s',
labels=dict(year='Year', pop='Population', country='Country'),
title=title
)
fig.update_traces(textfont_size=12, textangle=0, textposition='outside', cliponaxis=False)
fig.show()
# %%
# 11-1: bar chart : barmode=relative
add = "barmode='relative'"
title = f"px.bar(, <b style='color:red'>{add}</b>)"
filter_country = raw_df['country'].isin(['Japan','China','India'])
df = raw_df[filter_country]
# df
fig = px.bar(df,
x='year', y='pop',
color='country',
barmode='relative', # relative(default), group, overlay
labels=dict(year='Year', pop='Population', country='Country'),
title=title
)
fig.show()
# %%
# 11-2: bar chart : barmode=group
add = "barmode='group'"
title = f"px.bar(, <b style='color:red'>{add}</b>)"
filter_country = raw_df['country'].isin(['Japan','China','India'])
df = raw_df[filter_country]
# df
fig = px.bar(df,
x='year', y='pop',
color='country',
barmode='group', # relative(default), group, overlay
labels=dict(year='Year', pop='Population', country='Country'),
title=title
)
fig.show()
# %%
# 11-3: bar chart : barmode=overlay
add = "barmode='overlay'"
title = f"px.bar(, <b style='color:red'>{add}</b>)"
filter_country = raw_df['country'].isin(['Japan','China','India'])
df = raw_df[filter_country]
# df
fig = px.bar(df,
x='year', y='pop',
color='country',
barmode='overlay', # relative(default), group, overlay
labels=dict(year='Year', pop='Population', country='Country'),
title=title
)
fig.show()
# %%
# 12: bar chart : Basic Horizontal Bar Chart
add = "orientation='h'"
title = f"px.bar(, <b style='color:red'>{add}</b>)"
filter_country = raw_df['country'].isin(['Japan','China','India'])
df = raw_df[filter_country]
# df
fig = px.bar(df,
x='pop', y='country',
color='country',
# barmode='overlay', # relative(default), group, overlay
orientation='h', # h-horizontal, v-vertical
labels=dict(year='Year', pop='Population', country='Country'),
title=title
)
fig.show()
# %%
# 13: bar chart : Rotated Bar Chart Labels
add = ""
layout="xaxis_tickangle=-45"
title = f"px.bar(, <b style='color:red'>{add}</b>) + update_layout(<b style='color:red'>{layout}</b>)"
filter_country = raw_df['country'].isin(['Japan','China','India'])
df = raw_df[filter_country]
# df
fig = px.bar(df,
x='year', y='pop',
color='country',
# barmode='relative', # relative(default), group, overlay
labels=dict(year='Year', pop='Population', country='Country'),
title=title
)
fig.update_layout(xaxis_tickangle=-45)
fig.show()
# %%
# 14: bar chart : Pattern Fills
add = "pattern_shape='country', pattern_shape_sequence=['.', 'x', '+']"
title = f"px.bar(, <b style='color:red'>{add}</b>)"
filter_country = (raw_df['country'].isin(['Japan','China','India'])) & \
(raw_df['year'] > 1990)
df = raw_df[filter_country]
df
fig = px.bar(df,
x='year', y='pop',
color='country',
pattern_shape='country', pattern_shape_sequence=['.', 'x', '+'],
# text='country',
labels=dict(year='Year', pop='Population', country='Country'),
title=title
)
fig.show()
# %%
# 15: bar chart : color=continent : toggle continent(America, Asia, Europe)
add = "color='continent'"
title = f"px.bar(, <b style='color:red'>{add}</b>) + <b style='color:red'>toggle continent(America, Asia, Europe)</b>"
df = raw_df.groupby(['year','continent']) \
.agg({
'lifeExp': 'last',
'pop': 'sum',
'gdpPercap': 'sum'
}).reset_index()
df
fig = px.bar(df,
x='year', y='pop',
color='continent',
labels=dict(year='Year', pop='Population', country='Country'),
title=title
)
fig.show()
# %%
# 16-1: Horizontal Bar Chart: top / bottom N populations in the world
add = "orientation='h'"
title = f"px.bar(, <b style='color:red'>{add}</b>) + <b style='color:red'>Top 10 populations in the world</b>"
df = raw_df.query("year==2007") \
.nlargest(10, 'pop')
df.reset_index(inplace=True)
df['rank'] = df.index+1
# df
fig = px.bar(df,
x='pop', y='country',
color='country',
custom_data=['rank'],
# barmode='overlay', # relative(default), group, overlay
orientation='h', # h-horizontal, v-vertical
labels=dict(year='Year', pop='Population', country='Country'),
title=title
)
fig.update_traces(
textposition='inside',
texttemplate='Rank: %{customdata}'
)
fig.show()
# %%
# 16-2: Horizontal Bar Chart: top / bottom N populations + add_annotation()
add = "orientation='h'"
title = f"px.bar(, <b style='color:red'>{add}</b>) + <b style='color:red'>add_annotation(📍)</b>"
df = raw_df.query("year==2007") \
.nlargest(10, 'pop')
df.reset_index(inplace=True)
df['rank'] = df.index+1
# df
fig = px.bar(df,
x='pop', y='country',
color='country',
custom_data=['rank'],
# barmode='overlay', # relative(default), group, overlay
orientation='h', # h-horizontal, v-vertical
labels=dict(year='Year', pop='Population', country='Country'),
title=title
# title='Top 10 Most Populated Countries in the World in 2007'
)
fig.add_annotation(
text=' 📍 ', # 🎌 📌 📍
# text=' ▶ ', # 🎌
y='Japan',
showarrow=False
)
fig.update_traces(
textposition='inside',
texttemplate='Rank: %{customdata}'
)
fig.show()
# %%
# 16-3: Horizontal Bar Chart: op / bottom N lifeExp + texttemplate='Rank: %{customdata}'
add = "orientation='h'"
title = f"px.bar(, <b style='color:red'>{add}</b>) + <b style='color:red'>Bottom 10 Life Expectancy in the World in 2007</b>"
df = raw_df.query("year==2007") \
.nsmallest(10, 'lifeExp')
df.reset_index(inplace=True)
df['rank'] = df.index+1
# df
fig = px.bar(df,
x='lifeExp', y='country',
color='country',
custom_data=['rank'],
# barmode='overlay', # relative(default), group, overlay
orientation='h', # h-horizontal, v-vertical
labels=dict(year='Year', pop='Population', country='Country', lifeExp='Life Expectancy'),
title=title
# title='Bottom 10 Life Expectancy in the World in 2007'
)
fig.update_traces(
textposition='inside',
texttemplate='Rank: %{customdata}'
)
fig.show()
# %%
# 17-1: Horizontal Bar Chart: pop top 5%
add = "orientation='h'"
title = f"px.bar(, <b style='color:red'>{add}</b>) + <b style='color:red'>Top 5% Population in the World in 2007</b>"
df = raw_df.query("year==2007")[['country','pop']]
top_5p = df.set_index('country').quantile(0.95)
filter = f"pop >= {top_5p.values[0]}" # pop >= 99999
df = raw_df.query("year==2007") \
.query(filter)
df = df.sort_values('pop', ascending=False).reset_index(drop=True)
df['rank'] = df.index+1
# df
fig = px.bar(df,
x='pop', y='country',
color='country',
custom_data=['rank'],
# barmode='overlay', # relative(default), group, overlay
orientation='h', # h-horizontal, v-vertical
labels=dict(year='Year', pop='Population', country='Country', lifeExp='Life Expectancy'),
title=title
# title='Top 5% Population in the World in 2007'
)
fig.update_traces(
textposition='inside',
texttemplate='Rank: %{customdata}'
)
fig.show()
# %%
# 17-2: Horizontal Bar Chart: gdpPercap top 15%
add = "orientation='h'"
title = f"px.bar(, <b style='color:red'>{add}</b>) + <b style='color:red'>Top 15% GDP per Capita in the World in 2007</b>"
df = raw_df.query("continent=='Asia'") \
.query("year==2007")[['country','gdpPercap']]
top_15p = df.set_index('country').quantile(0.85)
filter = f"gdpPercap >= {top_15p.values[0]}" # gdpPercap >= 99999
df = raw_df.query("continent=='Asia'") \
.query("year==2007") \
.query(filter)
df = df.sort_values('gdpPercap', ascending=False).reset_index(drop=True)
df['rank'] = df.index+1
# df
fig = px.bar(df,
x='gdpPercap', y='country',
color='country',
custom_data=['rank'],
# barmode='overlay', # relative(default), group, overlay
orientation='h', # h-horizontal, v-vertical
labels=dict(year='Year', pop='Population', country='Country', gdpPercap='GDP per Capita'),
title=title
# title='Top 15% GDP per Capita in the World in 2007'
)
fig.add_annotation(
text=' 📍 ', # 🎌 📌 📍
# text=' ▶ ', # 🎌
y='Japan',
showarrow=False
)
fig.update_traces(
textposition='inside',
texttemplate='Rank: %{customdata}'
)
fig.show()
# %%
# 17-3: Horizontal Bar Chart: lifeExp top 5% + annotation
add = "orientation='h'"
title = f"px.bar(, <b style='color:red'>{add}</b>) + <b style='color:red'>Top 5% Life Expectancy in the World in 2007</b>"
df = raw_df.query("year==2007")[['country','lifeExp']]
top_5p = df.set_index('country').quantile(0.95)
filter = f"lifeExp >= {top_5p.values[0]}" # lifeExp >= 99999
df = raw_df.query("year==2007") \
.query(filter)
df = df.sort_values('lifeExp', ascending=False).reset_index(drop=True)
df['rank'] = df.index+1
# df
fig = px.bar(df,
x='lifeExp', y='country',
color='country',
custom_data=['rank'],
# barmode='overlay', # relative(default), group, overlay
orientation='h', # h-horizontal, v-vertical
labels=dict(year='Year', pop='Population', country='Country', lifeExp='Life Expectancy'),
title=title
# title='Top 5% Life Expectancy in the World in 2007'
)
fig.add_annotation(
text=' 📍 ', # 🎌 📌 📍
# text=' ▶ ', # 🎌
y='Japan',
showarrow=False
)
fig.update_traces(
textposition='inside',
texttemplate='Rank: %{customdata}'
)
fig.show()
# %%
# 17-4: Horizontal Bar Chart: lifeExp bottom 20% + annotation
add = "orientation='h'"
title = f"px.bar(, <b style='color:red'>{add}</b>) + <b style='color:red'>Bottom 20% Life Expectancy in the World in 2007</b>"
df = raw_df.query("continent=='Asia'") \
.query("year==2007")[['country','lifeExp']]
bottom_20p = df.set_index('country').quantile(0.20)
filter = f"lifeExp <= {bottom_20p.values[0]}" # lifeExp <= 99999
df = raw_df.query("continent=='Asia'") \
.query("year==2007") \
.query(filter)
df = df.sort_values('lifeExp', ascending=True).reset_index(drop=True)
df['rank'] = df.index+1
# df
fig = px.bar(df,
x='lifeExp', y='country',
color='country',
custom_data=['rank'],
# barmode='overlay', # relative(default), group, overlay
orientation='h', # h-horizontal, v-vertical
labels=dict(year='Year', pop='Population', country='Country', lifeExp='Life Expectancy'),
title=title
# title='Bottom 20% Life Expectancy in the World in 2007'
)
fig.update_traces(
textposition='inside',
texttemplate='Life Expectancy(%{x}), Rank(%{customdata})'
)
fig.show()