I am new to matplotlib pie charts and I want to know how we can set the radius of a single sector in pie chart. I know about how to set the overall radius of the chart, but want to increase the radius of a single sector. For example, in this picture, sector '1' is with grater radius as compared to others. Please note that: I don't want to explode that sector for increasing radius.
Please tell me if someone knows! Thanks in advance.
The direct answer is that it doesn't appear to be possible with Matplotlib to set multiple radii within the same pie plot.
#! /usr/bin/env python
# -*- coding: utf-8 -*-
import matplotlib.pyplot as plt
data = [4, 3, 2, 1]
fig, ax = plt.subplots(figsize=(4,4))
wedges, texts = ax.pie(data, radius=1)
for w in wedges:
w.set_width(.5)
wedges[0].set_radius(1.1)
wedges[1].set_radius(1)
wedges[2].set_radius(1)
wedges[3].set_radius(1)
plt.show()
radii = [w.radius for w in wedges]
print(radii)
We can see that the radii are, in fact, different.
[1.1, 1, 1, 1]
An interesting side note is that slice widths can be different:
However, you can work around this with a little bit of sleight of hand. Instead of plotting one pie plot, you plot two--the first with the "enlarged" slice invisible and the second plot with the "enlarged" slice set as the only visible slice.
#! /usr/bin/env python
# -*- coding: utf-8 -*-
import matplotlib.pyplot as plt
data = [4, 3, 2, 1]
fig, ax = plt.subplots(figsize=(4,4))
# Pie 1
wedges, texts = ax.pie(data, radius=1)
for w in wedges:
w.set_width(.5)
wedges[0].set_visible(False)
# Pie 2
wedges1, texts1 = ax.pie(data, radius=1.1)
for w in wedges1:
w.set_width(.6)
wedges1[1].set_visible(False)
wedges1[2].set_visible(False)
wedges1[3].set_visible(False)
plt.show()
The set_width
arguments are added to make the plot a "donut" plot like your example. Note the width values in each chart that are required to achieve the desired look.