如何在Matplotlib python中将图像用作x轴标签

时间:2018-10-21 20:36:14

标签: python matplotlib

我试图将国家/地区标志用作x轴标签,并且遇到了此问题 不错的stackoverflow post。这为如何绘制绘图提供了一些提示,但是我很难绕轴移动标志。

到目前为止,我已经做到了:

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()

可以在here中找到标志。

我将它们放在本地文件夹flags/*.png中。

我的结果: enter image description here

相关链接:

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

问题
我在更改标志的x,y坐标时遇到困难。我想要它们 全部位于刻度标签下方。

0 个答案:

没有答案
相关问题