Matplotlib Make Center Circle Transparent
Asked Answered
Y

3

8

I am plotting a pie chart making background in the png image looks transparent. How can I make the center circle also looks transparent instead of the white color? 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)
Yawata answered 19/8, 2017 at 12:22 Comment(0)
S
4

The way you create the white middle part in the above code is by obfuscating the center of the pie by a circle. This can of course not procude a transparent interior.

A solution to this would also be found in the more sophisticated question Double donut chart in matplotlib. Let me go into detail:

In order to produce a true donut chart with a hole in the middle, one would need to cut the wedges such that they become partial rings. Fortunately, matplotlib provides the tools to do so. A pie chart consists of several wedges. From the matplotlib.patches.Wedge documentation we learn

class matplotlib.patches.Wedge(center, r, theta1, theta2, width=None, **kwargs)
Wedge shaped patch. [...] If width is given, then a partial wedge is drawn from inner radius r - width to outer radius r.

In order to give set the width to all wedges, an easy method is to use plt.setp

wedges, _ = ax.pie([20,80], ...)
plt.setp( wedges, width=0.25)

Complete example:

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

enter image description here


The following would be a way to tackle the problem if the Wedge did not have a width argument. Since the pie chart is centered at (0,0), copying the outer path coordinates, reverting them and multiplying by some number smaller 1 (called r for radius in below code), gives the coordinates of the inner ring. Joining those two list of coordinates and taking care of the proper path codes allows to create a ring shape as desired.

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

enter image description here

Sottish answered 19/8, 2017 at 16:16 Comment(0)
B
0

The problem is that you didnt really make a real donut chart. With this part of the code

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

you drew a circle in the middle of a pie chart. The problem is if you make this circle transparent you will once again see the middle of the pie chart. I recommend using a free photo editing program like pixlr to just make it transparent. Unless you can find a way to make a true donut chart which I unfortunantly do not know how to do it.

Bernardabernardi answered 19/8, 2017 at 15:36 Comment(0)
T
0

Similarly to the Double donut chart in matplotlib solution referenced by importanceofbeingearnest, you will need to use plt.setp(pie, width=width) to set the width of your pie chart, which will make it a true donut instead of a pie chart with a solid circle drawn on top.

import matplotlib.pyplot as plt
fig1, ax1 = plt.subplots()
ax1.axis('equal')

# Set the width of the pie slices;
# this is equivalent to (1.0-0.75), or 
# (the radius of the pie chart - the radius of the inner circle)
width=0.25 

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

# ax1.pie will return three values:
# 1. pie (the dimensions of each wedge of the pie), 
# 2. labtext (the coordinates and text for the labels)
# 3. labpct (the coordinates and text of the "XX.X%"" labels)
pie, labtext, labpct = ax1.pie(x=sizes,
                labels=labels,
                colors=['green','red'],
                startangle=90,
                shadow=True,
                autopct='%1.1f%%'
                )
# apply "plt.setp" to set width property
plt.setp(pie, width=width)

# save the figure as transparent
fig1.savefig('foo.png', transparent=True)

a true donut

Tradein answered 27/2, 2020 at 20:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.