gpt4 book ai didi

python - 在 Python 3.3.2 上使用 Pandas 0.12.0 和 Matplotlib 1.3.1 绘制包含 NaN 的 DataFrame 时出错

转载 作者:太空狗 更新时间:2023-10-30 00:07:22 25 4
gpt4 key购买 nike

首先,这个问题与this one相同.

我遇到的问题是,当我尝试在一个单元格中绘制一个包含 numpy NaN 的 DataFrame 时,出现错误:

C:\>\Python33x86\python.exe
Python 3.3.2 (v3.3.2:d047928ae3f6, May 16 2013, 00:03:43) [MSC v.1600 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import pandas as pd
>>> import numpy as np
>>> import matplotlib.pyplot as plt
>>>
>>> dates = pd.date_range('20131201', periods=5, freq='H')
>>> data = [[1, 2], [4, 5], [9, np.nan], [16, 17], [25, 26]]
>>> df = pd.DataFrame(data, index=dates,
... columns=list('AB'))
>>>
>>> print(df.to_string())
A B
2013-12-01 00:00:00 1 2
2013-12-01 01:00:00 4 5
2013-12-01 02:00:00 9 NaN
2013-12-01 03:00:00 16 17
2013-12-01 04:00:00 25 26
>>> df.plot()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python33x86\lib\site-packages\pandas\tools\plotting.py", line 1636, in plot_frame
plot_obj.generate()
File "C:\Python33x86\lib\site-packages\pandas\tools\plotting.py", line 856, in generate
self._make_plot()
File "C:\Python33x86\lib\site-packages\pandas\tools\plotting.py", line 1240, in _make_plot
self._make_ts_plot(data, **self.kwds)
File "C:\Python33x86\lib\site-packages\pandas\tools\plotting.py", line 1321, in _make_ts_plot
_plot(data[col], i, ax, label, style, **kwds)
File "C:\Python33x86\lib\site-packages\pandas\tools\plotting.py", line 1295, in _plot
style=style, **kwds)
File "C:\Python33x86\lib\site-packages\pandas\tseries\plotting.py", line 77, in tsplot
lines = plotf(ax, *args, **kwargs)
File "C:\Python33x86\lib\site-packages\matplotlib\axes.py", line 4139, in plot
for line in self._get_lines(*args, **kwargs):
File "C:\Python33x86\lib\site-packages\matplotlib\axes.py", line 319, in _grab_next_args
for seg in self._plot_args(remaining, kwargs):
File "C:\Python33x86\lib\site-packages\matplotlib\axes.py", line 297, in _plot_args
x, y = self._xy_from_xy(x, y)
File "C:\Python33x86\lib\site-packages\matplotlib\axes.py", line 216, in _xy_from_xy
by = self.axes.yaxis.update_units(y)
File "C:\Python33x86\lib\site-packages\matplotlib\axis.py", line 1337, in update_units
converter = munits.registry.get_converter(data)
File "C:\Python33x86\lib\site-packages\matplotlib\units.py", line 137, in get_converter
xravel = x.ravel()
File "C:\Python33x86\lib\site-packages\numpy\ma\core.py", line 3969, in ravel
r._mask = ndarray.ravel(self._mask).reshape(r.shape)
File "C:\Python33x86\lib\site-packages\pandas\core\series.py", line 981, in reshape
return ndarray.reshape(self, newshape, order)
TypeError: an integer is required

如果我将 np.NaN 替换为数字(例如“2.3”),则上述代码有效。

绘制为两个单独的 Series 也不起作用(当我将包含 NaN 的 Series 添加到图中时失败):

C:\>\Python33x86\python.exe
Python 3.3.2 (v3.3.2:d047928ae3f6, May 16 2013, 00:03:43) [MSC v.1600 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import pandas as pd
>>> import numpy as np
>>> import matplotlib.pyplot as plt
>>>
>>> dates = pd.date_range('20131201', periods=5, freq='H')
>>> data = [[1, 2], [4, 5], [9, np.nan], [16, 17], [25, 26]]
>>> df = pd.DataFrame(data, index=dates,
... columns=list('AB'))
>>>
>>> print(df.to_string())
A B
2013-12-01 00:00:00 1 2
2013-12-01 01:00:00 4 5
2013-12-01 02:00:00 9 NaN
2013-12-01 03:00:00 16 17
2013-12-01 04:00:00 25 26
>>> df['A'].plot(label='This is A', style='k')
<matplotlib.axes.AxesSubplot object at 0x02ACFF90>
>>> df['B'].plot(label='This is B', style='g')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python33x86\lib\site-packages\pandas\tools\plotting.py", line 1730, in plot_series
plot_obj.generate()
File "C:\Python33x86\lib\site-packages\pandas\tools\plotting.py", line 856, in generate
self._make_plot()
File "C:\Python33x86\lib\site-packages\pandas\tools\plotting.py", line 1240, in _make_plot
self._make_ts_plot(data, **self.kwds)
File "C:\Python33x86\lib\site-packages\pandas\tools\plotting.py", line 1311, in _make_ts_plot
_plot(data, 0, ax, label, self.style, **kwds)
File "C:\Python33x86\lib\site-packages\pandas\tools\plotting.py", line 1295, in _plot
style=style, **kwds)
File "C:\Python33x86\lib\site-packages\pandas\tseries\plotting.py", line 77, in tsplot
lines = plotf(ax, *args, **kwargs)
File "C:\Python33x86\lib\site-packages\matplotlib\axes.py", line 4139, in plot
for line in self._get_lines(*args, **kwargs):
File "C:\Python33x86\lib\site-packages\matplotlib\axes.py", line 319, in _grab_next_args
for seg in self._plot_args(remaining, kwargs):
File "C:\Python33x86\lib\site-packages\matplotlib\axes.py", line 297, in _plot_args
x, y = self._xy_from_xy(x, y)
File "C:\Python33x86\lib\site-packages\matplotlib\axes.py", line 216, in _xy_from_xy
by = self.axes.yaxis.update_units(y)
File "C:\Python33x86\lib\site-packages\matplotlib\axis.py", line 1337, in update_units
converter = munits.registry.get_converter(data)
File "C:\Python33x86\lib\site-packages\matplotlib\units.py", line 137, in get_converter
xravel = x.ravel()
File "C:\Python33x86\lib\site-packages\numpy\ma\core.py", line 3969, in ravel
r._mask = ndarray.ravel(self._mask).reshape(r.shape)
File "C:\Python33x86\lib\site-packages\pandas\core\series.py", line 981, in reshape
return ndarray.reshape(self, newshape, order)
TypeError: an integer is required

但是,如果我直接使用 Matplotlib 的 Pyplot plot() 而不是使用 Pandas 的 plot() 函数来执行此操作,它会起作用:

C:\>\Python33x86\python.exe
Python 3.3.2 (v3.3.2:d047928ae3f6, May 16 2013, 00:03:43) [MSC v.1600 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import pandas as pd
>>> import numpy as np
>>> import matplotlib.pyplot as plt
>>> dates = pd.date_range('20131201', periods=5, freq='H')
>>> plt.plot(dates, [1, 4, 9, 16, 25], 'k', dates, [2, 5, np.NAN, 17, 26], 'g')
[<matplotlib.lines.Line2D object at 0x03E98650>, <matplotlib.lines.Line2D object at 0x040929B0>]
>>> plt.show()
>>>

所以我似乎有一个解决方法,但是当我绘制大型 DataFrame 时,我更愿意使用 Pandas 的 plot() 方法,这更方便。我试着跟踪堆栈跟踪,但过了一会儿它变得复杂了(我不熟悉 Pandas、Numpy 和 Matplotlib 源代码)。我做错了什么,还是 Pandas 的 plot() 中可能存在错误?

感谢您的帮助!

我在 Windows x86 和 Linux AMD64 上都试过,这些版本的结果相同:

  • python 3.3.2
  • Pandas 0.12.0
  • Matplotlib 1.3.1
  • NumPy 1.7.1

最佳答案

这似乎是 matplotlib 1.3.1 和 pandas 0.12 integration bug :

The workaround is to downgrade to matplotlib 1.3.0. (Note, however, that this version of matplotlib contains a bug on systems which have fonts with non-ASCII font names, so you may need to pick your problem!). This downgrade will trigger a downgrade to numpy 1.7.1, so you should then (again) upgrade to numpy 1.8.0. This error should be fixed in the upcoming Pandas 0.13. However Pandas 0.13 may break some existing code (because pandas.Series is no longer a subclass of numpy.ndarray), so again, some hard choices may be required, at least in the short term.

刚刚检查过,代码在 matplotlib 1.3.0 上工作正常:

>>> import matplotlib
>>> matplotlib.__version__
'1.3.0'
>>> df.plot()
<matplotlib.axes.AxesSubplot object at 0x04E8B4F0>
>>> plt.show(_)

enter image description here

关于python - 在 Python 3.3.2 上使用 Pandas 0.12.0 和 Matplotlib 1.3.1 绘制包含 NaN 的 DataFrame 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20564536/

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