给条形图添加图像注释

11

例如,假设我有一些数据。

countries = ["Norway", "Spain", "Germany", "Canada", "China"]
valuesA = [20, 15, 30, 5, 26]
valuesB = [1, 5, 3, 6, 2] 

我确实想要像这样绘制它们

this.

我该如何将这些旗帜图片放入图表中(如果可能的话)? 其次,我如何自动化这个过程?

1个回答

24

这种解决方案适用于使用 matplotlibseabornpandas.DataFrame.plot 生成的轴级图。

主要思路是将问题分解为小块:

  1. 将标记作为数组传入脚本。例如:

     def get_flag(name):
         path = "path/to/flag/{}.png".format(name)
         im = plt.imread(path)
         return im
    
  2. 在图中的特定位置放置图片,可以使用OffsetImage。可以在matplotlib页面上找到示例。最好使用一个函数,该函数接受国家名称和位置作为参数,并生成带有OffsetImageAnnotationBbox

  3. 使用ax.bar绘制条形图。要将国家名称设置为刻度标签,请使用ax.set_ticklabels(countries)。然后对于每个国家,使用循环放置上面的OffsetImage

(coord, 0)xybox=(0., -16.)可以调整以将图像注释放置在任何位置。

最终结果可能类似于这样:

enter image description here

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.offsetbox import OffsetImage,AnnotationBbox

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

def offset_image(coord, name, ax):
    img = get_flag(name)
    im = OffsetImage(img, zoom=0.72)
    im.image.axes = ax

    ab = AnnotationBbox(im, (coord, 0),  xybox=(0., -16.), frameon=False,
                        xycoords='data',  boxcoords="offset points", pad=0)

    ax.add_artist(ab)
    

countries = ["Norway", "Spain", "Germany", "Canada", "China"]
valuesA = [20, 15, 30, 5, 26]
 

fig, ax = plt.subplots()

ax.bar(range(len(countries)), valuesA, width=0.5,align="center")
ax.set_xticks(range(len(countries)))
ax.set_xticklabels(countries)
ax.tick_params(axis='x', which='major', pad=26)

for i, c in enumerate(countries):
    offset_image(i, c, ax)

plt.show()

1
旗帜来源:https://github.com/google/region-flags 或 https://github.com/hjnilsson/country-flags - juanbretti
如果您只想使用旗帜而不是国家名称,您将如何更改此示例以使其工作?您是否仍然需要使用这种方法,还是可以直接使用图像作为x轴标签? - Raleigh L.
你能更详细地说明AnnotationBbox函数的所有参数吗?比如xy值是用来将图像与标签对齐的。 - pas-calc
1
你更改了Y值,使得德国现在得分最高(无论以什么单位来衡量;-))。 - pas-calc

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接