fizix137
fizix137

Reputation: 120

matplotlib: Applying an image as the background of a matplotlib wedge

I want to insert the image of a flag into the wedge of a pie chart in matplotlib. I tried to do it by modifying the each patch with the image of the flag through the Figure atribute, but it didn't work. The end goal is to place the appropriate flag in each wedge.

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

Upvotes: 1

Views: 448

Answers (0)

Related Questions