I am new to tikz and pgf plots. I want to draw a bar chart with a mixed positive and negative values.
I want to display all the values (both positive and negative) in a single figure with single axis.
It means the values greater than 0 and less than 0 in the y-axis need to be displayed in the same figure itself. I have tried but the negative values is not displayed in the bar chart.
The MWE is as follows.
\documentclass[10pt,a4paper,twoside]{article}
\usepackage[usenames,dvipsnames]{xcolor}
\usepackage{pgfplots}
\usepackage{textcomp}
\usepackage{subcaption}
\usepackage{caption}
\usepackage{latexsym}
\pgfplotsset{compat=1.3}
\usepackage[justification=raggedright]{caption}
\pgfplotsset{
bar group size/.style 2 args={
/pgf/bar shift={%
-0.5*(#2*\pgfplotbarwidth + (#2-1)*\pgfkeysvalueof{/pgfplots/bar group skip}) +
(.5+#1)*\pgfplotbarwidth + #1*\pgfkeysvalueof{/pgfplots/bar group skip}},%
},
bar group skip/.initial=2pt,
plot 0/.style={blue,fill=blue!30!white,mark=none},%
plot 1/.style={red,fill=red!30!white,mark=none},%
plot 2/.style={brown!60!black,fill=brown!30!white,mark=none},%
}
\begin{document}
\begin{figure}
\small
\centering
\begin{tikzpicture}
\centering
\begin{axis}[
ybar,
width=11cm,
height=8cm,
legend style={area legend, at={(0.55,1)},
anchor=north,legend columns=-1},
xtick=data,
symbolic x coords={N \textrightarrow N+T, S \textrightarrow S+T, P \textrightarrow P+T, E \textrightarrow E+T, H \textrightarrow H+T, W\_H \textrightarrow W\_H+T},
x tick label style ={rotate=45, anchor=east, align=right,text width=2.6cm},
xlabel={Models},
enlarge x limits=0.15,
tick label style={},
ylabel={Percentage difference at nDCG@1},
every axis y label/.style={rotate=90,at={(-0.12,0.5)}},
bar width=5pt,
]
\addplot[ybar,black,fill=blue] coordinates {(N \textrightarrow N+T,100) (S \textrightarrow S+T,200) (P \textrightarrow P+T,-120) (E \textrightarrow E+T,150) (H \textrightarrow H+T,155) (W\_H \textrightarrow W\_H+T,267)};
\addplot[ybar,black,fill=pink] coordinates {(N \textrightarrow N+T,100) (S \textrightarrow S+T,-200) (P \textrightarrow P+T,200) (E \textrightarrow E+T,388) (H \textrightarrow H+T,155) (W\_H \textrightarrow W\_H+T,-267)};
\addplot[ybar,black,fill=violet] coordinates {(N \textrightarrow N+T,100) (S \textrightarrow S+T,-200) (P \textrightarrow P+T,-200) (E \textrightarrow E+T,388) (H \textrightarrow H+T,155) (W\_H \textrightarrow W\_H+T,267)};
\legend{\footnotesize TF-IDF,\footnotesize BM25,\footnotesize PL2}
\end{axis}
\end{tikzpicture}
\captionsetup{width=.95\textwidth}
\caption{Performance differences with transliteration.}
\label{isffig14}
\end{figure}
\end{document}
答案1
只是为了避免一些误解:这是我使用您的代码得到的结果,并且只是在 y=0 处添加了一行。
% used PGFPlots v1.15
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.3}
\begin{document}
\small
\begin{tikzpicture}
\begin{axis}[
ybar,
width=11cm,
height=8cm,
legend style={
area legend,
at={(0.55,1)},
anchor=north,
legend columns=-1,
font=\footnotesize, % <-- moved here from each legend entry
},
xtick=data,
symbolic x coords={
N \textrightarrow N+T,
S \textrightarrow S+T,
P \textrightarrow P+T,
E \textrightarrow E+T,
H \textrightarrow H+T,
W\_H \textrightarrow W\_H+T
},
x tick label style={
rotate=45,
anchor=east,
align=right,
text width=2.6cm,
},
xlabel={Models},
ylabel={Percentage difference at nDCG@1},
bar width=5pt,
]
\addplot [black,fill=blue] coordinates {
(N \textrightarrow N+T,100)
(S \textrightarrow S+T,200)
(P \textrightarrow P+T,-120)
(E \textrightarrow E+T,150)
(H \textrightarrow H+T,155)
(W\_H \textrightarrow W\_H+T,267)
};
\addplot [black,fill=pink] coordinates {
(N \textrightarrow N+T,100)
(S \textrightarrow S+T,-200)
(P \textrightarrow P+T,200)
(E \textrightarrow E+T,388)
(H \textrightarrow H+T,155)
(W\_H \textrightarrow W\_H+T,-267)
};
\addplot [black,fill=violet] coordinates {
(N \textrightarrow N+T,100)
(S \textrightarrow S+T,-200)
(P \textrightarrow P+T,-200)
(E \textrightarrow E+T,388)
(H \textrightarrow H+T,155)
(W\_H \textrightarrow W\_H+T,267)
};
% ---------------------------------------------------------------
% draw a line at $y = 0$
\draw (axis cs:{[normalized]\pgfkeysvalueof{/pgfplots/xmin}},0)
-- (axis cs:{[normalized]\pgfkeysvalueof{/pgfplots/xmax}},0);
% ---------------------------------------------------------------
\legend{
TF-IDF,
BM25,
PL2,
}
\end{axis}
\end{tikzpicture}
\end{document}