Matplotlib 使中心圆透明

Moh*_*him 7 python plot geometry png matplotlib

我正在绘制一个饼图,使 png 图像中的背景看起来透明。如何使中心圆看起来也透明而不是白色? 在此处输入图片说明

import matplotlib.pyplot as plt

# Pie chart, where the slices will be ordered and plotted counter-clockwise:
labels = 'Correct', 'Wrong'
sizes = [20, 80]

fig1, ax1 = plt.subplots()
ax1.pie(sizes,colors=['green','red'], labels=labels,autopct='%1.1f%%', 
shadow=True, startangle=90)
centre_circle = plt.Circle((0,0),0.75,edgecolor='black', 
facecolor='white',fill=True,linewidth=0.25)
fig1 = plt.gcf()
fig1.gca().add_artist(centre_circle)
ax1.axis('equal')  # Equal aspect ratio ensures that pie is drawn as a circle.
fig1.savefig('foo.png', transparent=True)
Run Code Online (Sandbox Code Playgroud)

Imp*_*est 4

在上面的代码中创建白色中间部分的方法是用圆圈模糊饼图的中心。这当然不能产生透明的内部。

在 matplotlib 中更复杂的问题 Double Donut Chart中也可以找到此问题的解决方案。让我详细说一下:

为了生成中间有孔的真正的圆环图,需要切割楔形物,使它们成为部分环。幸运的是,matplotlib 提供了这样做的工具。饼图由多个楔形图组成。从 matplotlib.patches.Wedge文档中我们了解到

class matplotlib.patches.Wedge(center, r, theta1, theta2, width=None, **kwargs)
楔形贴片。r - width[...] 如果给定宽度,则从内半径到外半径绘制部分楔形r

为了给所有楔形设置宽度,一个简单的方法是使用plt.setp

wedges, _ = ax.pie([20,80], ...)
plt.setp( wedges, width=0.25)
Run Code Online (Sandbox Code Playgroud)

完整示例:

import matplotlib.pyplot as plt

fig, ax = plt.subplots()
fig.set_facecolor("#fff9c9") # set yellow background color to see effect

wedges, text, autotext = ax.pie([25, 40], colors=['limegreen','crimson'],
                                labels=['Correct', 'Wrong'], autopct='%1.1f%%')
plt.setp( wedges, width=0.25)

ax.set_aspect("equal")
# the produced png will have a transparent background
plt.savefig(__file__+".png", transparent=True)
plt.show()
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述


Wedge如果没有争论,以下将是解决问题的方法width。由于饼图以 为中心(0,0),因此复制外部路径坐标,将其恢复并乘以某个小于 1 的数字(r在下面的代码中称为半径),得到内环的坐标。连接这两个坐标列表并处理正确的路径代码可以根据需要创建环形。

import matplotlib.pyplot as plt
import matplotlib.path as mpath
import matplotlib.patches as mpatches
import numpy as np

def cutwedge(wedge, r=0.8):
    path = wedge.get_path()
    verts = path.vertices[:-3]
    codes = path.codes[:-3]
    new_verts = np.vstack((verts , verts[::-1]*r, verts[0,:]))
    new_codes =  np.concatenate((codes , codes[::-1], np.array([79])) )
    new_codes[len(codes)] = 2
    new_path = mpath.Path(new_verts, new_codes)
    new_patch = mpatches.PathPatch(new_path)
    new_patch.update_from(wedge)
    wedge.set_visible(False)
    wedge.axes.add_patch(new_patch)
    return new_patch

fig, ax = plt.subplots()
fig.set_facecolor("#fff9c9") # set yellow background color to see effect


wedges, text, autotext = ax.pie([25, 75], colors=['limegreen','indigo'], 
                                labels=['Correct', 'Wrong'], autopct='%1.1f%%')

for w in wedges:
    cutwedge(w)
    # or try cutwedge(w, r=0.4)

ax.set_aspect("equal")

# the produced png will have a transparent background
plt.savefig(__file__+".png", transparent=True)
plt.show()
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述