python - Plotting histrogram from numpy array -


i need create histograms 2d arrays obtain convolving input array , filter. bins should range of values in array.

i tried following example: how numpy.histogram() work? code this:

import matplotlib.pyplot plt import numpy np plt.hist(result, bins = (np.min(result), np.max(result),1)) plt.show() 

i error message:

attributeerror: bins must increase monotonically. 

thanks help.

what doing specifying 3 bins first bin np.min(result), second bin np.max(result) , third bin 1. need provide want bins located in histogram, , must in increasing order. guess want choose bins np.min(result) np.max(result). 1 seems bit odd, i'm going ignore it. also, want plot 1d histogram of values, yet input 2d. if you'd plot distribution of data on unique values in 1d, you'll need unravel 2d array when using np.histogram. use np.ravel that.

now, i'd refer np.linspace. can specify minimum , maximum value , many points want in between uniformly spaced. so:

bins = np.linspace(start, stop) 

the default number of points in between start , stop 50, can override this:

bins = np.linspace(start, stop, num=100) 

this means generate 100 points in between start , stop.

as such, try doing this:

import matplotlib.pyplot plt import numpy np num_bins = 100 # <-- change here - specify total number of bins histogram plt.hist(result.ravel(), bins=np.linspace(np.min(result), np.max(result), num=num_bins)) #<-- change here.  note use of ravel. plt.show() 

Comments

Popular posts from this blog

python - pip install -U PySide error -

arrays - C++ error: a brace-enclosed initializer is not allowed here before ‘{’ token -

cytoscape.js - How to add nodes to Dagre layout with Cytoscape -