我一直想让这些条形图彼此相邻,而不是一个在另一个前面,但我一直没能搞清楚。以下是代码片段
\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}
数据文件
\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}
答案1
我建议您不要将这些条形图放在一起(这可能会产生误导),而应该opacity
使用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}