Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas: Exception while plotting two data frames on the same graph

I have two Pandas DataFrame that I am trying to plot on the same graph.

  • all_data: need to plot this as a line graph
  • points_of_interest: need to plot this as a scatter plot in the same graph

Here is the code that I use to plot them:

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

When I run this code it says:

>>> 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

I have confirmed that both the dataframes are of type 'DataFrame'. What am I missing?

like image 550
Darth.Vader Avatar asked Nov 09 '22 07:11

Darth.Vader


1 Answers

you are trying to use the pd.Series points_of_interest[ASK_PRICE] with plot(kind='scatter'). You assumed it would naturally take the index vs the values. That is unfortunately not true.

Try this

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()
like image 59
piRSquared Avatar answered Nov 14 '22 21:11

piRSquared