Research breakthrough possible @S-Logix pro@slogix.in

Office Address

Social List

How to Plot Exploded Pie Chart Using Matplotlib

Exploded Pie Chart Using Matplotlib

Condition for Plotting Exploded Pie Chart Using Matplotlib in Python

  • Description:
    An exploded pie chart is a variation of the standard pie chart, where one or more slices of the pie are "exploded" or pulled away from the center to emphasize those slices. This type of chart is useful when you want to highlight a particular slice of the pie for better visibility or comparison.
Step-by-Step Process
  • Import Libraries:
    You will need to import matplotlib.pyplot for plotting the chart.
  • Prepare Data:
    Prepare the dataset that you want to visualize, typically categorical data with corresponding values.
  • Create Pie Chart:
    Use the pie() function from Matplotlib to create the pie chart.
  • Explode Slices:
    Specify which slices you want to "explode" by using the explode parameter in the pie() function. The explode parameter is a sequence where each element corresponds to a slice of the pie. A value of 0 means the slice is not exploded, and a value greater than 0 (e.g., 0.1) will "explode" the slice.
  • Customize Plot:
    Add labels, a title, and adjust the appearance of the chart.
Sample Source Code
  • # Code for Explode Pie-Chart

    import matplotlib.pyplot as plt

    labels = ['Product A', 'Product B', 'Product C', 'Product D']
    sizes = [25, 35, 20, 20]

    # Explode the slices
    explode = (0.1, 0.1, 0.1, 0.1)

    plt.pie(sizes, explode=explode, labels=labels, autopct='%1.1f%%', startangle=140,
    colors=['#ff9999','#66b3ff','#99ff99','#ffcc99'])

    plt.title('Product Sales Distribution (Exploded Pie Chart)')

    plt.show()
Screenshots
  • Exploded Pie Chart Output