0

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)
  • 1
    Did you try anything yet? Like at least look at [how other people got flags in their bar charts](https://stackoverflow.com/questions/44246650/automating-bar-charts-plotting-to-show-country-flags-as-ticklabels/44264051#44264051)? – ImportanceOfBeingErnest Dec 20 '19 at 18:11
  • I think you stepped forward to answer a question I wanted to ask about: how to show the country flag as ticklabels? But I am looking for how to insert the image into the bar (not at axis). – helloFGon82 Dec 20 '19 at 21:03
  • I didn't answer anything. I suggested that you try something yourself, based on existing solutions. The difference between adding the images as "ticklabels" and adding them on the bar tips is just a matter of using different coordinates. If you encounter a problem trying to implement something, feel free to ask about it. – ImportanceOfBeingErnest Dec 20 '19 at 21:21

0 Answers0