Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change plot properties of statsmodels qqplot? (Python)

So I am plotting a normal Q-Q plot using statsmodels.graphics.gofplots.qqplot().

The module uses matplotlib.pyplot to create figure instance. It plots the graph well.

However, I would like to plot the markers with alpha=0.3.

Is there a way to do this?

Here is a sample of code:

import numpy as np
import statsmodels.api as sm
import matplotlib.pyplot as plt

test = np.random.normal(0,1, 1000)

sm.qqplot(test, line='45')
plt.show()

And the output figure: QQ Plot

like image 606
Ash B Avatar asked Mar 08 '16 21:03

Ash B


2 Answers

You can use statsmodels.graphics.gofplots.ProbPlot class which has qqplot method to pass matplotlib pyplot.plot **kwargs.

import numpy as np
import statsmodels.api as sm
import matplotlib.pyplot as plt

test = np.random.normal(0, 1, 1000)

pp = sm.ProbPlot(test, fit=True)
qq = pp.qqplot(marker='.', markerfacecolor='k', markeredgecolor='k', alpha=0.3)
sm.qqline(qq.axes[0], line='45', fmt='k--')

plt.show()

enter image description here

like image 83
su79eu7k Avatar answered Nov 15 '22 20:11

su79eu7k


qqplot returns a figure object which can be used to get the lines which can then be modified using set_alpha

fig = sm.qqplot(test, line='45');

# Grab the lines with blue dots
dots = fig.findobj(lambda x: hasattr(x, 'get_color') and x.get_color() == 'b')

[d.set_alpha(0.3) for d in dots]

enter image description here

Obviously you have a bit of overlap of the dots so even though they have a low alpha value, where they are piled on top of one another they look to be more opaque.

like image 41
Suever Avatar answered Nov 15 '22 21:11

Suever