Pandas graph graphic chart removes text labels on a wedge

An example of a pie chart in the pandas graphic tutorial http://pandas.pydata.org/pandas-docs/version/0.15.0/visualization.html generates the following image:

enter image description here

using 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 subheadings, because for my application these labels are long, so I want to show them only in the legend.

After reading: How to add legend to matplotlib pie chart? , I figure out a way with matplotlib.pyplot.pie , but this figure is not so fantastic, even if I still use 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 to combine the things I want on both sides? to be clear, I want attachment from pandas, but delete the text from the wedges.

thanks

+7
matplotlib pandas pie-chart legend
source share
1 answer

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

 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

+20
source share

All Articles