Matplotlib Make Center Circle Transparent

I draw a pie chart that makes the background in the png image transparent. How can I make the center circle also transparent instead of white? enter image description here

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)
+4
source share
2 answers

The way you create the white middle part in the above code is to entangle the center of the pie in a circle. This, of course, cannot provide a transparent interior.

A solution to this can also be found in the more complex question Double donut plot in matplotlib . Let me dwell in detail:

, , . , 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)

:

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


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

+3

, .

centre_circle = plt.Circle((0,0),0.75,edgecolor='black', 
facecolor='white',fill=True,linewidth=0.25)

. , , . , pixlr, . , , , , .

0

Source: https://habr.com/ru/post/1684024/


All Articles