Last active
February 4, 2020 01:09
-
-
Save jorgerance/1c9f16aba8b6dbe4a062e019fd29b792 to your computer and use it in GitHub Desktop.
[pandas - cool pie (donut) charts] #pandas #jupyter #plotting #piechart #python
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
def plot_piechart(data, column_numbers, column_index): | |
""" | |
Plots a preformated pie chart | |
- data: dataframe | |
- column_values: column name for quantitative values | |
- column_index: column name for qualitative values | |
""" | |
''' colormaps: https://matplotlib.org/examples/color/colormaps_reference.html | |
https://matplotlib.org/tutorials/colors/colormaps.html#kovesi-colormaps''' | |
colors = plt.cm.GnBu(np.linspace(0.6, 1, len(data))) | |
maxdata = max(data[column_numbers]) | |
mindata = min(data[column_numbers]) | |
explode = [] | |
for v in data[column_numbers]: | |
if v == maxdata: | |
explode.append(0.15) | |
elif v == mindata: | |
explode.append(0) | |
else: | |
explode.append(0) | |
plt.pie(data[column_numbers], counterclock=False, shadow=False, \ | |
autopct='%.1f', textprops={'color':"white"}, explode=explode) | |
circle=plt.Circle( (0,0), 0.3, color='white') | |
plt.gca().add_artist(circle) | |
plt.legend(labels=data[column_index],bbox_to_anchor=(1,0.5), \ | |
loc="center right", fontsize=10, | |
bbox_transform=plt.gcf().transFigure) | |
plt.rcParams['axes.prop_cycle'] = cycler(color=colors) | |
return(plt.show()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment