Any way to create a histogram using matplotlib.pyplot without plotting?

I use matplotlib.pyplot to create bar charts. I'm not interested in the graphs of these histograms, but they are interested in frequencies and cells (I know that I can write my own code for this, but would prefer to use this package).

I know I can do the following:

import numpy as np import matplotlib.pyplot as plt x1 = np.random.normal(1.5,1.0) x2 = np.random.normal(0,1.0) freq, bins, patches = plt.hist([x1,x1],50,histtype='step') 

to create a histogram. All I need is freq[0] , freq[1] and bins[0] . The problem occurs when I try to use,

 freq, bins, patches = plt.hist([x1,x1],50,histtype='step') 

in function. For instance,

 def func(x, y, Nbins): freq, bins, patches = plt.hist([x,y],Nbins,histtype='step') # create histogram bincenters = 0.5*(bins[1:] + bins[:-1]) # center bins xf= [float(i) for i in freq[0]] # convert integers to float xf = [float(i) for i in freq[1]] p = [ (bincenters[j], (1.0 / (xf[j] + yf[j] )) for j in range(Nbins) if (xf[j] + yf[j]) != 0] Xt = [j for i,j in p] # separate pairs formed in p Yt = [i for i,j in p] Y = np.array(Yt) # convert to arrays for later fitting X = np.array(Xt) return X, Y # return arrays X and Y 

When I call func(x1,x2,Nbins) and print or print X and Y , I do not get the expected curve / value. I suspect this is due to plt.hist , as there is a partial histogram in my plot.

+7
source share
3 answers

I donโ€™t know if I understand your question well, but here you have an example of a simple home histogram (in 1D or 2D), each of which inside the function is correctly called:

 import numpy as np import matplotlib.pyplot as plt def func2d(x, y, nbins): histo, xedges, yedges = np.histogram2d(x,y,nbins) plt.plot(x,y,'wo',alpha=0.3) plt.imshow(histo.T, extent=[xedges.min(),xedges.max(),yedges.min(),yedges.max()], origin='lower', interpolation='nearest', cmap=plt.cm.hot) plt.show() def func1d(x, nbins): histo, bin_edges = np.histogram(x,nbins) bin_center = 0.5*(bin_edges[1:] + bin_edges[:-1]) plt.step(bin_center,histo,where='mid') plt.show() x = np.random.normal(1.5,1.0, (1000,1000)) func1d(x[0],40) func2d(x[0],x[1],40) 

Of course, you can verify that the data is centered correctly, but I think the example shows some useful information about this topic.

My recommendation: try to avoid any loop in your code! They kill the play. If you look, in my example there are no loops. Best practice in python numeric issues is to avoid loops! Numpy has many C-implemented functions that do all the complex work of the loop.

+3
source

No.

But you can get around the gun:

 import matplotlib.pyplot fig = matplotlib.figure.Figure() ax = matplotlib.axes.Axes(fig, (0,0,0,0)) numeric_results = ax.hist(data) del ax, fig 

This will not affect the active axes and shapes, so it would be nice to use it even in the middle of another chart.

This is because any use of plt.draw_something() will put the graph in the current axis - a global variable.

0
source

If you want to simply calculate the histogram (that is, count the number of points in a given bin) and not display it, the np.histogram () function is available

0
source

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


All Articles