Home > database >  Matplotlib - chart - ValueError: 'label' must be of length 'x'
Matplotlib - chart - ValueError: 'label' must be of length 'x'

Time:11-09

plt.figure(figsize = (12,8))
plt.pie(df['Rating'].value_counts().sort_index(),
       labels=df['Rating'].value_counts().sort_index().index,
       explode = [0.00,0.0,0.0,0.0,0.05],
       autopct= '%.2f%%',
        colors = ["#ff4545", "#ffa534",'#ffe234','#b7dd29','#57e32c'],
       shadow= True,
       startangle= 190,
       textprops = {'size':'small',
                   'fontweight':'bold',
                    'rotation':'0',
                   'color':'black'})
plt.legend(loc= 'upper right')
plt.title("Most Ratings Distribution Pie Chart", fontsize = 18, fontweight = 'bold')
plt.show()

This is the error that I am getting:

File C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\axes\_axes.py:3062, in Axes.pie(self, x, explode, labels, colors, autopct, pctdistance, shadow, labeldistance, startangle, radius, counterclock, wedgeprops, textprops, center, frame, rotatelabels, normalize)
   3060     raise ValueError("'label' must be of length 'x'")
   3061 if len(x) != len(explode):
-> 3062     raise ValueError("'explode' must be of length 'x'")
   3063 if colors is None:
   3064     get_next_color = self._get_patches_for_fill.get_next_color

ValueError: 'explode' must be of length 'x'

I want to make a pie chart using matplotlib. How can I make my code work?

CodePudding user response:

The pie documentation can help you.

Regarding the error, here is information about explode and x:

Parameters: x, 1D array-like The wedge sizes.

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

If you want for the code to work and make a simple pie chart, just use explode = None

  • Related