Created
March 27, 2013 19:08
-
-
Save vals/5257113 to your computer and use it in GitHub Desktop.
Making nicer looking pie charts with matplotlib
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from random import shuffle | |
slices = [1,2,3] * 4 + [20, 25, 30] * 2 | |
shuffle(slices) | |
fig = plt.figure(figsize=[10, 10]) | |
ax = fig.add_subplot(111) | |
cmap = plt.cm.prism | |
colors = cmap(np.linspace(0., 1., len(slices))) | |
labels = ["Some text"] * len(slices) | |
ax.pie(slices, colors=colors, labels=labels, labeldistance=1.05) | |
ax.set_title("Figure 1"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fig = plt.figure(figsize=[10, 10]) | |
ax = fig.add_subplot(111) | |
pie_wedge_collection = ax.pie(slices, colors=colors, labels=labels, labeldistance=1.05); | |
for pie_wedge in pie_wedge_collection[0]: | |
pie_wedge.set_edgecolor('white') | |
ax.set_title("Figure 2"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
slices = sorted(slices) | |
fig = plt.figure(figsize=[10, 10]) | |
ax = fig.add_subplot(111) | |
pie_wedge_collection = ax.pie(slices, colors=colors, labels=labels, labeldistance=1.05); | |
for pie_wedge in pie_wedge_collection[0]: | |
pie_wedge.set_edgecolor('white') | |
ax.set_title("Figure 3"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
large = slices[:len(slices) / 2] | |
small = slices[len(slices) / 2:] | |
reordered = large[::2] + small[::2] + large[1::2] + small[1::2] | |
fig = plt.figure(figsize=[10, 10]) | |
ax = fig.add_subplot(111) | |
pie_wedge_collection = ax.pie(reordered, colors=colors, labels=labels, labeldistance=1.05); | |
for pie_wedge in pie_wedge_collection[0]: | |
pie_wedge.set_edgecolor('white') | |
ax.set_title("Figure 4"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fig = plt.figure(figsize=[10, 10]) | |
ax = fig.add_subplot(111) | |
angle = 180 + float(sum(small[::2])) / sum(reordered) * 360 | |
pie_wedge_collection = ax.pie(reordered, colors=colors, labels=labels, labeldistance=1.05, startangle=angle); | |
for pie_wedge in pie_wedge_collection[0]: | |
pie_wedge.set_edgecolor('white') | |
ax.set_title("Figure 5"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I was looking for some good colors, but no luck here...