ENH: support pie plot in series and dataframe plot by sinhrks · Pull Request #6976 · pandas-dev/pandas (original) (raw)

Related to #413, added pie plot for Series.plot and DataFrame.plot kind.

If data includes NaN, it will be automatically filled by 0. If data contains negative value, ValueError will be raised.

import pandas as pd
import numpy as np
series = pd.Series(3 * np.random.rand(4), index=['a', 'b', 'c', 'd'], name='series')
series.plot(kind='pie')

series_pie_plot

Plotting with DataFrame

Pie plot with DataFrame requires either to specify target column by y argument or subplots=True. When y is specified, pie plot of selected column will be drawn. If subplots=True is specified, pie plots for each columns are drawn as subplots. Legend will be drawn in each pie plots by default, specify legend=False to hide it.

df = pd.DataFrame(3 * np.random.rand(4, 2), index=['a', 'b', 'c', 'd'], columns=['x', 'y'])
df.plot(kind='pie', subplots=True) 

df_pie_plot

Plotting with Options

You can use labels and colors keywords to specify labels and colors of each wedges (Cannot use label and color, because of matplotlib's specification). If you want to hide wedge labels, specify labels=None. If fontsize is specified, the value will be applied to wedge labels. Also, other keywords supported by matplotlib.pyplot.pie can be used.

series.plot(kind='pie', labels=['AA', 'BB', 'CC', 'DD'], colors=['r', 'g', 'b', 'c'],
               autopct='%.2f', fontsize=20)

series_pie_plot_options