【发布时间】: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