1

I used the recipe in this answer to create a scatter plot with one column of my dataframe as the y and several other as different x's. The relevant code from that answer was:

ax1 = df.plot(kind='scatter', x='a', y='b', color='r')    
ax2 = df.plot(kind='scatter', x='c', y='d', color='g', ax=ax1)    
ax3 = df.plot(kind='scatter', x='e', y='f', color='b', ax=ax1)

Problem is, mine looks like this:

enter image description here

Is there something elegant and simple you can add to this plot command so that each x column's data will be scaled such that it occupies the whole axis? I don't care if none of the variables have ticks underneath anymore, just want to visually compare how the y changes for each of them.

cardamom
  • 6,873
  • 11
  • 48
  • 102
  • Can you look into this post https://stackoverflow.com/questions/34280444/python-scatter-plot-with-multiple-y-values-for-each-x – Akash Sep 02 '17 at 18:47
  • Thanks, am currently looking at [this](https://stackoverflow.com/questions/21764475/scaling-numbers-column-by-column-with-pandas-python) one, and think that I probably need to create a new dataframe with several columns rescaled rather than trying to get the plot command to do it.. – cardamom Sep 02 '17 at 18:50

1 Answers1

2

IIUC, you can use twiny:

df = pd.DataFrame({'a':np.random.randint(1,10,20),
               'b':np.random.randint(1,500,20),
               'c':np.random.randint(20,50,20),
               'd':np.random.randint(1,500,20),
               'e':np.random.randint(20,50,20),
               'f':np.random.randint(1,500,20)})

ax2 = df.plot(kind='scatter', x='c', y='d', color='g')    
ax1 = ax2.twiny()
_ = df.plot(kind='scatter', x='a', y='b', color='r',ax=ax1)
_ = df.plot(kind='scatter', x='e', y='f', color='b', ax=ax2)

Output:

enter image description here

Scott Boston
  • 147,308
  • 15
  • 139
  • 187
  • Thanks have never heard of that am testing it now – cardamom Sep 02 '17 at 18:52
  • Basically, you are creating a secondary x-axis, so you plot some of the data on the primary x-axis and the other data on the secondary a-axis. Notice the scale of the axis on top vs 'normal' x-axis at the bottom. – Scott Boston Sep 02 '17 at 18:56
  • My data actually has a fourth x-variable, can your pattern above be continued to include another line? – cardamom Sep 02 '17 at 18:59
  • Yes, I all four can be plotted on two scales. Just pick which axes you want to plot each x on. ax=ax1 or ax=ax2. – Scott Boston Sep 02 '17 at 19:00
  • 1
    Thanks, it does basically work in that it scales some of the coordinates but not sure if it can be coaxed to scale them all at once, learned something new, will experiment with the MinMaxScaler on the dataframe some more.. – cardamom Sep 02 '17 at 19:13
  • Yeah, there are more ways to skin a cat. Thanks for the upvote and accept. – Scott Boston Sep 02 '17 at 19:14