2

I've been trying to get these bars next to each other rather than one in front of the other but I haven't been able to figure it out. Here's a snippet of the code

\begin{tikzpicture}
\begin{axis}[
grid=major,
ymin=0,
xlabel=Number of Deaths,
ylabel=Frequency,
width=12cm,
height=8cm,
ybar
]
\addplot +[
hist={
    bins=15,
    data min=120,
    data max=230
}   
] table [y index=0,col sep=comma] {data1999.csv};

\addplot +[
hist={
    bins=15,
    data min=120,
    data max=230
}   
] table [y index=0] {data2014.csv};
\legend{1999,2014}
\end{axis}
\end{tikzpicture}

Data Files

\begin{filecontents}{data1999.csv}
x1
126
134
145
146
148
154
155
156
156
156
158
159
160
160
161
163
164
164
167
167
167
169
170
171
172
174
176
177
177
177
177
178
178
178
179
179
180
181
181
181
181
182
182
183
186
186
187
192
194
195
216
\end{filecontents}


\begin{filecontents}{data2014.csv}
x2
131.3
143.7
152.8
155.2
155.5
160.3
162.8
164.8
166.3
166.4
166.6
167.8
168.4
168.4
168.9
169.7
170.7
174.3
174.7
175
175
175.6
176.3
177.3
177.5
177.6
177.9
179
179
179.2
179.3
182.5
182.8
183.9
184.5
185.1
187.6
187.7
187.7
189.2
192.4
192.6
193.3
196.4
198
199
199.1
201.2
201.3
203.5
209.5
\end{filecontents}
3

Instead of placing the bars next to each other (which could be misleading), I'd suggest you to use opacity of fill opacity:

\documentclass{article}
\usepackage{pgfplots}

\usepackage{filecontents}
\begin{filecontents*}{data1999.csv}
x1
126
134
145
146
148
154
155
156
156
156
158
159
160
160
161
163
164
164
167
167
167
169
170
171
172
174
176
177
177
177
177
178
178
178
179
179
180
181
181
181
181
182
182
183
186
186
187
192
194
195
216
\end{filecontents*}


\begin{filecontents*}{data2014.csv}
x2
131.3
143.7
152.8
155.2
155.5
160.3
162.8
164.8
166.3
166.4
166.6
167.8
168.4
168.4
168.9
169.7
170.7
174.3
174.7
175
175
175.6
176.3
177.3
177.5
177.6
177.9
179
179
179.2
179.3
182.5
182.8
183.9
184.5
185.1
187.6
187.7
187.7
189.2
192.4
192.6
193.3
196.4
198
199
199.1
201.2
201.3
203.5
209.5
\end{filecontents*}
\begin{document}

\begin{tikzpicture}
\begin{axis}[
grid=major,
ymin=0,
xlabel=Number of Deaths,
ylabel=Frequency,
width=12cm,
height=8cm,
ybar
]
\addplot +[
red!60,
fill opacity=0.4,
hist={
    bins=15,
    data min=120,
    data max=230
}   
] table [y index=0,col sep=comma] 
{data1999.csv};
\addplot +[
blue!60,
fill opacity=0.4,
hist={
    bins=15,
    data min=120,
    data max=230
}   
] table [y index=0] 
{data2014.csv};
\legend{1999,2014}
\end{axis}
\end{tikzpicture}

\end{document}

enter image description here

  • Thank you for the help! Would it be possible to put them side by side? I think the point I'm trying to make would be clearer if the data was put side by side. – JTian Sep 12 '15 at 4:08
  • @JTian Wouldn't it be misleading to place the bars side by side? – Gonzalo Medina Sep 12 '15 at 4:16
  • Oh, I see what you mean now. Yes that would be misleading. Thank you for your help! I appreciate it – JTian Sep 12 '15 at 4:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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