Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to resolve Python error while generating Pie chart : ValueError: 'explode' must be of length 'x'

Can anyone help me to resolve below error while generating Pie chart with Explode option. ValueError: 'explode' must be of length 'x'

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import datetime as dt

figureObject, axesObject = plt.subplots()
labels = "ABC", "XYZ"
delay = [delay1, delay2]
colors  = ("red", "green", "orange", "cyan", "brown", 
"grey","blue","indigo", "beige", "yellow")
explode = (0, 0.1, 0, 0)

# Draw the pie chart
axesObject.pie(delay,
    explode=explode,
    labels=labels,
    colors=colors,
    shadow=True,
    autopct='%1.2f',
    startangle=90,
    wedgeprops = { 'linewidth' : 2, 'edgecolor' : "cyan" })

  plt.legend(patches, labels, loc="best") 

 # Aspect ratio - equal means pie is a circle
  axesObject.axis('equal')
  plt.show()

additional information: I am using anaconda 3.6 version. I am able to generate pie chart without explode, but when I use explode I am getting an error - ValueError: 'explode' must be of length 'x'.

Please help me, how to overcome this issue.

like image 737
Sekhar Avatar asked Jan 30 '23 09:01

Sekhar


1 Answers

Mostly it helps to read the documentation, which says

matplotlib.pyplot.pie(x, explode=None ,...)

x : array-like
The input array used to make the pie chart.

explode: array-like, optional, default: None
If not None, is a len(x) array which specifies the fraction of the radius with which to offset each wedge.

Hence if the input x has two elements, explode must also have two elements, ... and not 4 as in the code from the question.

like image 104
ImportanceOfBeingErnest Avatar answered Jan 31 '23 22:01

ImportanceOfBeingErnest