【问题标题】:matplotlib fill_between raises errors of "ufunc 'isfinite' not supported for the input types..."?matplotlib fill_between 引发“输入类型不支持 ufunc 'isfinite'...”的错误?
【发布时间】:2020-07-26 10:16:19
【问题描述】:

我想使用 matplotlib 填补绘图中两行之间的空白,但它总是显示如下错误消息。我查过有类似的问题posted in the forum,但是他们的解决方案并没有解决这个问题。

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''

【问题讨论】:

  • 我遇到了同样的问题。原来我的 x 是一个列表,并且边界(在你的情况下为 W_low 和 W_high)是 ndarrays。将所有内容都转换为列表并且有效。

标签: python matplotlib


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2020-02-16
    • 2022-12-17
    • 2020-09-29
    • 2019-11-04
    • 2018-01-08
    • 2020-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多