matplotlib:在matplotlib饼图中应用图像作为背景

2024-09-30 03:26:03 发布

您现在位置:Python中文网/ 问答频道 /正文

我想在matplotlib中将国旗的图像插入饼图的楔形。我试图通过图形属性修改每个补丁中的国旗图像,但没有成功。最终目标是在每个楔子中放置适当的旗帜。在

http://en.wikipedia.org/wiki/File:Flag_of_Ireland.svg

import matplotlib.pyplot as plt
import collections

iname = r'D:\code\flags\ireland_flag.gif'
image = plt.imread(iname)

gg1 = 'Ireland'
gg2 = 'Ireland'
gg3 = 'Italy'
gg4 = 'Slovakia'
gg5 = 'USA'
gg6 = 'Germany'
gg7 = 'USA'
gg8 = 'USA'

gg = [gg1,gg2,gg3,gg4,gg5,gg6,gg7,gg8]

counter=collections.Counter(gg)

labels = list(counter.keys())
sizes = list(counter.values())

patches, texts, autotexts = plt.pie(sizes, labels=labels, autopct='%1.1f%%', shadow=True)
patches[1].set_figure(image)
plt.axis('equal')
plt.show()

Tags: 图像imageimportlabelsmatplotlibcounterpltcollections

热门问题