0

I am using matplotlib to do a Component-Component plus Residual (CCPR) Plots (= partial residual plot)

This script :

fig, ax = plt.subplots(figsize=(5, 5))
fig = sm.graphics.plot_ccpr(lm_full, 'diag[T.sz]', ax=ax)
plt.close

Gives :

Bad Plot

How can I modify my script to get something like

Good plot

I don't want my dots to be aligned. In both cases, the variables of the x axis are dummy variable (ill vs healthy controls).

This may seem stupid, but I don't even know how to express what I want : it's much more easier with the images.

salim
  • 321
  • 1
  • 2
  • 12

1 Answers1

1

It sounds like you want to add some jitter to the x values, like this:

import numpy as np

# get x and y coordinates from the axes
coords = ax.collections[0].get_offsets()

# add small random number to each x coordinate
coords[:,0] = coords[:,0] + np.random.rand(coords.shape[0]) * 0.01

# move the points to the new coordinates
ax.collections[0].set_offsets(coords)
Michael
  • 969
  • 6
  • 19
  • run it after the line `fig = sm.graphics.plot_ccpr(lm_full, 'diag[T.sz]', ax=ax)` – Michael Dec 16 '16 at 17:36
  • I tried but the ax.collections[0] returns "list index out of range" – salim Dec 16 '16 at 17:38
  • it is quite strange because I have the same error message when trying to apply this example. http://stackoverflow.com/questions/38578873/how-to-get-x-and-y-coordinates-from-matplotlib-scatter-plot-using-plt-gca could it be possible that this function has been deprecated ? – salim Dec 17 '16 at 00:29
  • I tried also to do that in seaborn, but i didn't find documentation on a partial regression plot.... – salim Dec 18 '16 at 16:39