我有这个用最小值(-ve)和最大值(+ve)绘制条形图的脚本
\documentclass[border=5mm]{standalone}
\usepackage{pgfplots}
\pgfplotstableread{
Year FarMin FarMax NearMin NearMax HereMin HereMax
1930 -20 50 -10 30 -15 40
1940 -10 60 -15 60 -20 70
1950 -15 78 -20 20 -32 42
1960 -20 30 -15 40 -20 10
1970 -5 30 -30 40 -15 20
}\datatable
\begin{document}%
\begin{tikzpicture}
\begin{axis}[
x tick label style={
/pgf/number format/1000 sep=},
ylabel=Population,
ybar,
enlarge x limits=0.15,
bar width=0.8em,
after end axis/.append code={
\draw ({rel axis cs:0,0}|-{axis cs:0,0}) -- ({rel axis cs:1,0}|-{axis cs:0,0});
}
]
\addplot +[forget plot] table {\datatable};
\addplot table [y index=2] {\datatable};
\addplot +[forget plot] table [y index=3] {\datatable};
\addplot table [y index=4] {\datatable};
\addplot +[forget plot] table [y index=5] {\datatable};
\addplot table [y index=6] {\datatable};
\legend{Far,Near,Here}
\end{axis}
\end{tikzpicture}
\end{document}
但是,当我想要绘制一个具有正最小值的图形时,我得到的图形从零开始而不是给定的正值。
\documentclass[border=5mm]{standalone}
\usepackage{pgfplots}
\pgfplotstableread{
Year FarMin FarMax NearMin NearMax HereMin HereMax
1930 20 50 10 30 15 40
1940 10 60 15 60 20 70
1950 15 78 20 20 32 42
1960 20 30 15 40 20 10
1970 5 30 30 40 15 20
}\datatable
\begin{document}%
\begin{tikzpicture}
\begin{axis}[
x tick label style={
/pgf/number format/1000 sep=},
ylabel=Population,
ybar,
enlarge x limits=0.15,
bar width=0.8em,
after end axis/.append code={
\draw ({rel axis cs:0,0}|-{axis cs:0,0}) -- ({rel axis cs:1,0}|-{axis cs:0,0});
}
]
\addplot +[forget plot] table {\datatable};
\addplot table [y index=2] {\datatable};
\addplot +[forget plot] table [y index=3] {\datatable};
\addplot table [y index=4] {\datatable};
\addplot +[forget plot] table [y index=5] {\datatable};
\addplot table [y index=6] {\datatable};
\legend{Far,Near,Here}
\end{axis}
\end{tikzpicture}
\end{document}
请帮我绘制带有正最小值的图表。谢谢!
答案1
仅作为一种解决方法:
\documentclass[border=5mm]{standalone}
\usepackage{pgfplotstable}
\usetikzlibrary{matrix}
\pgfplotsset{compat=1.14}
\pgfplotstableread{
Year FarMin FarMax NearMin NearMax HereMin HereMax
1930 20 50 10 30 15 40
1940 10 60 15 60 20 70
1950 15 78 20 20 32 42
1960 20 30 15 40 20 10
1970 5 30 30 40 15 20
}\datatable
\begin{document}%
\begin{tikzpicture}
\pgfplotsset{
every axis/.style={
ybar stacked,
enlarge x limits=0.15,
bar width=6pt,
ymin=0,ymax=80,
},
compat/bar nodes=1.8,
minimum/.style={forget plot,draw=none,fill=none}
}
\begin{axis}[
x tick label style={/pgf/number format/1000 sep=},
ylabel=Population,
bar shift=-8pt
]
\addplot [minimum] table {\datatable};
\addplot table [y expr=\thisrowno{2}-\thisrowno{1}] {\datatable};
\label{plot:Far}
\end{axis}
\begin{axis}[
axis lines=none,
bar shift=0pt,
cycle list shift=1
]
\addplot [minimum] table[y index=3] {\datatable};
\addplot table [y expr=\thisrowno{4}-\thisrowno{3}] {\datatable};
\label{plot:Near}
\end{axis}
\begin{axis}[
axis lines=none,
bar shift=8pt,
cycle list shift=2
]
\addplot [minimum] table[y index=5] {\datatable};
\addplot table [y expr=\thisrowno{6}-\thisrowno{5}] {\datatable};
\label{plot:Here}
\end{axis}
\matrix[
matrix of nodes,
draw,
anchor=north east,
nodes={inner ysep=.1em},
column 1/.style={nodes={anchor=center}},
column 2/.style={nodes={anchor=west}}
] at([shift={(-5pt,-5pt)}]current axis.north east)
{
\ref{plot:Far}&Far\\
\ref{plot:Near}&Near\\
\ref{plot:Here}&Here\\
};
\end{tikzpicture}
\end{document}