问题描述
我希望每个条形的节点都是实际值及其对应的百分比,但在 Y 轴上我希望有一个值的比例。使用我当前的 MWE,我得到了以下输出:
梅威瑟:
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{pgf-pie}
\usepackage[margin=0.5in]{geometry}
\usepackage{textcomp}
\usepackage{pgfplots}
\pgfplotsset{width=10cm,compat=1.9}
\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\begin{document}
\pgfplotstableread[col sep=comma,header=false]{
1,15,5
2,9,11
3,3,6
4,5,6
}\data
\pgfplotsset{
percentage plot/.style={
point meta=explicit,
every node near coord/.append style={
align=center,
text width=1cm
},
nodes near coords={
\pgfmathtruncatemacro\iszero{\originalvalue==0}
\ifnum\iszero=0
\pgfmathprintnumber{\originalvalue}$\,\%$\\ \pgfmathprintnumber[fixed zerofill,precision=1]{\pgfplotspointmeta}
\fi},
nodes near coords align=vertical,
yticklabel=\pgfmathprintnumber{\tick}\,$\%$,
ymin=0,
ymax=100,
enlarge y limits={upper,value=0.18},
visualization depends on={y \as \originalvalue}
},
}
\begin{tikzpicture}
\begin{axis}[
axis on top,
width=10cm,
title = Graph,
ybar,
percentage plot,bar width=0.75cm,
enlarge x limits=0.25,
symbolic x coords={1,2,3,4},
xtick=data,
xlabel=Response value,
ylabel=Number of responses,
legend style={
at={(0.5,-0.2)},
anchor=north,
column sep=1ex}
]
\addplot coordinates {(1,47)[15] (2,28)[9] (3,9)[3] (4,16)[5]};
\addplot coordinates {(1,18)[5] (2,39)[11] (3,21)[6] (4,21)[6]};
\legend{Text 1, Text 2}
\end{axis}
\end{tikzpicture}
\end{document}
期望的输出/解决方案
我希望 Y 轴显示值 1、2、...、15,而不是百分比。
答案1
一个选项是交换图中的 y 值和元值。例如
\addplot coordinates {(1,47)[15] (2,28)[9] (3,9)[3] (4,16)[5]};
你有
\addplot coordinates {(1,15)[47] (2,9)[28] (3,3)[9] (4,5)[16]};
对于每个坐标,(x,y)[meta]
y 值和元值都会交换。
并且设置nodes near coords
有
\pgfmathprintnumber{\pgfplotspointmeta}$\,\%$\\ \pgfmathprintnumber[fixed zerofill,precision=1]{\originalvalue}
代替
\pgfmathprintnumber{\originalvalue}$\,\%$\\ \pgfmathprintnumber[fixed zerofill,precision=1]{\pgfplotspointmeta}
您可以看到两者中的\pgfplotspointmeta
和进行了交换。\originalvalue
我刚刚注意到我忘了更改ylabel
,所以你需要修复它。
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[margin=0.5in]{geometry}
\usepackage{textcomp}
\usepackage{pgfplotstable} % loads pgfplots which loads tikz
\pgfplotsset{width=10cm,compat=1.9}
\begin{document}
\pgfplotstableread[col sep=comma,header=false]{
1,15,5
2,9,11
3,3,6
4,5,6
}\data
\pgfplotsset{
percentage plot/.style={
point meta=explicit,
every node near coord/.append style={
align=center,
text width=1cm
},
nodes near coords={
\pgfmathtruncatemacro\iszero{\originalvalue==0}
\ifnum\iszero=0
\pgfmathprintnumber{\pgfplotspointmeta}$\,\%$\\ \pgfmathprintnumber[fixed zerofill,precision=1]{\originalvalue}
\fi},
nodes near coords align=vertical,
% yticklabel=\pgfmathprintnumber{\tick}\,$\%$,
ymin=0,
% ymax=100,
enlarge y limits={upper,value=0.18},
visualization depends on={y \as \originalvalue}
},
}
\begin{tikzpicture}
\begin{axis}[
axis on top,
width=10cm,
title = Graph,
ybar,
percentage plot,bar width=0.75cm,
enlarge x limits=0.25,
symbolic x coords={1,2,3,4},
xtick=data,
xlabel=Response value,
ylabel=Number of responses,
legend style={
at={(0.5,-0.2)},
anchor=north,
column sep=1ex}
]
\addplot coordinates {(1,15)[47] (2,9)[28] (3,3)[9] (4,5)[16]};
\addplot coordinates {(1,5)[18] (2,11)[39] (3,6)[21] (4,6)[21]};
\legend{Text 1, Text 2}
\end{axis}
\end{tikzpicture}
\end{document}