# How do I create a weighted histogram from a data file using pgfplots?

There is already a question with a good answer on how to create a histogram from a data file:

How to compute an histogram (plot bars) binning the values from a .csv using TeX?

I am in a similar situation, and I'm using essentially the same code as the approved answer in that question, but in my case, each data entry in the file also has a weight. I.e. I have a table with two columns: measure and weight. When computing the histogram bins, each data point in the measure column does not count with a frequency of 1, but rather a frequency defined by the weight column. I could not find any documentation on how to modify the frequency counting behavior of the hist plot to take these weights into account. Is this possible out of the box?

Here is the code I have so far:

\documentclass[border=5mm]{standalone}

\usepackage{pgfplots, pgfplotstable}
\usepgfplotslibrary{statistics}

\begin{document}
\begin{tikzpicture}
-0.928770 4.70650000e-08
-0.299800 0.00027664
1.2239000 0.00015706
-0.325590 0.00037748
-1.394800 0.00010935
-1.672700 0.00012530
-16.30100 8.08970000e-05
1.3411000 1.41510000e-05
-0.208830 3.22500000e-06
-20.56700 0.00030339
-5.851300 1.06270000e-05
-2.400000 7.91360000e-05
0.0274690 5.86580000e-05
-0.449670 0.00050059
-0.609090 0.00025153
6.4945000 0.00031208
-0.323260 6.17550000e-05
-0.731820 0.00011005
-0.101990 1.80110000e-05
-0.255170 6.37970000e-06
0.8010800 5.94810000e-05
-1.170300 0.00011584
-2.109700 6.93600000e-05
0.4504900 0.00039688
-3.959400 0.00027925
-17.01700 0.00034205
13.374000 8.05630000e-05
-0.783290 0.00065051
-0.209290 0.00029204
0.0253600 0.00068065
-0.316010 0.00049834
-0.284310 0.00056931
3.9096000 0.00011451
-6.210000 8.00440000e-05
-8.520500 0.00010193
-0.584620 9.61480000e-05
-0.481480 0.00051246
-9.908400 0.00030773
21.625000 5.33230000e-05
0.4616500 2.46780000e-05
-7.467300 0.00011440
-0.561220 1.86010000e-05
1.3631000 0.00010656
-0.021861 0.00023348
}\errdata

\begin{axis}[
ybar,
ymin=0]
hist={
bins=21,
data min=-82,
data max=82}]
table [y index=0] {\errdata};
\end{axis}
\end{tikzpicture}
\end{document}


Currently, this code does not take into account the second column, meaning each data point counts as 1 when computing the bin frequencies.

• Welcome to TeX.SX! Please post what you've got so far as MWE (in combination with a sample data file). – TeXnician Jun 1 '17 at 15:14
• Do you mean with weight "number of samples"? – Stefan Pinnow Jun 1 '17 at 16:03
• @StefanPinnow: The histogram is not precomputed in the data file, so it's not "number of samples in this bin". The data is measured from differently-sized patches on a surface, and the weight represents the surface area of the patch. Larger patches need to count more towards their bin, otherwise the histogram will not reflect the true continuous distribution that was measured. – toster Jun 2 '17 at 6:40