如何在坐标轴中添加图像到条形图中(matplotlib)

问题描述 投票:2回答:1

我想在我的柱状图中添加标志图片,比如下面的图片,我试过AnnotationBbox,但显示的是方形轮廓。

enter image description here

我已经尝试过AnnotationBbox,但显示的是一个正方形的轮廓。有谁能告诉我如何实现如上图一样的效果?

编辑。

以下是我的代码

ax.barh(y = y, width = values, color = r, height = 0.8)

height = 0.8
for i, (value, url) in enumerate(zip(values, image_urls)):
    response = requests.get(url)
    img = Image.open(BytesIO(response.content))

    width, height = img.size
    left = 10
    top = 10
    right = width-10
    bottom = height-10
    im1 = img.crop((left, top, right, bottom)) 
    print(im1.size)
    im1

    ax.imshow(im1, extent = [value - 6, value, i - height / 2, i + height / 2], aspect = 'auto', zorder = 2)

编辑2:

height = 0.8
for j, (value, url) in enumerate(zip(ww, image_urls)):
    response = requests.get(url)
    img = Image.open(BytesIO(response.content))
    ax.imshow(img, extent = [value - 6, value - 2, j - height / 2, j + height / 2], aspect = 'auto', zorder = 2)

ax.set_xlim(0, max(ww)*1.05)
ax.set_ylim(-0.5, len(yy) - 0.5)
plt.tight_layout()

enter image description here

python matplotlib plot figure axes
1个回答
1
投票

你需要的图像在一个 .png 格式的透明背景。软件,如Gimp或 图片魔术 如果图像还没有所需的背景,可以帮上忙)。)

有了这样的图片。plt.imshow() 可以将其放置在小区中。位置是通过 extent=[x0, x1, y0, y1]. 为防止 imshow 如果要强制使用相同的纵横比,请添加 aspect='auto'. zorder=2 有助于让图像在条形图上面。之后,在 plt.xlimplt.ylim 需要明确设置(也因为 imshow 乱码)。)

下面的示例代码使用了'ada.png',因为它是matplotlib的标配,所以代码可以独立测试。现在它正在加载来自 国旗网,以下是 此职位.

请注意,图像会被放置在一个盒子里,在 数据座标 (本例中为6宽0.9高)。这个框会被拉伸,例如当图形被调整大小时。你可能会想把6改成另一个值,这取决于x-scale和图的大小。

import numpy as np
import matplotlib.pyplot as plt
# import matplotlib.cbook as cbook
import requests
from io import BytesIO

labels = ['CW', 'CV', 'GW', 'SX', 'DO']
colors = ['crimson', 'dodgerblue', 'teal', 'limegreen', 'gold']
values = 30 + np.random.randint(5, 20, len(labels)).cumsum()

height = 0.9
plt.barh(y=labels, width=values, height=height, color=colors, align='center')

for i, (label, value) in enumerate(zip(labels, values)):
    # load the image corresponding to label into img
    # with cbook.get_sample_data('ada.png') as image_file:
    #    img = plt.imread(image_file)
    response = requests.get(f'https://www.countryflags.io/{label}/flat/64.png')
    img = plt.imread(BytesIO(response.content))
    plt.imshow(img, extent=[value - 8, value - 2, i - height / 2, i + height / 2], aspect='auto', zorder=2)
plt.xlim(0, max(values) * 1.05)
plt.ylim(-0.5, len(labels) - 0.5)
plt.tight_layout()
plt.show()

example plot

PS:正如Ernest在评论中所解释的那样,也可以在 此职位,使用 OffsetImage 图像的纵横比保持不变。(同时, xlimylim 保持不变)。) 当条形图较多时,图像不会缩小,所以你可能需要在以下因素中进行试验 OffsetImage(img, zoom=0.65) 和x-offset在 AnnotationBbox(..., xybox=(-25, 0)).

一个额外的选项可以把标志放在太短的柱子外面。或者在Y轴的左边。

适应水平条的代码可以是这样的。

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

def offset_image(x, y, label, bar_is_too_short, ax):
    response = requests.get(f'https://www.countryflags.io/{label}/flat/64.png')
    img = plt.imread(BytesIO(response.content))
    im = OffsetImage(img, zoom=0.65)
    im.image.axes = ax
    x_offset = -25
    if bar_is_too_short:
        x = 0
    ab = AnnotationBbox(im, (x, y), xybox=(x_offset, 0), frameon=False,
                        xycoords='data', boxcoords="offset points", pad=0)
    ax.add_artist(ab)

labels = ['CW', 'CV', 'GW', 'SX', 'DO']
colors = ['crimson', 'dodgerblue', 'teal', 'limegreen', 'gold']
values = 2 ** np.random.randint(2, 10, len(labels))

height = 0.9
plt.barh(y=labels, width=values, height=height, color=colors, align='center', alpha=0.8)

max_value = values.max()
for i, (label, value) in enumerate(zip(labels, values)):
    offset_image(value, i, label, bar_is_too_short=value < max_value / 10, ax=plt.gca())
plt.subplots_adjust(left=0.15)
plt.show()

example using <code>OffsetImage</code>


1
投票

为了完成@johanC的回答,可以使用下面的标志。iso-flags-png 在GNUlinux和 iso3166 python包。

import matplotlib.pyplot as plt
from iso3166 import countries
import matplotlib.image as mpimg


def pos_image(x, y, pays, haut):
    pays = countries.get(pays).alpha2.lower()
    fichier = "/usr/share/iso-flags-png-320x240"
    fichier += f"/{pays}.png"
    im = mpimg.imread(fichier)
    ratio = 4 / 3
    w = ratio * haut
    ax.imshow(im,
              extent=(x - w, x, y, y + haut),
              zorder=2)


plt.style.use('seaborn')
fig, ax = plt.subplots()

liste_pays = [('France', 10), ('USA', 9), ('Spain', 5), ('Italy', 5)]

X = [p[1] for p in liste_pays]
Y = [p[0] for p in liste_pays]

haut = .8
r = ax.barh(y=Y, width=X, height=haut, zorder=1)
y_bar = [rectangle.get_y() for rectangle in r]
for pays, y in zip(liste_pays, y_bar):
    pos_image(pays[1], y, pays[0], haut)


plt.show()

这给:enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.