我想在条形图中画一条水平线。但是,我得到的结果并不令人满意(如下图所示)。
我不知道如何修改代码才能达到以下效果:
- 删除两边的节点值3.0;
- 在线的下方中央添加一个‘中性’字样(图中字样为我自己添加的);
- 将线延伸至两侧边界。
有人能帮助我吗?
\begin{filecontents*}{data.csv}
x,ave,med
Launch,3.93,4
Options,4,4
Text Input,4.38,4
Text Display,3.35,3.5
Fixed-item List,3.44,3
Dynamic-item List,4.32,4
\end{filecontents*}
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{pgfplots}
% \usepgfplotslibrary{statistics}
\pgfplotsset{compat=1.14}
% \usepackage{tikz}
\usetikzlibrary{patterns}
\begin{document}
\begin{figure}[tb]
\centering
\hspace*{-0.32cm}
\begin{tikzpicture}[font=\scriptsize]
\begin{axis}[ybar,
height=4.5cm,width=9cm,
ymin=0,ymax=6,
ylabel style = {align=center},
ylabel={\# relevant score},
ytick={1,...,5},
symbolic x coords={Launch,Options,Text Input,
Text Display,Fixed-item List,Dynamic-item List},
xticklabels={,Launch,Options,Text Input,
Text\\ Display,Fixed-\\item List,Dynamic-\\item List},
xticklabel style={align=center},
nodes near coords,
nodes near coords align={vertical},
nodes near coords style={/pgf/number format/fixed,/pgf/number format/fixed zerofill,/pgf/number format/precision=2},
every node near coord/.append style={font=\scriptsize},
]
\addplot table [x=x,y=ave,col sep=comma]{data.csv};
\addplot[red,line legend,sharp plot,
update limits=false] coordinates {(Launch,3) (Dynamic-item List,3)};
\end{axis}
\begin{axis}[
axis x line={none},
ymin=0,ymax=6,
ytick={1,...,5},
height=4.5cm,width=9cm,
]
\addplot[only marks,mark=*,mark size=2pt,magenta,
] table [x expr=\coordindex,y=med,col sep=comma]{data.csv};
\end{axis}
\end{tikzpicture}
\end{figure}
\end{document}
答案1
怎么样
\begin{filecontents*}{data.csv} x,ave,med 启动,3.93,4 选项,4,4 文本输入,4.38,4 文本显示,3.35,3.5 固定项目列表,3.44,3 动态项目列表,4.32,4 \end{filecontents*}
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{pgfplots}
% \usepgfplotslibrary{statistics}
\pgfplotsset{compat=1.14}
% \usepackage{tikz}
\usetikzlibrary{patterns}
\begin{document}
\begin{figure}[tb]
\centering
\hspace*{-0.32cm}
\begin{tikzpicture}[font=\scriptsize]
\begin{axis}[ybar,
height=4.5cm,width=9cm,
ymin=0,ymax=6,
ylabel style = {align=center},
ylabel={\# relevant score},
ytick={1,...,5},
symbolic x coords={Launch,Options,Text Input,
Text Display,Fixed-item List,Dynamic-item List},
xticklabels={,Launch,Options,Text Input,
Text\\ Display,Fixed-\\item List,Dynamic-\\item List},
xticklabel style={align=center},
nodes near coords,
nodes near coords align={vertical},
nodes near coords style={/pgf/number format/fixed,/pgf/number format/fixed zerofill,/pgf/number format/precision=2},
every node near coord/.append style={font=\scriptsize},
]
\addplot table [x=x,y=ave,col sep=comma]{data.csv};
\addplot[red,line legend,sharp plot,nodes near coords={},
update limits=false,shorten >=-3mm,shorten <=-3mm]
coordinates {(Launch,3) (Dynamic-item List,3)}
node[midway,below,font=\bfseries\sffamily]{neutral};
\end{axis}
\begin{axis}[
axis x line={none},
ymin=0,ymax=6,
ytick={1,...,5},
height=4.5cm,width=9cm,
]
\addplot[only marks,mark=*,mark size=2pt,magenta,
] table [x expr=\coordindex,y=med,col sep=comma]{data.csv};
\end{axis}
\end{tikzpicture}
\end{figure}
\end{document}