Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

How can I create a histogram from a file as follows. I can't seem to pick up the variable names from the file even.

\documentclass[tikz,border=12pt]{standalone}
\usepackage{filecontents}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}

\begin{filecontents*}{data.csv}
0,70.1
20,10
40,5.5
60,3.3
80,2.2
100,1.
120,1.
140,0.9
160,0.7
180,0.6
200,0.4
220,0.3
240,0.3
\end{filecontents*}

\begin{document}

\begin{tikzpicture}
\begin{axis}[ybar interval]
\addplot table [x, y, col sep=comma] {data.csv};
\end{axis}
\end{tikzpicture}

\end{document}

I want the histogram to display the first column on the xaxis with the percentages on the yaxis and have whatever amount of bins are in the file (i.e. amount of rows).

share|improve this question
    
what do you mean by 'mark the height'? Do you want a line plotted there? –  papabravo Feb 22 at 13:45
add comment

1 Answer

up vote 5 down vote accepted

This is how I understand your question:

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}

\begin{filecontents*}{data.csv}
0,70.1
20,10
40,5.5
60,3.3
80,2.2
100,1.0
120,1.0
140,0.9
160,0.7
180,0.6
200,0.4
220,0.3
240,0.3
\end{filecontents*}

\begin{document}


\begin{tikzpicture}
\begin{axis}
\addplot[ybar,fill] table [x, y, col sep=comma] {data.csv};
\addplot[domain=0:240] {70.1};
\end{axis}
\end{tikzpicture}

\end{document}

Results in enter image description here

share|improve this answer
    
I just edited the question to remove that red herring there. I can't seem get the output you have in your example. If I remove the \addplot[domain=0:240] {70.1}; line, the xaxis and yaxis ranges are from 1 to 5. It looks more like a bar chart. Do I have another version of pgfplots or something? –  user2146441 Feb 22 at 14:14
1  
any errors? Have you sanitized your data file (it had some errors in it) ? –  papabravo Feb 22 at 14:16
    
@user2146441 instead of [ybar,fill] use [ycomb] to get a histogram instead of a bar plot. –  Gonzalo Medina Feb 22 at 14:24
    
This works fine but is not a histogram yet. How can I put the bars flush with the yaxis and no space between like a histogram? For example, the first point (0,70.1) shouldn't have a bar at 0 on the xaxis, it should be a bar placed between 0 and 20 (in fact, the bar should be the point (0-20, 70)). How can I do this? –  user2146441 Feb 22 at 14:58
    
you can use the option you provided in the beginning: ybar interval instead of ybar –  papabravo Feb 22 at 15:35
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.