Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pandas pie chart plot remove the label text on the wedge

Tags:

the pie chart example on pandas plotting tutorial http://pandas.pydata.org/pandas-docs/version/0.15.0/visualization.html generates the following figure:

enter image description here

with this code:

import matplotlib.pyplot as plt plt.style.use('ggplot') import numpy as np np.random.seed(123456)   import pandas as pd df = pd.DataFrame(3 * np.random.rand(4, 2), index=['a', 'b', 'c', 'd'], columns=['x', 'y'])  f, axes = plt.subplots(1,2, figsize=(10,5)) for ax, col in zip(axes, df.columns):     df[col].plot(kind='pie', autopct='%.2f', labels=df.index,  ax=ax, title=col, fontsize=10)     ax.legend(loc=3)  plt.show() 

I want to remove the text label (a,b,c,d) from both subplots, because for my application those label are long, so I only want to show them in legend.

After read this: How to add a legend to matplotlib pie chart?, I figure out an way with matplotlib.pyplot.pie but the figure is not as fancy even if i am still using ggplot.

f, axes = plt.subplots(1,2, figsize=(10,5)) for ax, col in zip(axes, df.columns):     patches, text, _ = ax.pie(df[col].values, autopct='%.2f')     ax.legend(patches, labels=df.index, loc='best') 

enter image description here

My question is, is there a way that can combine the things I want from both side? to be clear, I want the fanciness from pandas, but remove the text from the wedges.

Thank you

like image 345
fast tooth Avatar asked May 05 '15 17:05

fast tooth


People also ask

How do I remove the Y label from a pie chart in Python?

MatPlotLib with Python Plot a pie chart using pie() method. To hide the label on the left side in matplotlib, we can use plt. ylabel("") with ablank string.

How do you avoid overlapping of labels and Autopct in a Matplotlib pie chart?

Use legend() method to avoid overlapping of labels and autopct. To display the figure, use show() method.

How do you add a title to a pie chart in Python?

To add a title we use the title() method of the matplotlib pyplot module. Here we define data coordinates and labels. Next, we use the pie() function to create a pie chart. To add a title to the pie chart, use the title() function.


1 Answers

You can turn off the labels in the chart, and then define them within the call to legend:

df[col].plot(kind='pie', autopct='%.2f', labels=['','','',''],  ax=ax, title=col, fontsize=10) ax.legend(loc=3, labels=df.index) 

or

... labels=None ... 

enter image description here

like image 115
iayork Avatar answered Nov 03 '22 02:11

iayork