I just want to mark where the maximum deviation is occurred between two curves using matplotlib. Please help me.
The vertical distance is for the Kolmogorov–Smirnov test
import numpy as np
%matplotlib inline
import matplotlib.pyplot as plt
import scipy.stats as stats
#------------------------------------
data=np.random.uniform(low=1,high=10,size=300)
standardized_data=np.sort(data-np.mean(data))/np.std(data)
probs=np.arange(1.0, 301)/300
plt.plot(standardized_data,probs) #curve1
plt.plot(stats.norm.ppf(probs),probs) #curve2
plt.show()
output:
like this
or like this
You would need to interpolate one of the curves on the x values of the other. Then find the maximum of their difference.
import numpy as np
import matplotlib.pyplot as plt
import scipy.stats as stats
#------------------------------------
data=np.random.uniform(low=1,high=10,size=300)
x1= np.sort(data-np.mean(data))/np.std(data)
y = np.arange(1.0, 301)/300
x2 = stats.norm.ppf(y)
yc = np.interp(x1, x2, y)
ind_max = np.argmax((yc-y)**2)
plt.plot(x1, y) #curve1
plt.plot(x2, y) #curve2
plt.axvline(x1[ind_max], color="red", linestyle="dashed", alpha=0.4)
plt.plot([x1[ind_max], x1[ind_max]], [y[ind_max], yc[ind_max]], color="red")
plt.show()
Related
I'm trying to find the FWHM of a curve I've generated. This is the code for the curve and a picture of what it looks like.
import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import norm
from scipy.signal import peak_widths, find_peaks
k = np.load('/data/Var/test.npy')
x = np.load('/data/Var/sigrng.npy')
peakk = find_peaks(k)
kfwhm = peak_widths(k, peakk[0], rel_height=0.5)
plt.plot(x, k, ls='--', color='red')
plt.show()
Which produces this curve:
However, when I print out the output from what is supposed to be the FWHM and plot it on the curve it's not evaluating on the actual curve and is giving values much larger than what I expect.
import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import norm
from scipy.signal import peak_widths, find_peaks
k = np.load('/data/Var/test.npy')
x = np.load('/data/Var/sigrng.npy')
peakk = find_peaks(k)
kfwhm = peak_widths(k, peakk[0], rel_height=0.5)
plt.plot(x, k, ls='--', color='red')
plt.hlines(*kfwhm[1:], color="red")
plt.show()
Can anyone see anything I'm doing wrong that could cause this? When checking where the peak in the curve is manually the find_peaks function is working correctly.
I worked out the problem. It is due to the fact that when it calculates the FWHM it does not take into account the x values that you plot the curve against. Since my plots are supposed to go between 0 and 1 I simply divide the values by the size of the array on the y axis (here the array k) and it fixes the issue. Code below:
peakk, _ = find_peaks(k)
kfwhm = peak_widths(k, peakk, rel_height=0.5)
kfwhm = np.asarray(kfwhm)
plt.plot(x_k, k, ls='--', color='red')
plt.hlines(0.5, *(kfwhm[2:]/k.size), color="blue")
plt.show()
Giving:
and a FWMH of:
print(kfwhm[0]/k.size)
Out[384]: array([0.02415405])
I draw an exponential chart and now I want to move the peak freely along the X axis.
The center should be like a bell, and the right side like a my current chart but totaly reversed.
How can I do that?
import matplotlib
import math
import numpy as np
matplotlib.use('Agg')
import matplotlib.pyplot as plt
arr_name=[]
arr_value=[]
k=-0.01
for l in range(0,100):
x=pow(k,2)*np.exp(-k*1.8);
if (l%10)==0:
arr_name.append(l/10)
else:
arr_name.append("")
arr_value.append(x)
k=k+0.05
print x
y_pos = np.arange(len(arr_name))
plt.figure(figsize=(8,4))
plt.rcParams.update({'font.size': 8})
plt.subplot(1,1,1)
plt.plot(y_pos, arr_value, '-r')
plt.xticks(y_pos, arr_name)
plt.savefig('/var/www/html/bar/img/test_chart.png')
update:
possible solution to draw peak at the center of the chart:
for l in range(0,100):
x=pow(k,5)*np.exp(-k*1.96);
if (l%10)==0:
arr_name.append(l/10)
else:
arr_name.append("")
arr_value.append(x)
if l>50:
k=k-0.05
else:
k=k+0.05
print x
adding reversibility:
arr_value.reverse()
You can plot this much easier without a four loop. Varying the coefficient inside the exponential moves the value and location of the peak.
import matplotlib.pyplot as plt
import numpy as np
def f(x, s):
x**2 * np.exp(-s*x)
x = np.linspace(0, 10, 100)
y1 = f(x, 1.8)
y2 = f(x, 0.8)
plt.plot(x, y1, label='s=1.8')
plt.plot(x, y2, label='s=0.8')
plt.legend()
plt.show()
I am trying to plot a graph using matplotlib.pyplot.
import matplotlib.pyplot as plt
import numpy as np
x = [i for i in range (1,201)]
y = np.loadtxt('final_fscore.txt', dtype=np.float128)
plt.plot(x, y, lw=2)
plt.show()
It looks something like this:
I want to mark the first value of x where y has reached the highest ( which is already known, say for x= 23, y= y[23]), like this figure shown below:
I have been searching this for some time now, with little success. I have tried adding a straight line for now, which is not behaving the desired way:
import matplotlib.pyplot as plt
import numpy as np
x = [i for i in range (1,201)]
y = np.loadtxt('final_fscore.txt', dtype=np.float128)
plt.plot(x, y, lw=2)
plt.plot([23,y[23]], [23,0])
plt.show()
Resulting graph:
Note: I want to make the figure like in the second graph.
It's not clear what y[23] would do here. You would need to find out the maximum value and the index at which this occurs (np.argmax). You may then use this to plot a 3 point line with those coordinates.
import matplotlib.pyplot as plt
import numpy as np; np.random.seed(9)
x = np.arange(200)
y = np.cumsum(np.random.randn(200))
plt.plot(x, y, lw=2)
amax = np.argmax(y)
xlim,ylim = plt.xlim(), plt.ylim()
plt.plot([x[amax], x[amax], xlim[0]], [xlim[0], y[amax], y[amax]],
linestyle="--")
plt.xlim(xlim)
plt.ylim(ylim)
plt.show()
I'm using Matplotlib's function hist2d() and I want to unpack the output in order to further use it. Here's what I do: I simply load with numpy a 2-column file containing my data and use the following code
import matplotlib.pyplot as plt
from matplotlib.colors import LogNorm
import numpy as np
traj = np.loadtxt('trajectory.txt')
x = traj[:,0]
y = traj[:,1]
M, xe, ye, img = plt.hist2d(x, y, bins = 80, norm = LogNorm())
plt.imshow(M)
plt.show()
The result I get is the following:
Instead, if I try to directly plot the hist2d results without unpacking them:
import matplotlib.pyplot as plt
from matplotlib.colors import LogNorm
import numpy as np
traj = np.loadtxt('trajectory.txt')
x = traj[:,0]
y = traj[:,1]
plt.hist2d(x, y, bins = 80, norm = LogNorm())
plt.show()
I get the whole plot without the strange blue box. What am I doing wrong?
You can create a histogram plot directly with plt.hist2d. This calculates the histogram and plots it to the current axes. There is no need to show it yet another time using imshow.
import matplotlib.pyplot as plt
from matplotlib.colors import LogNorm
import numpy as np; np.random.seed(9)
x = np.random.rayleigh(size=9900)
y = np.random.rayleigh(size=9900)
M, xe, ye, img = plt.hist2d(x, y, bins = 80, norm = LogNorm())
plt.show()
Or, you may first calculate the histogram and afterwards plot the result as an image to the current axes. Note that the histogram produced by numpy is transposed, see Matplotlib 2D histogram seems transposed, making it necessary to call imshow(M.T). Also note that in order to obtain the correct axes labeling, you need to set the imshow's extent to the extremal values of the xe and ye edge arrays.
import matplotlib.pyplot as plt
from matplotlib.colors import LogNorm
import numpy as np; np.random.seed(9)
x = np.random.rayleigh(size=9900)
y = np.random.rayleigh(size=9900)
M, xe, ye = np.histogram2d(x, y, bins = 80)
extent = [xe[0], xe[-1], ye[0], ye[-1]]
plt.imshow(M.T, extent=extent, norm = LogNorm(), origin="lower")
plt.show()
I'm trying to do a little bit of distribution plotting and fitting in Python using SciPy for stats and matplotlib for the plotting. I'm having good luck with some things like creating a histogram:
seed(2)
alpha=5
loc=100
beta=22
data=ss.gamma.rvs(alpha,loc=loc,scale=beta,size=5000)
myHist = hist(data, 100, normed=True)
Brilliant!
I can even take the same gamma parameters and plot the line function of the probability distribution function (after some googling):
rv = ss.gamma(5,100,22)
x = np.linspace(0,600)
h = plt.plot(x, rv.pdf(x))
How would I go about plotting the histogram myHist with the PDF line h superimposed on top of the histogram? I'm hoping this is trivial, but I have been unable to figure it out.
just put both pieces together.
import scipy.stats as ss
import numpy as np
import matplotlib.pyplot as plt
alpha, loc, beta=5, 100, 22
data=ss.gamma.rvs(alpha,loc=loc,scale=beta,size=5000)
myHist = plt.hist(data, 100, normed=True)
rv = ss.gamma(alpha,loc,beta)
x = np.linspace(0,600)
h = plt.plot(x, rv.pdf(x), lw=2)
plt.show()
to make sure you get what you want in any specific plot instance, try to create a figure object first
import scipy.stats as ss
import numpy as np
import matplotlib.pyplot as plt
# setting up the axes
fig = plt.figure(figsize=(8,8))
ax = fig.add_subplot(111)
# now plot
alpha, loc, beta=5, 100, 22
data=ss.gamma.rvs(alpha,loc=loc,scale=beta,size=5000)
myHist = ax.hist(data, 100, normed=True)
rv = ss.gamma(alpha,loc,beta)
x = np.linspace(0,600)
h = ax.plot(x, rv.pdf(x), lw=2)
# show
plt.show()
One could be interested in plotting the distibution function of any histogram.
This can be done using seaborn kde function
import numpy as np # for random data
import pandas as pd # for convinience
import matplotlib.pyplot as plt # for graphics
import seaborn as sns # for nicer graphics
v1 = pd.Series(np.random.normal(0,10,1000), name='v1')
v2 = pd.Series(2*v1 + np.random.normal(60,15,1000), name='v2')
# plot a kernel density estimation over a stacked barchart
plt.figure()
plt.hist([v1, v2], histtype='barstacked', normed=True);
v3 = np.concatenate((v1,v2))
sns.kdeplot(v3);
plt.show()
from a coursera course on data visualization with python
Expanding on Malik's answer, and trying to stick with vanilla NumPy, SciPy and Matplotlib. I've pulled in Seaborn, but it's only used to provide nicer defaults and small visual tweaks:
import numpy as np
import scipy.stats as sps
import matplotlib.pyplot as plt
import seaborn as sns
sns.set(style='ticks')
# parameterise our distributions
d1 = sps.norm(0, 10)
d2 = sps.norm(60, 15)
# sample values from above distributions
y1 = d1.rvs(300)
y2 = d2.rvs(200)
# combine mixture
ys = np.concatenate([y1, y2])
# create new figure with size given explicitly
plt.figure(figsize=(10, 6))
# add histogram showing individual components
plt.hist([y1, y2], 31, histtype='barstacked', density=True, alpha=0.4, edgecolor='none')
# get X limits and fix them
mn, mx = plt.xlim()
plt.xlim(mn, mx)
# add our distributions to figure
x = np.linspace(mn, mx, 301)
plt.plot(x, d1.pdf(x) * (len(y1) / len(ys)), color='C0', ls='--', label='d1')
plt.plot(x, d2.pdf(x) * (len(y2) / len(ys)), color='C1', ls='--', label='d2')
# estimate Kernel Density and plot
kde = sps.gaussian_kde(ys)
plt.plot(x, kde.pdf(x), label='KDE')
# finish up
plt.legend()
plt.ylabel('Probability density')
sns.despine()
gives us the following plot:
I've tried to stick with a minimal feature set while producing relatively nice output, notably using SciPy to estimate the KDE is very easy.