简体   繁体   中英

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

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 QQ or a PP plot.

Yet all the sources I can find state that a probability plot refers to either a QQ plot or PP plot. Go figure.

Anyway, as far as I'm concerned, what you've generated is a QQ 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:

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM