sount
sount

Reputation: 33

Python: Multiple QQ-Plot

I am new and usually coming from R. I want to create a QQ-Plot wit multiple lines.

I have a beta distributed dataset I want to try different parameters for the beta distribution and compare them in one QQ-Plot for better comparison. If I try the following code, every plot has the same color and I got 3 QQ-lines. Is there a possibility to bring all this three QQ-plots into one?
I hope you get my problem

import scipy.stats as stats
import numpy
x=numpy.random.beta(2, 3, size=100)
stats.probplot(x, dist=stats.beta, sparams=(2,3),plot=plt,fit=False)
stats.probplot(x, dist=stats.beta, sparams=(1,2),plot=plt,fit=False)
stats.probplot(x, dist=stats.beta, sparams=(1,4),plot=plt,fit=False)

Kind regrads

Upvotes: 2

Views: 9068

Answers (1)

jwalton
jwalton

Reputation: 5686

Okay, so stats.probplot has left me a little confused. The documentation clearly states that:

probplot generates a probability plot, which should not be confused with a Q-Q or a P-P plot.

Yet all the sources I can find state that a probability plot refers to either a Q-Q plot or P-P plot. Go figure.

Anyway, as far as I'm concerned, what you've generated is a Q-Q plot.

It also seems to me that the option fit=False of stats.probplot is ignored, and a regression line is always added to the data.

Anyway, to get what you desire, we can explicitly create a matplotlib axes instance, and use the get_lines method to remove the unwanted regression lines and change the marker colours.

import scipy.stats as stats
import numpy as np
import matplotlib.pyplot as plt

plt.style.use('seaborn')

x = numpy.random.beta(2, 3, size=100)

fig, ax = plt.subplots(1, 1, figsize=(6, 4))
stats.probplot(x, dist=stats.beta, sparams=(2,3), plot=plt, fit=False)
stats.probplot(x, dist=stats.beta, sparams=(1,2), plot=plt, fit=False)
stats.probplot(x, dist=stats.beta, sparams=(1,4), plot=plt, fit=False)

# Remove the regression lines
ax.get_lines()[1].remove()
ax.get_lines()[2].remove()
ax.get_lines()[3].remove()

# Change colour of scatter
ax.get_lines()[0].set_markerfacecolor('C0')
ax.get_lines()[1].set_markerfacecolor('C1')
ax.get_lines()[2].set_markerfacecolor('C2')

# Add on y=x line
ax.plot([0, 1], [0, 1], c='C3')

This gave me the following, which I think this time really is what you desired:

enter image description here

Upvotes: 6

Related Questions