# Questions tagged [bins]

49 questions

0

votes

0

answer

6

Views

### How to count non-zeroes values using binned_statistic

I need to efficiently process very large 1D arrays extracting some statistics per bin and I have found very useful the function binned_statistic from scipy.stats as it includes a 'statistic' argument that works quite efficiently.
I would like to perform a 'count' function but without considering zer...

1

votes

1

answer

339

Views

### Matplotlib Histogram: Green and Blue Bins

I'm using pyplot to make a histogram. Here is approximately what I'm doing:
import numpy as np
import pylab as pl
A = {my dataset as a dictionary: different numbers and their frequencies}
numbers = A.keys()
frequencies = A.values()
plot = np.transpose(np.array([[numbers,frequencies]])
n =
pl.hist(p...

1

votes

1

answer

247

Views

### MATLAB how to plot a vector of probability densities on to a histogram?

I currently have a vector of calculated probability densities, i.e.
probden = (0.0008, 0.0016, 0.0048, 0.0064, 0.0072, ... , 1.0936, ... , 0.0072, 0.0064, 0.0048, 0.0016, 0.0008)
The list of calculated probability densities should be in the shape of a normal distribution.
I also have a same length...

0

votes

1

answer

24

Views

### Split an array into data based on bins returned by numpy histogram

I have an array x with data like this: [3.1, 3.0, 3.3, 3.5, 3.8, 3.75, 4.0] etc.
I have another variable y with corresponding 0s and 1s [0, 1, 0]
I want to get from that new separate arrays to have that divided
freq, bins = np.histogram(X, 5)
That allows me to know the cutoffs for each bin. But how...

17

votes

2

answer

9.6k

Views

### Matplotlib histogram with collection bin for high values

I have an array with values, and I want to create a histogram of it. I am mainly interested in the low end numbers, and want to collect every number above 300 in one bin. This bin should have the same width as all other (equally wide) bins. How can I do this?
Note: this question is related to this q...

1

votes

3

answer

779

Views

### Divide into bins R

I have the following data
A 1 6
A 2 72
A 3 90
A 4 81
A 5 81
A 6 42
A 7 12
A 8 32
A 9 34
A 10 92
B 1 44
B 2 54
B 3 10
B 4 21
B 5 47
B 6 35
B 7 94
B 8 5
B 9 35
B 10 77
B 11 9
B 12 52
B 13 73
B 14 93
B 15 38
B...

6

votes

3

answer

11k

Views

### how to generate bins for histogram using apache math 3.0 in java?

I have been looking for away to generate bins for specific dataset (by specifying lower band, upper band and number of bins required) using apache common math 3.0. I have looked at Frequency http://commons.apache.org/math/apidocs/org/apache/commons/math3/stat/Frequency.html
but it does not give me w...

6

votes

5

answer

16.4k

Views

### Normalizing histogram bins in gnuplot

I'm trying to plot a histogram whose bins are normalized by the number of elements in the bin.
I'm using the following
binwidth=5
bin(x,width)=width*floor(x/width) + binwidth/2.0
plot 'file' using (bin($2, binwidth)):($4) smooth freq with boxes
to get a basic histogram, but I want the value of eac...

2

votes

3

answer

642

Views

### Find lists items closest to certain values in Python

I have a list of sorted floats y, as well as a list of unsorted floats x.
Now, I need to find out for every element in x between which values of y it lies, preferably by index of y. So for example, if
y=[1,2,3,4,5]
x[0]=3.5
I would need the output for index 0 of x to be (2,3), because 3.5 is betw...

3

votes

1

answer

15.5k

Views

### Python pyplot histogram: Adjusting bin width, Not number of bins

I have been able to make myself a pretty little histogram that looks like this:
I was able to produce the image with the following code:
import numpy as np
import matplotlib.pyplot as plt
plt.figure()
plt.axis([0, 6000, 0, 45000])
data['column'][data.value == 0].hist(bins=200, label='A')
data['...

1

votes

1

answer

2.9k

Views

### how to make a frequency function in matlab like the excel frequency function?

I have a list that is sorted and I want to know how many values are in each bin? I made bins using linspace(floor(fist_element_list), ceil(last_element_list), num_bins) Is there a built in function or an easy way to do this in Matlab? All I can think of is doing it manually. I would like a frequency...

4

votes

2

answer

713

Views

### cut several variables, using several different number of bins for each variable

I have a dataset which looks like this:
ID a b
ID1 0.1 20.3
ID2 0.2 21.6
ID3 1.2 1.5
etc.
I would like to group these values into equal-sized groups. This can be done with:
data$bin1

4

votes

2

answer

166

Views

### Pandas: Bin dates into 30 minute intervals and calculate averages

I have a Pandas dataframe with two columns which are speed and time.
speed date
54.72 1:33:56
49.37 1:33:59
37.03 1:34:03
24.02 7:39:58
28.02 7:40:01
24.04 7:40:04
24.02 7:40:07
25.35 7:40:10
26.69 7:40:13
32.04 7:40:16
28.02 11:05:43
30.71 11:05:46
29.36 11:05:49
18.68...

1

votes

1

answer

102

Views

### Create bins and dataframe including a calculation

import pandas as pd
import numpy as np
import random
import string
N = 100
J = [2012,2013,2014]
K = ['A','B','C','D','E','F','G','H']
L = ['h','d','a']
df = pd.DataFrame(
np.random.uniform(1,10,size=(N, 3)),
columns=list('XYZ')
)
df['ht'] = pd.Series(random.choice(K) for _ in range(N))
df['at'] =...

3

votes

1

answer

463

Views

### MATLAB: combining and normalizing histograms with different sample sizes

I have four sets of data, the distribution of which I would like to represent in MATLAB in one figure. Current code is:
[n1,x1]=hist([dataset1{:}]);
[n2,x2]=hist([dataset2{:}]);
[n3,x3]=hist([dataset3{:}]);
[n4,x4]=hist([dataset4{:}]);
bar(x1,n1,'hist');
hold on; h1=bar(x1,n1,'hist'); set(h1,'facec...

3

votes

1

answer

589

Views

### Unable to align bins in a histogram of datetime objects using the hist() function

So I am trying to plot a histogram of some datetime objects, but i can never get the bins to line up with the bars. My code is below:
I start by importing my stuff and declaring the start, end and a one day object:
import datetime
import matplotlib.pyplot as plt
end = datetime.date(2017,5,14)
star...

2

votes

1

answer

855

Views

### easy way to subset data into bins

I have a data frame as seen below with over 1000 rows. I would like to subset the data into bins by 1m intervals (0-1m, 1-2m, etc.). Is there an easy way to do this without finding the minimum depth and using the subset command multiple times to place the data into the appropriate bins?
Temp..ºC. D...

2

votes

2

answer

404

Views

### How to bin data based on values in one column, and count occurrences from another column excluding duplicates in R?

I have a file of r-values for correlations. I want to split the r-values into bins and count how many CNVs are in each bin. Is there a way to do this without the duplicates?
GeneChr SNP SNP_Position CNV start end r-value
1 rs7520551 100716167 1:101161140-10...

2

votes

1

answer

79

Views

### How do i get all the numbers of fft bins in a defined frequency band?

I use the matlab software. To my question.
I have a audio signal, on which i am applying a STFT. I take a segment
(46 ms, specifially chosen) out of my signal y(audio signal) and use a FFT on it. Then i go to the next segment, until to end of my audio signal.
My WAV-File is 10.8526 seconds long. I...

2

votes

1

answer

539

Views

### Equivalent of histc function in R?

Is there an equivalent function in R to the matlab function histc?
Vector and bins are numeric.
[ countInBin, binIndexOfVectorElement ] = histc( vector, bins )
where:
bins = min(vector) : increment : max( vector )
and
length( binIndexOfVectorElement ) = length( vector )

2

votes

1

answer

475

Views

### How to create a difference map between two matplotlib hexbin maps?

I encountered a problem on creating a difference map between two matplotlib.pyplot hexbin plots, which means to get the value differences of each corresponding hexbin first and then create a difference hexbin map.
To give a simple example of my problem here, say the value of one hexbin in Map 1 is...

2

votes

2

answer

230

Views

### histogram with varying bin widths

I am trying to replicate the concept of chart Fig 1 from the following paper (http://dx.doi.org/10.1016/j.envsci.2011.08.004):
It is a histogram whose bin widths vary dependent upon the value of x and whose height depends on variable y. The precise values in the chart are not of concern - rather, u...

3

votes

0

answer

1.1k

Views

### Numpy: binning data with ties according to percentiles

I want to bin my data into 10 bins (histograms) using percentile ranges:
bins = [0, 10th-percentile(myData), 20th-percentile(myData), 30th..., 90th-percentile(myData), +inf]
So in order make a histogram out of my data, I just do:
import numpy as np
myBinnedData = np.histogram(myData, bins=bins)[0]
M...

2

votes

2

answer

67

Views

### Python: checking which bins two time points belong to

I have a list of lists with two values that represent a start time-point and an end time-point. I would like to count how much of the time range between the two points fall into bins.
The bins are between 0-300,300-500 and 500-1200.
I would also like to bin them between 0-50, 50-100, 100-150 and so...

7

votes

1

answer

2.3k

Views

### With `pandas.cut()`, how do I get integer bins and avoid getting a negative lowest bound?

My dataframe has zero as the lowest value. I am trying to use the precision and include_lowest parameters of pandas.cut(), but I can't get the intervals consist of integers rather than floats with one decimal. I can also not get the left most interval to stop at zero.
import pandas as pd
import seab...

2

votes

0

answer

58

Views

### Creating bins dynamically in Tableu

I've got 50+ measures and I want to create histogram with binned data and switching displayed measures dynamically. I know how create bins manually but is there any possibility to create bins dynamically?

2

votes

0

answer

395

Views

### ValueError: incompatible categories in categorical concat

It is probably quite easy to solve, I however, cannot find the solution.
When I use pd.qcut and groupby on column 'S' I get the ValueError if I have multiple values in that column. When I only have one (AR1), it returns the result needed. What I am doing wrong? Btw, when I leave the labels, it retur...

2

votes

1

answer

1.5k

Views

### Extending the range of bins in seaborn histogram

I'm trying to create a histogram with seaborn, where the bins start at 0 and go to 1. However, there is only date in the range from 0.22 to 0.34. I want the empty space more for a visual effect to better present the data.
I create my sheet with
import pandas as pd
import matplotlib as mpl
import ma...

2

votes

1

answer

1.1k

Views

### How to choose number of bins in numpy.histogram?

If I use histogram of matplotlib , I can choose the number of bins.
But how can I choose the number of bins at histogram of numpy?
import matplotlib.pyplot as plt
import numpy as np
array = [1,3,4,4,8,9,10,12]
range = int((max(array)) - min(array))+1
x, bins, patch = plt.hist(array, bins=range)
In t...

2

votes

1

answer

19

Views

### How to create a matrix based on bins?

I have a set of values ranging from 3-27, that have 20 finite values:
A = [(0,21),(1,12),(2,15),(3,3),(4,21),(5,15),(6,27),(7,21),(8,9),(9,27),(10,12),(11,9),(12,12),(13,3),(14,9),(15,12),(16,6),(17,3),(18,9),(19,15)]
I would like to learn how to create a numpy array that would have 9 bins, each on...

2

votes

1

answer

571

Views

### How to change the colour of bins in ggplot (geom_bin2d) to reflect difference between density in that area and the average density across a dataset?

Say I have some data that looks a bit like this
library(ggplot2)
library(dplyr)
employee

4

votes

3

answer

356

Views

### Python: Assigning # values in a list to bins, by rounding up

I want a function that can take a series and a set of bins, and basically round up to the nearest bin. For example:
my_series = [ 1, 1.5, 2, 2.3, 2.6, 3]
def my_function(my_series, bins):
...
my_function(my_series, bins=[1,2,3])
> [1,2,2,3,3,3]
This seems to be very close to what Numpy's Digitize...

3

votes

0

answer

456

Views

### Hexbin in R ggplot - hexagons get bigger if data is too sparse

I'm generating a series of hexbin plots for use in an animated GIF, and there are occasional frames that have a low density of data. The plots seem to create giant, misshapen hexagons.
Here is an example that works as expected:
library(ggplot2)
set.seed(23)
x

4

votes

1

answer

8.7k

Views

### Am I using histc wrong, or is this MATLAB's fault?

Ok, here's some code in MATLAB:
data = [1 1.5 2 3 4 4.5 5 6 7 7 7 0 0 0];
histc(data, [1:1:5])
histc(data, [1:1:5, inf])
histc(data, [-inf, 1:1:5])
which outputs the following:
ans = 2 1 1 2 1
ans = 2 1 1 2 5 0
ans = 3 2 1 1 2 1
My question is,...

2

votes

2

answer

43

Views

### Dimension issue when using .apply(value_counts, bin=x)

I am trying to apply value_counts to the following dataframe using bins
df2 = pd.DataFrame(np.random.randint(0,100,size=(1000, 4)), columns=list('ABCD'))
df2.apply(pd.value_counts, normalize=True, bins=[0,25,50,75,101]).sort_values(by=['A'], ascending=False)
However when I do this i get the followin...

1

votes

1

answer

1.7k

Views

### Increasing space between bins in seaborn distplot

So I have this, probably, simple question. I created a histogram from data out of an excel file with seaborn. Forbetter visualization, I would like to have some space between the bars/bins. Is that possible?
My code looks as followed
import pandas as pd
import matplotlib as mpl
import matplotlib.pyp...

4

votes

2

answer

2.1k

Views

### Plotting a choropleth map (with geopandas) using a user_defined classification scheme

I'm kind of new to python, so I'm hoping that the answer to my question is relatively straight forward.
I'm trying to make a choropleth map using geopandas. However, since I'm making multiple maps that need to be compared to each other, it is indispensable that I use a custom data classification sc...

3

votes

3

answer

5.2k

Views

### Histogram in logarithmic scale in gnuplot

I have to plot an histogram in logarithmic scale on both axis using gnuplot. I need bins to be equally spaced in log10. Using a logarithmic scale on the y axis isn't a problem. The main problem is creating the bin on the x axis. For example, using 10 bins in log10, first bins will be [1],[2],[3].......

2

votes

2

answer

1.2k

Views

### Creating 2D bins in R

I have coordinate data in R, and I would like to determine a distribution of where my points lie. The entire space of points is a square of side length 100.
I'd like to assign points to different segments on the square, for example rounded to the nearest 5. I've seen examples using cut and findinte...

2

votes

0

answer

26

Views

### How to make a weight array from a list of tuples in order to plot a histogram whose y axis is weighted?

I have a list of tuples as follows:
A=[(122208102.23250552, 34), (164096757.6449624, 4), (212275562.3177331, 72), (499344188.7213493, 240), (515347294.02090293, 2), (614044718.1056056, 4), (623878472.271997, 37), (1050993427.1862154, 2), (1885818969.4904015, 106), (2718942354.402217, 539), (27848384...