我有两个 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/