2017-10-21 2582 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

相關問題