繁体   English   中英

matplotlib fill_between 引发“输入类型不支持 ufunc 'isfinite' ”的错误?

[英]matplotlib fill_between raises errors of “ufunc 'isfinite' not supported for the input types…”?

我想使用 matplotlib 填补 plot 中两行之间的空白,但它总是显示如下错误消息。 我已经检查过论坛中发布了类似的问题,但他们的解决方案不能解决这个问题。

import numpy as np
import pandas as pd
import openpyxl, os
import matplotlib.pyplot as plt

#read and convert data from an excel file to two dimensional numpy.array
df = pd.read_excel("Water.xlsx").values

#x axis is year, from 1900 to 2017
year = np.arange(1900,2018,1)

#read data from df and convert them to one-dimensional numpy.array 
W_low = df[:,[29]].ravel()
W_high = df[:,[30]].ravel()

#plot the two lines and fill in the gap 
fig, ax1 = plt.subplots(1, 1, sharex=True)
#no errors are raised until this line
ax1.fill_between (year, W_low, W_high)

错误信息

  File "C:\Users\Christina\.spyder-py3\temp.py", line 24, in <module>
    ax1.fill_between (year, W_low, W_high)

  File "C:\Users\Christina\anaconda3\lib\site-packages\matplotlib\__init__.py", line 1599, in inner
    return func(ax, *map(sanitize_sequence, args), **kwargs)

  File "C:\Users\Christina\anaconda3\lib\site-packages\matplotlib\axes\_axes.py", line 5233, in fill_between
    y1 = ma.masked_invalid(self.convert_yunits(y1))

  File "C:\Users\Christina\anaconda3\lib\site-packages\numpy\ma\core.py", line 2377, 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''

我自己解决了这个问题。 看来我之前使用的数据集不够干净。 我将整个数据集复制粘贴到一个新的 excel 工作表中,然后运行相同的脚本,然后它就可以完美运行了。

希望我的解决方案能帮助遇到同样问题的其他人。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM