【问题标题】:Pandas: Exception while plotting two data frames on the same graphPandas:在同一图表上绘制两个数据框时出现异常
【发布时间】:2017-03-03 22:18:41
【问题描述】:

我有两个 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”类型。我错过了什么?

【问题讨论】:

    标签: python pandas numpy matplotlib plot


    【解决方案1】:

    您正在尝试将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()
    

    【讨论】:

      猜你喜欢
      • 2019-06-12
      • 1970-01-01
      • 2015-01-10
      • 1970-01-01
      • 2019-09-14
      • 1970-01-01
      • 1970-01-01
      • 2018-01-10
      • 2019-12-12
      相关资源
      最近更新 更多