将图像插入饼图切片 [英] Insert image into pie chart slice
问题描述
我正在使用python 3.5.2
我想制作一个嵌入了png图像的饼图。我有一些我希望插入切片的批量产品的图片。例如,一片中的草莓和另一片中的覆盆子。很像图片
这肯定是可能的。我们可以从正常的饼图开始。然后我们需要将图像放入图中。这是使用 plt.imread
并使用 matplotlib.offsetbox.OffsetImage
。我们需要找到好的坐标和缩放级别来放置图像,这样它就会与相应的饼形楔形完全重叠。然后,饼图的楔形路径用作图像的剪辑路径,例如只留下楔子里面的部分。将未填充的楔形的zorder设置为较大的数字可确保将边框放置在图像的顶部。这样看起来楔子充满了图像。
从matplotlib导入mattlotlib.pyplot为plt
。补丁从matplotlib.offsetbox导入PathPatch
导入OffsetImage,AnnotationBbox
total = [5,7,4]
labels = [Raspberries,Blueberries,Blackberries ]
plt.title('Berries')
plt.gca()。axis(equal)
wedges,texts = plt.pie(total,startangle = 90,labels = labels ,
wedgeprops = {'linewidth':2,edgecolor:k,fill:False,})
def img_to_pie(fn,wedge,xy ,zoom = 1,ax = None):
如果ax ==无:ax = plt.gca()
im = plt.imread(fn,format ='png')
path = wedge.get_path()
patch = PathPatch(path,facecolor ='none')
ax.add_patch(patch)
imagebox = OffsetImage(im,zoom = zoom,clip_path = patch, zorder = -10)
ab = AnnotationBbox(imagebox,xy,xycoords ='data',pad = 0,frameon = False)
ax.add_artist(ab)
仓位= [( - 1,0.3),(0,-0.5),(0.5,0.5)]
缩放= [0.4,0.4,0.4]
for i in range(3):
fn =data / {}。png.format(labels [i] .lower())
img_to_pie(fn,wedges [i],xy = positions [i],zoom = zoom [i])
wedges [i] .set_zorder(10)
plt.show()
I am using python 3.5.2 I would like to make a pie chart with an png image imbedded. I have pictures of certain bulk products that I would like to insert into the slices. For example strawberries in one slice and raspberries in another. Much like the picture http://www.python-course.eu/images/pie_chart_with_raspberries.png shows.
I can produce images and even plot images instead of points as demonstrated here Matplotlib: How to plot images instead of points?
However, I could not find any approach towards what I am proposing. I suppose it could be manually done in paint, but I was trying to avoid that.
That is sure possible. We can start with a normal pie chart. Then we would need to get the images into the plot. This is done using plt.imread
and by using a matplotlib.offsetbox.OffsetImage
. We would need to find good coordinates and zoom levels to place the image, such that it overlapps completely with respective pie wedge. Then the Path of the pie's wedge is used as a clip path of the image, such that only the part inside the wedge is left over. Setting the zorder of the unfilled wedge to a high number ensures the borders to be placed on top of the image. This way it looks like the wedges are filled with the image.
import matplotlib.pyplot as plt
from matplotlib.patches import PathPatch
from matplotlib.offsetbox import OffsetImage, AnnotationBbox
total = [5,7,4]
labels = ["Raspberries", "Blueberries", "Blackberries"]
plt.title('Berries')
plt.gca().axis("equal")
wedges, texts = plt.pie(total, startangle=90, labels=labels,
wedgeprops = { 'linewidth': 2, "edgecolor" :"k","fill":False, })
def img_to_pie( fn, wedge, xy, zoom=1, ax = None):
if ax==None: ax=plt.gca()
im = plt.imread(fn, format='png')
path = wedge.get_path()
patch = PathPatch(path, facecolor='none')
ax.add_patch(patch)
imagebox = OffsetImage(im, zoom=zoom, clip_path=patch, zorder=-10)
ab = AnnotationBbox(imagebox, xy, xycoords='data', pad=0, frameon=False)
ax.add_artist(ab)
positions = [(-1,0.3),(0,-0.5),(0.5,0.5)]
zooms = [0.4,0.4,0.4]
for i in range(3):
fn = "data/{}.png".format(labels[i].lower())
img_to_pie(fn, wedges[i], xy=positions[i], zoom=zooms[i] )
wedges[i].set_zorder(10)
plt.show()
这篇关于将图像插入饼图切片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!