1

I was trying to use country flags as x-axis label and encountered this nice stackoverflow post. This gives some hints how to draw the plot, but I am having difficulty moving the flages around the axes.

So far I have done this:

def get_flag(name):
    path = "flags/{}.png".format(name)
    im = plt.imread(path)
    return im

import matplotlib.pyplot as plt
import numpy as np

from matplotlib.patches import Circle
from matplotlib.offsetbox import (TextArea, DrawingArea, OffsetImage,
                                  AnnotationBbox)
from matplotlib.cbook import get_sample_data


# axes
fig, ax = plt.subplots()

# data
x = [1, 2, 3, 4, 5] 
y = [20, 15, 30, 5, 26]

countries = ['','ad','ae','af','ag','ai']

# bar plot
ax.bar(x,y)
ax.set_xticklabels(countries)

# image
arr_img = get_flag('ad')

# add image
imagebox = OffsetImage(arr_img, zoom=0.4)
imagebox.image.axes = ax

xy = (0.6, 0.1)
ab = AnnotationBbox(imagebox, xy,
                    xybox=(1.4, -0.3),
                    xycoords='data',
                    boxcoords="offset points",
                    pad=0.5,
                    arrowprops=dict(
                        arrowstyle="->",
                        connectionstyle="angle,angleA=0,angleB=90,rad=3")
                    )

ax.add_artist(ab)
plt.tight_layout()

The flages can be found here.

I have them in local folder flags/*.png.

My result: enter image description here

Related links:

https://github.com/hjnilsson/country-flags
Automating bar charts plotting to show country flags as ticklabels

Problem
I am having difficulty changing x,y coordinates of flag. I want them all below the tick labels.

0 Answers0