0

I have only the angle values for a set of data. Now i need to plot a angle distribution curve ie., angle on the x axis v/s no.of times/frequency of angle occurring on the y axis. These are the angles sorted out for a set of data:-

[98.1706427, 99.09896751, 99.10879006, 100.47518838, 101.22770381, 101.70374296,
103.15715294, 104.4653976,105.50441485, 106.82885361, 107.4605319, 108.93228646,
111.22463712, 112.23658018, 113.31223886, 113.4000603, 114.14565594, 114.79809084,
115.15788861, 115.42991416, 115.66216071, 115.69821092, 116.56319054, 117.09232139,
119.30835385, 119.31377834, 125.88278338, 127.80937901, 132.16187185, 132.61262906,
136.6751744, 138.34164387,]

How can i do this..?? How can i write a python program for this...?? and plot it in a graph as a distribution curve

Thomas Kühn
  • 9,412
  • 3
  • 47
  • 63
Dilip.H.N
  • 21
  • 4

2 Answers2

0

EDIT:

If you want a line plot as well, it is better to generate the histogram with numpy and then use that information also for the line:

from matplotlib import pyplot as plt
import numpy as np

angles = [98.1706427, 99.09896751, 99.10879006, 100.47518838, 101.22770381,
101.70374296, 103.15715294, 104.4653976, 105.50441485, 106.82885361, 
107.4605319, 108.93228646, 111.22463712, 112.23658018, 113.31223886, 
113.4000603, 114.14565594, 114.79809084, 115.15788861, 115.42991416, 
115.66216071, 115.69821092, 116.56319054, 117.09232139, 119.30835385, 
119.31377834, 125.88278338, 127.80937901, 132.16187185, 132.61262906,
136.6751744, 138.34164387, ] 


hist,edges = np.histogram(angles, bins=20)
bin_centers = 0.5*(edges[:-1] + edges[1:])
bin_widths = (edges[1:]-edges[:-1])



plt.bar(bin_centers,hist,width=bin_widths)
plt.plot(bin_centers, hist,'r')
plt.xlabel('angle [$^\circ$]')
plt.ylabel('frequency')
plt.show()

this looks like this: angle histogram with curve

If you are not interested in the histogram itself, leave out the line plt.bar(bin_centers,hist,width=bin_widths).

EDIT2:

I don't really see the scientific value in a smoothed histogram. If you increase the resolution of the histogram (the bins parameter in the np.histogram command), it can change quite considerably. For instance, new peaks may occur if you increase the bin count, or two peaks may merge into one if you decrease the bin count. Keeping this in mind, smoothing the histogram curve suggests that you have more data than you do. However, if you really must, you can smooth a curve as explained in this answer, i.e.

from scipy.interpolate import spline
x = np.linspace(edges[0], edges[-1], 500)
y = spline(bin_centers, hist, x)

and then plot y over x.

Thomas Kühn
  • 9,412
  • 3
  • 47
  • 63
  • n.p. you can accept the answer, if this solves your problem. – Thomas Kühn May 24 '17 at 04:51
  • But my another question is tht this is a histogram plot. How can i plot the curve over this histogram..?? ie i needed in terms of curve (not the gaussian curve) ... and tht would be better if both histogram and curve are in same plot – Dilip.H.N May 24 '17 at 04:52
  • Do you mean a line plot with the histogram values? – Thomas Kühn May 24 '17 at 04:53
  • Sir , i mean tht a curve plot... but not the Gaussian curve(since it has one maxima), curve plot which follows the histogram (since from histogram thr is more no.of angles around 115 degree and less around 120 degree). so the curve showing peak around 115 degree and minima around 120 degree and so on... – Dilip.H.N May 24 '17 at 04:58
  • Can i smoothen the line plot...?? ie., it shows more pointed one and to the centre of every bar in histogram... but how can i smoothen the line graph since it is depicting the angle distribution (ie to say tht i have more angles around 115 degree, comparatively less round 120 degree ). – Dilip.H.N May 24 '17 at 05:24
  • No Sir, i need histogram too, and thn the line graph (smoothened line graph- more or like a free hand curve depicting it) – Dilip.H.N May 24 '17 at 05:44
  • Sir, i didn't mean smoothening the histogram, i meant smoothening of the line plot... – Dilip.H.N May 24 '17 at 07:47
  • Yes, I know. And I gave you a method how to do that. Did you try it? – Thomas Kühn May 24 '17 at 07:53
0

Function hist actually returns the x and y coordinates of the bins. You can use this function to prepare the data for the line plot:

y, x, _ = plt.hist(angles) # No need for the 3rd return value
xc = (x[:-1] + x[1:]) / 2 # Take centerpoints
# plt.clf()
plt.plot(xc, y)
plt.show() # Etc.

You will end up having both the histogram and the line plot. If this is not desirable, clean the canvas before plotting the line by uncommenting the call to clf().

DYZ
  • 55,249
  • 10
  • 64
  • 93
  • Can i smoothen the line plot...?? ie., it shows more pointed one and to the centre of every bar in histogram... but how can i smoothen the line graph since it is depicting the angle distribution (ie to say tht i have more angles around 90 degree, comparatively less round 110 degree , again maxima around 120 degree ). – Dilip.H.N May 24 '17 at 06:02
  • Sure. You can use `numpy.interp()` on the Xs and Ys https://docs.scipy.org/doc/numpy/reference/generated/numpy.interp.html – DYZ May 24 '17 at 16:17