I'm trying to country insert a country flag at the end of the bar chart.
How to show the country flag at the end of the bar chart?
Best example that I'm trying to emulate is here: bar chart
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
from IPython.display import HTML
url = 'https://gist.githubusercontent.com/bagraprac75/e9f880f31367a23e2ce1cf4ff830813f/raw/7ce2f0442a3b93eba989ae3556a501b52187bc5c/Test-Table-people-with-flags2.csv'
df = pd.read_csv(url, usecols=['name', 'ForM', 'year', 'value', 'countryflags'])
colors = dict(zip(
["Female", "Male"],
["#ffc0cb", "#007aff"]
))
group_lk = df.set_index('name')['ForM'].to_dict()
fig, ax = plt.subplots(figsize=(15, 8))
def draw_barchart(current_year):
dff = df[df['year'].eq(current_year)].sort_values(by='value', ascending=True).tail(10)
ax.clear()
ax.barh(dff['name'], dff['value'], color=[colors[group_lk[x]] for x in dff['name']])
dx = dff['value'].max() / 200
for i, (value, name) in enumerate(zip(dff['value'], dff['name'])):
ax.text(value-dx, i, name, size=10, weight=600, ha='right', va='center')
ax.text(value+dx, i, f'{value:,.0f}', size=12, ha='left', va='center')
ax.xaxis.set_major_formatter(ticker.StrMethodFormatter('{x:,.0f}'))
ax.xaxis.set_ticks_position('top')
ax.tick_params(axis='x', colors='#999999', labelsize=10)
ax.set_yticks([])
ax.margins(0, 0.01)
ax.grid(which='major', axis='x', linestyle='-',color='#c5c5c5')
ax.set_axisbelow(True)
plt.box(False)
draw_barchart(2018)