Search code examples
pythonmatplotlibpie-chart

python: pie chart font size


I'm plotting a pie chart using the following code:

fig1, ax1 = plt.subplots(figsize=(8,12))
ax1.pie(data,
    labels=label,
    radius=1,
    startangle=90,
    colors=cols,
    counterclock=False,
    shadow=False,
    wedgeprops={'edgecolor': 'white', 'linewidth': 1},
    textprops={'fontsize': 8},
    pctdistance=0.85,
    autopct='%1.1f%%')

plt.title('Title', fontsize=16)
plt.tight_layout()

When I change the font size in textprops both the font size of the labels and the percentages change.

What I would like to do is use different font sizes for the labels and the percentages.


Solution

  • I applied your code to the example in the official reference and added the code to change the font.

    import matplotlib.pyplot as plt
    
    # Pie chart, where the slices will be ordered and plotted counter-clockwise:
    l = 'Frogs', 'Hogs', 'Dogs', 'Logs'
    sizes = [15, 30, 45, 10]
    explode = (0, 0.1, 0, 0)  # only "explode" the 2nd slice (i.e. 'Hogs')
    cols = ['C0','C1','C2','C3']
    
    fig1, ax1 = plt.subplots(figsize=(8,12))
    wdges, labels, autopct = ax1.pie(sizes,
        labels=l,
        radius=1,
        startangle=90,
        colors=cols,
        counterclock=False,
        shadow=False,
        wedgeprops={'edgecolor': 'white', 'linewidth': 1},
        textprops={'fontsize': 8},
        pctdistance=0.85,
        autopct='%1.1f%%')
    
    plt.setp(labels, fontsize=15) #update
    plt.title('Title', fontsize=16)
    plt.tight_layout()
    

    enter image description here