Python equivalent for MATLABs normplot?

2024/10/3 12:35:24

Is there a python equivalent 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 have tried using matplotlib & numpy module to determine the probability/percentile of the values in array but the output plot y-axis scales are linear as compared to the plot from MATLAB.

import numpy as np
import matplotlib.pyplot as pltdata =[-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

Else, how could the scales be adjusted as in the first plot?

Thanks.

Answer

A late answer, but I just came across the same problem and found a solution, that is worth sharing. I guess.

As joris pointed out the probplot function is an equivalent to normplot, but the resulting distribution is in form of the cumulative density function. Scipy.stats also offers a function, to convert these values.

cdf -> percentile

stats.'distribution function'.cdf(cdf_value)

percentile -> cdf

stats.'distribution function'.ppf(percentile_value)

for example:

stats.norm.ppf(percentile)

To get an equivalent y-axis, like normplot, you can replace the cdf-ticks:

from scipy import stats
import matplotlib.pyplot as pltnsample=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()

The result:

probability plot with matplotlib

https://en.xdnf.cn/q/70723.html

Related Q&A

python mask netcdf data using shapefile

I am using the following packages:import pandas as pd import numpy as np import xarray as xr import geopandas as gpdI have the following objects storing data:print(precip_da)Out[]:<xarray.DataArray …

Whats a good general way to look SQLAlchemy transactions, complete with authenticated user, etc?

Im using SQLAlchemys declarative extension. Id like all changes to tables logs, including changes in many-to-many relationships (mapping tables). Each table should have a separate "log" table…

OpenCV - Tilted camera and triangulation landmark for stereo vision

I am using a stereo system and so I am trying to get world coordinates of some points by triangulation.My cameras present an angle, the Z axis direction (direction of the depth) is not normal to my sur…

Node.jss python child script outputting on finish, not real time

I am new to node.js and socket.io and I am trying to write a small server that will update a webpage based on python output. Eventually this will be used for a temperature sensor so for now I have a du…

lambda function returning the key value for use in defaultdict

The function collections.defaultdict returns a default value that can be defined by a lambda function of my own making if the key is absent from my dictionary.Now, I wish my defaultdict to return the u…

Calling Matlab function from python

I have one project in which I have one one matlab code which I have to run tho Django. I tried installing Mlabwrap ..But it gives me following error.Traceback (most recent call last): File "<st…

Suds ignoring proxy setting

Im trying to use the salesforce-python-toolkit to make web services calls to the Salesforce API, however Im having trouble getting the client to go through a proxy. Since the toolkit is based on top of…

CSV to JSON script

I took this script from here: import csv from itertools import izip f = open( /django/sw2/wkw2/csvtest1.csv, r ) reader = csv.reader( f ) keys = ( "firm_url", "firm_name", "fir…

Accessing an ALREADY running process, with Python

Question: Is there a way, using Python, to access the stdout of a running process? This process has not been started by Python.Context: There is a program called mayabatch, that renders out images fro…

sum up two pandas dataframes with different indexes element by element

I have two pandas dataframes, say df1 and df2, of some size each but with different indexes and I would like to sum up the two dataframes element by element. I provide you an easy example to better und…