Skip to content Skip to sidebar Skip to footer

Change Color Of Pie Chart According To Section Label (pandas/matplotlib)

I am working from a DataFrame called plot_df that looks like this: Country Visual Format $ 0 France DEFAULT

Solution 1:

You can use the colors parameter for pie charts. Since this takes an array, you'll have to create an array that corresponds to your input data for each plot.

cdict = {'DIGITAL': 'r', 'DIGIMAX3D': 'y', 'DIGITAL3D': 'b', ...}

for country in plot_df.index.get_level_values(0).unique():
    ax = axes_list.pop(0)
    df = plot_df.loc[(country, slice(None))]
    colors = [cdict[x] for x in df.index]  % colors based on index of input data
    df.plot(kind='pie', colors=colors, subplots=True, legend=False, autopct='%1.1f%%', ax=ax)

Solution 2:

Another thing you could do is to reshape the data and plot with pandas:

(df['$'].unstack(0,fill_value=0)
 .plot.pie(subplots=True, layout=(4,4), figsize=(12,12))
);

Output:

enter image description here

Post a Comment for "Change Color Of Pie Chart According To Section Label (pandas/matplotlib)"