gpt4 book ai didi

python - 如何解决生成饼图时出现Python错误: ValueError: 'explode' must be of length 'x'

转载 作者:行者123 更新时间:2023-12-01 02:30:34 24 4
gpt4 key购买 nike

任何人都可以帮助我解决使用爆炸选项生成饼图时出现的以下错误。ValueError:“爆炸”的长度必须为“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()

附加信息:我使用的是 anaconda 3.6 版本。我能够在不爆炸的情况下生成饼图,但是当我使用爆炸时,我收到错误 - ValueError: 'explode' must be of length 'x'.

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

最佳答案

主要是阅读 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。

关于python - 如何解决生成饼图时出现Python错误: ValueError: 'explode' must be of length 'x' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46864696/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com