YouTip LogoYouTip

Matplotlib Ref Pie

* * * [![Image 1: Matplotlib Reference Documentation](https://example.com/images/up.gif) Matplotlib Reference Documentation](https://example.com/matplotlib/matplotlib-apiref.html) `pie()` is used to draw a pie chart, showing the proportion of each part in the whole. Pie charts are suitable for showing the proportion of a small number of categories (usually 5-8 or fewer). ## Function Definition ### pyplot Interface matplotlib.pyplot.pie(x, explode=None, labels=None, colors=None, autopct=None, pctdistance=0.6, shadow=False, labeldistance=1.1, startangle=0, radius=1, counterclock=True, wedgeprops=None, textprops=None, center=(0, 0), frame=False, rotatelabels=False, *, normalize=True, hatch=None, **kwargs) ### Axes Interface Axes.pie(x, explode=None, labels=None, colors=None, autopct=None, pctdistance=0.6, shadow=False, labeldistance=1.1, startangle=0, radius=1, counterclock=True, wedgeprops=None, textprops=None, center=(0, 0), frame=False, rotatelabels=False, *, normalize=True, hatch=None, **kwargs) ## Parameter Description | Parameter | Type | Description | | --- | --- | --- | | x | 1D array-like | Value for each slice (required), automatically normalized | | explode | array-like | Distance of each slice from the center, 0 means no offset | | labels | list | Label text for each slice | | colors | list | Color for each slice | | autopct | str or callable | Auto display percentage, e.g., '%1.1f%%' means one decimal place; None means no display | | pctdistance | float | Distance ratio of percentage text from center, default 0.6 | | shadow | bool | Whether to add shadow effect | | labeldistance | float | Distance ratio of label from center, default 1.1 | | startangle | float | Starting angle of the first slice (counterclockwise), 0 means starting from 3 o'clock direction | | radius | float | Pie chart radius, default 1 | | counterclock | bool | Slice direction: True counterclockwise (default), False clockwise | | wedgeprops | dict | Parameters passed to each slice (Wedge), e.g., {'edgecolor':'white', 'linewidth':1} | | textprops | dict | Parameters passed to text, e.g., {'fontsize':12, 'color':'gray'} | | center | tuple | Pie chart center coordinates, default (0, 0) | | normalize | bool | Whether to normalize x to make the sum 1, default True | > pie() returns three lists: `(patches, texts, autotexts)`. patches are the slice objects, texts are the label texts, and autotexts are the percentage texts. * * * ## Usage Examples ### Example 1: Basic Pie Chart ## Instance import matplotlib.pyplot as plt # Data sizes =[30,25,20,15,10] labels =['Python','Java','JavaScript','C++','Go'] colors =['#3498db','#e74c3c','#f39c12','#2ecc71','#9b59b6'] fig, ax = plt.subplots(layout='constrained') # Draw pie chart ax.pie(sizes, labels=labels, colors=colors, autopct='%1.1f%%',# Display percentage, keep one decimal place startangle=90,# Start from 12 o'clock direction wedgeprops={'edgecolor': 'white','linewidth': 1}) ax.set_title('Programming Language Usage') plt.show() ### Example 2: Highlight Specific Slice (explode) ## Instance import matplotlib.pyplot as plt sizes =[35,25,20,12,8] labels =['Search','Social','Direct','Email','Referral'] explode =(0.1,0,0,0,0)# Highlight the first slice (Search) by 0.1 fig, ax = plt.subplots(layout='constrained') wedges, texts, autotexts = ax.pie(
← Matplotlib Ref SavefigMatplotlib Ref Layout Config β†’