2

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

3
  • 1
    you need to make your two dataframes into a single dataframe, then you can use the hints on plotting that you've already found. thus, your question is really about join/merging dataframe. there are already lots of questions on SO re: merging dataframes in pandas Commented Sep 21, 2017 at 23:56
  • @PaulH Thanks. I was able to concatenate the two dataframes and able to create a single plot. My next challenge is to scale down my axis. Can you help me with how I can scale my y axis to show data between 1 to 1000. Commented Sep 22, 2017 at 0:47
  • There are probably dozens of questions on SO about setting axis limits for matplotlib plots. Look through a few of them, and the matplotlib+pandas docs and tell me what you find confusing. Commented Sep 22, 2017 at 0:55

1 Answer 1

3

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>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.