堆栈图“ TypeError:输入类型不支持ufunc'isfinite'”

时间:2019-10-22 13:21:19

标签: python python-3.x matplotlib typeerror

尝试使用以下代码堆栈绘制两个数组。我试图简化问题以找出原因,但问题仍然存在。

diet_projection = perc_contributions_WRAP.percentage_contributions(diet_props, 2050, 1961, 2013)

print(diet_projection.iloc[:].values)
print(np.arange(2013, 2051, 1))

plt.stackplot(np.arange(2013, 2051, 1), diet_projection.iloc[:].values)
# plt.legend(loc = "upper left")
# plt.ylabel("Diet makeup - kcal/capita/day")
# plt.xlabel("Year")
# plt.xticks(np.arange(2010, 2050, 5))
plt.show()

这将返回以下内容:

[[0.7772766317126378 0.7760459815112983 0.7748153313099588
  0.7735846811086193 0.7723540309072798 0.7711233807059403
  0.7698927305046008 0.7686620803032613 0.7674314301019218
  0.7662007799005823 0.7649701296992428 0.7637394794979033
  0.7625088292965638 0.7612781790952243 0.7600475288938848
  0.7588168786925453 0.7575862284912058 0.7563555782898663
  0.7551249280885268 0.7538942778871873 0.7526636276858478
  0.7514329774845083 0.7502023272831688 0.7489716770818293
  0.7477410268804898 0.7465103766791503 0.7452797264778108
  0.7440490762764713 0.7428184260751318 0.7415877758737923
  0.7403571256724528 0.7391264754711133 0.7378958252697738
  0.7366651750684343 0.7354345248670948 0.7342038746657553
  0.7329732244644158 0.7317425742630763]
 [0.21688670893743733 0.21809486726005778 0.21930302558267778
  0.22051118390529822 0.22171934222791823 0.22292750055053867
  0.22413565887315912 0.22534381719577912 0.22655197551839956
  0.22776013384101956 0.22896829216364 0.23017645048626
  0.23138460880888045 0.2325927671315009 0.2338009254541209
  0.23500908377674135 0.23621724209936135 0.2374254004219818
  0.2386335587446018 0.23984171706722224 0.24104987538984224
  0.24225803371246268 0.24346619203508313 0.24467435035770313
  0.24588250868032357 0.24709066700294358 0.24829882532556402
  0.24950698364818402 0.25071514197080447 0.2519233002934249
  0.2531314586160449 0.25433961693866536 0.25554777526128536
  0.2567559335839058 0.2579640919065258 0.25917225022914625
  0.2603804085517667 0.2615885668743867]
 [0.005836659349924521 0.0058591512286437705 0.00588164310736302
  0.005904134986082277 0.005926626864801526 0.005949118743520783
  0.0059716106222400325 0.005994102500959282 0.006016594379678539
  0.006039086258397788 0.006061578137117045 0.0060840700158362945
  0.006106561894555551 0.006129053773274801 0.00615154565199405
  0.006174037530713307 0.0061965294094325565 0.006219021288151813
  0.006241513166871063 0.006264005045590312 0.006286496924309569
  0.0063089888030288185 0.006331480681748075 0.006353972560467325
  0.006376464439186574 0.006398956317905831 0.0064214481966250805
  0.006443940075344337 0.006466431954063587 0.006488923832782843
  0.006511415711502093 0.0065339075902213425 0.006556399468940599
  0.006578891347659849 0.006601383226379105 0.006623875105098355
  0.0066463669838176045 0.006668858862536861]]
[2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026
 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040
 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050]
Traceback (most recent call last):
  File "data_sort.py", line 130, in <module>
    analysis_temp()
  File "data_sort.py", line 129, in analysis_temp
    SA()
  File "data_sort.py", line 122, in SA
    plt.stackplot(np.arange(2013, 2051, 1), diet_projection.iloc[:].values)
  File "C:\Users\Thomas Ball\AppData\Local\Programs\Python\Python37-32\lib\site-packages\matplotlib\pyplot.py", line 2915, in stackplot
    **kwargs)
  File "C:\Users\Thomas Ball\AppData\Local\Programs\Python\Python37-32\lib\site-packages\matplotlib\__init__.py", line 1810, in inner
    return func(ax, *args, **kwargs)
  File "C:\Users\Thomas Ball\AppData\Local\Programs\Python\Python37-32\lib\site-packages\matplotlib\axes\_axes.py", line 4858, in stackplot
    return mstack.stackplot(self, x, *args, **kwargs)
  File "C:\Users\Thomas Ball\AppData\Local\Programs\Python\Python37-32\lib\site-packages\matplotlib\stackplot.py", line 110, in stackplot
    **kwargs)
  File "C:\Users\Thomas Ball\AppData\Local\Programs\Python\Python37-32\lib\site-packages\matplotlib\__init__.py", line 1810, in inner
    return func(ax, *args, **kwargs)
  File "C:\Users\Thomas Ball\AppData\Local\Programs\Python\Python37-32\lib\site-packages\matplotlib\axes\_axes.py", line 5049, in fill_between
    y2 = ma.masked_invalid(self.convert_yunits(y2))
  File "C:\Users\Thomas Ball\AppData\Local\Programs\Python\Python37-32\lib\site-packages\numpy\ma\core.py", line 2366, in masked_invalid
    condition = ~(np.isfinite(a))
TypeError: ufunc 'isfinite' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''

我看不到是什么原因导致了错误。所有数字均在正常范围内

我尝试了不带.values的情况,但是它也会返回相同的错误(通常对于.stackplot来说是正常的)。

1 个答案:

答案 0 :(得分:0)

通过在行foo()中添加.astype(float)来解决,该行是原始生成数据的地方。但是,不太确定它是如何首先丢失了float或int64类型的。