# Histogram in LaTeX with random x axis values

I am trying to create a histogram in LaTeX for the following coordinates with random values

(22,7041) (3,299) (4,299) (5,305) (6,64) (7,127) (8,469) (9,279) (10,237) (11,267) (12,257) (13,200) (14,125) (15,54) (16,2554) (17,126) (18,3974) (19,3271) (20,2461) (21,110) (2,236) (23,545) (24,151)

using the following code

\begin{tikzpicture}
\begin{axis}[ybar, ymax=8000,ymin=0, minor y tick num = 3, xticklabel style={rotate=90},
symbolic x coords={14033,483,490,501,101,253,819,431,347,408,380,288,192,107,5092,251,6984,5614,4522,172,471,1085,301},
bar width = 4
]
\addplot coordinates {(14033,7041) (483,299) (490,299) (501,305) (101,64) (253,127) (819,469) (431,279) (347,237) (408,267) (380,257) (288,200) (192,125) (107,54) (5092,2554) (251,126) (6984,3974) (5614,3271) (4522,2461) (172,110) (471,236) (1085,545) (301,151)};

\end{axis}
\end{tikzpicture}


Figure given below is the one that I generated in excel and wants to generate using latex.

It is easy to draw histogram for the given values in Excel but couldn't do in histogram. Any help will be highly appreciated.

• Welcome to TeX.SX! A tip: If you indent lines by 4 spaces, they'll be marked as a code sample. You can also highlight the code and click the "code" button (with "{}" on it). You don't have to sign with your name since it automatically appears in the lower right corner of your post. Jan 1 '17 at 13:30
• I'm probably ignorant, but what output do you expect? Perhaps you could show a screenshot of what Excel does. If you use ybar instead of ybar interval, is that closer to what you want? Jan 1 '17 at 15:29

It appears from the image that you want the x-values to be unsorted. So what you can do is plot a ybar using a dummy x-coordinate (0,1,...), and add custom ticklabels. I do this below by first saving the data to a text file (which makes things easier), and use \addplot table[x expr=\coordindex,y=y] {filename}; instead of \addplot coordinates. The x expr statement means that the x-value will be the index of the data point in the table, and the y-value will be the column with the header y.

The ticklabels are modified by adding

xticklabels from table={data.dat}{x},
xtick=data,


to the axis options. The second means a tick is added at every data point.

Below I used the filecontents* environment to save the data file, but that is just to make the example self-contained. You can produce the file however you like (e.g. export from Excel), without having to clutter your preamble.

\documentclass[border=4mm]{standalone}
\usepackage{filecontents}
% the filecontents environment saves its content to the specified file (data.dat)
\begin{filecontents*}{data.dat}
x  y
347  237
408  267
380  257
288  200
192  125
107  54
5092  2554
251  126
6984  3974
5614  3271
14033  7041
4522  2461
172  110
471  236
1085  545
301  151
483  299
490  299
501  305
101  64
253  127
819  469
431  279
\end{filecontents*}
\usepackage{pgfplots}
\pgfplotsset{compat=1.14}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
width=12cm,
height=6cm,
ybar,
bar width=0.7,
ymin=0,
ytick={0,1000,2000,3000,4000,5000,6000,7000,8000},
xticklabel style={rotate=45,font=\footnotesize},
xticklabels from table={data.dat}{x}, % use the x column from the file for ticklabels
xtick=data, % add a tick at every data point,
enlarge x limits=0.03, % adjust space between axis edge and plot edge
xlabel=Number of something else,
ylabel=Number of something
]
\addplot table[x expr=\coordindex,y=y] {data.dat}; % \coordindex runs from 0 to N (number of datapoints in table/file
\end{axis}
\end{tikzpicture}
\end{document}

• Valuable your work. Jan 2 '17 at 13:35