python - Pandas :在同一张图上绘制两个数据框时出现异常

标签 python pandas numpy matplotlib plot

我有两个 Pandas DataFrame,我试图在同一张图上绘制。

  • all_data:需要将其绘制为折线图
  • points_of_interest:需要在同一张图中将其绘制为散点图

这是我用来绘制它们的代码:

axes = all_data[ASK_PRICE].plot(figsize=(16, 12))
points_of_interest[ASK_PRICE].plot(figsize=(16, 12), ax = axes, kind='scatter')
pylab.show()

当我运行这段代码时,它说:

>>> points_of_interest[ASK_PRICE].plot(figsize=(16, 12), ax = axes, kind='scatter')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/shubham/.local/lib/python2.7/site-packages/pandas/tools/plotting.py", line 3599, in __call__
**kwds)
  File "/home/shubham/.local/lib/python2.7/site-packages/pandas/tools/plotting.py", line 2673, in plot_series
**kwds)
  File "/home/shubham/.local/lib/python2.7/site-packages/pandas/tools/plotting.py", line 2430, in _plot
% kind)
ValueError: plot kind 'scatter' can only be used for data frames

我已经确认这两个数据帧都是“DataFrame”类型。我错过了什么?

最佳答案

您正在尝试将 pd.Series points_of_interest[ASK_PRICE]plot(kind='scatter') 一起使用。您假设它自然会采用索引与值。不幸的是,这不是真的。

试试这个

axes = all_data[ASK_PRICE].plot(figsize=(16, 12))
poi = points_of_interest[ASK_PRICE]
poi.reset_index().plot.scatter(0, 1, ax=axes)
pylab.show()

关于python - Pandas :在同一张图上绘制两个数据框时出现异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40179434/

相关文章:

带有 os.path.isdir() 的 Python os.listdir() 不返回所有目录

python - 以某些标准特别开始的阅读行

python - Pandas DataFrame - 将系列字符串拆分为多列

python - 如何让我的 python 程序运行得更快?

python - 从 threading.Thread 子类返回 wx.Panel

python - 使用 Pandas 分配方法拆分字符串并分配给两列

python - 将 Pandas 数据框转换为以第一列为键的字典

python - 在 MacOS 上安装 Spyder/Python - 非常令人沮丧

python - Debian 没有名为 numpy 的模块

python - 解释(和比较)numpy.correlate 的输出