2017-10-21 2592 views
0

任何人都可以帮助我解决以下错误,同时生成具有爆炸选项的饼图选项。 ValueError异常: '爆炸' 必须是长的 'X'如何在生成饼图时解决Python错误:ValueError:'explode'必须长度为'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() 

附加信息:我用的蟒蛇3.6版本。 我能够生成没有爆炸的饼图,但是当我使用爆炸时,我得到一个错误 - ValueError:'爆炸'必须是长度'x'。

请帮助我,如何解决这个问题。

回答

0

主要是它可以帮助阅读the documentation,它说

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.

因此,如果输入x有两个要素,explode还必须具备两个要素, ...而不是4在从问题的代码。

+0

嗨,感谢您的支持。现在我明白了这个错误。现在我的饼图按预期正常工作。非常好。 :-) – Sekhar

相关问题