abhinav singh
abhinav singh

Reputation: 1104

Scatter plotting data from two different data frames in python

I have two different data frames in following format.

dfclean
Out[1]: 
      obj
0     682
1     101
2      33

dfmalicious
Out[2]: 
                obj
        0        17
        1        43
        2         8
        3         9
        4       211

My use-case is to plot a single scatter graph that distinctly shows the obj values from both the dataframes. I am using python for this purpose. I looked at a few examples where two columns of same dataframe were used to plot the data but couldnt replicate it for my use-case. Any help is greatly appreciated.

How to plot two DataFrame on same graph for comparison

Upvotes: 2

Views: 7366

Answers (1)

Zero
Zero

Reputation: 77027

To plot multiple column groups in a single axes, repeat plot method specifying target ax

Option 1]

In [2391]: ax = dfclean.reset_index().plot(kind='scatter', x='index', y='obj',
                                           color='Red', label='G1')

In [2392]: dfmalicious.reset_index().plot(kind='scatter', x='index', y='obj',
                                          color='Blue', label='G2', ax=ax)
Out[2392]: <matplotlib.axes._subplots.AxesSubplot at 0x2284e7b8>

enter image description here

Option 2]

In [2399]: dff = dfmalicious.merge(dfclean, right_index=True, left_index=True,
                                   how='outer').reset_index()

In [2406]: dff
Out[2406]:
   index  obj_x  obj_y
0      0     17  682.0
1      1     43  101.0
2      2      8   33.0
3      3      9    NaN
4      4    211    NaN

In [2400]: ax = dff.plot(kind='scatter', x='index', y='obj_x', color='Red', label='G1')

In [2401]: dff.plot(kind='scatter', x='index', y='obj_y', color='Blue', label='G2', ax=ax)
Out[2401]: <matplotlib.axes._subplots.AxesSubplot at 0x11dbe1d0>

Upvotes: 3

Related Questions