Python equivalent for normal MATLAB plan?

Is there an equivalent python function similar to normplot from MATLAB? Perhaps in matplotlib?

MATLAB Syntax:

 x = normrnd(10,1,25,1); normplot(x) 

gives:

enter image description here

I tried using the matplotlib and numpy modules to determine the probability / percentile of the values ​​in the array, but the weight scales of the y-axis of the graph are linear compared to the graph from MATLAB.

 import numpy as np import matplotlib.pyplot as plt data =[-11.83,-8.53,-2.86,-6.49,-7.53,-9.74,-9.44,-3.58,-6.68,-13.26,-4.52] plot_percentiles = range(0, 110, 10) x = np.percentile(data, plot_percentiles) plt.plot(x, plot_percentiles, 'ro-') plt.xlabel('Value') plt.ylabel('Probability') plt.show() 

gives: enter image description here

Otherwise, how can you scale the scales, as in the first figure?

Thanks.

+6
source share
4 answers

Late answer, but I just ran into the same problem and found a solution that is worth sharing. I think so.

As Joris pointed out, the probplot function is equivalent to the norm, but the resulting distribution has the form of a cumulative density function. Scipy.stats also offers a function to convert these values.

cdf β†’ percentiles

 stats.'distribution function'.cdf(cdf_value) 

percentiles β†’ cdf

 stats.'distribution function'.ppf(percentile_value) 

eg:

 stats.norm.ppf(percentile) 

To get an equivalent y axis, such as a normal plan, you can replace the cdf ticks:

 from scipy import stats import matplotlib.pyplot as plt nsample=500 #create list of random variables x=stats.t.rvs(100, size=nsample) # Calculate quantiles and least-square-fit curve (quantiles, values), (slope, intercept, r) = stats.probplot(x, dist='norm') #plot results plt.plot(values, quantiles,'ob') plt.plot(quantiles * slope + intercept, quantiles, 'r') #define ticks ticks_perc=[1, 5, 10, 20, 50, 80, 90, 95, 99] #transfrom them from precentile to cumulative density ticks_quan=[stats.norm.ppf(i/100.) for i in ticks_perc] #assign new ticks plt.yticks(ticks_quan,ticks_perc) #show plot plt.grid() plt.show() 

Result:

probability plot with matplotlib

+4
source

I'm sure matplotlib does not provide anything like this.

This can be done, of course, but you will either have to rescale your data, or change the ticks / labels of the y-axis to match, or, if you plan to do this often, perhaps introduce a new scale that can be applied to the matplotlib axes, as in this example: http://matplotlib.sourceforge.net/examples/api/custom_scale_example.html .

+2
source

Perhaps you can use the probplot scipy function ( scipy.stats ), this seems like the equivalent for the normal MATLAB plan:

Calculate the quantiles for the probability plot of the sampled data against the indicated theoretical distribution.

probplot optionally calculates the best line for data and graphs using Matplotlib or a given graph function.

http://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.probplot.html

But this does not solve your problem with a different y axis scale.

+2
source

Using matplotlib.semilogy approach the matlab output.

+1
source

Source: https://habr.com/ru/post/890740/


All Articles