我需要十进制值(它们在代码中,但在图中。它们以整数形式获取)作为条形上方的值。此外,x 轴图例重叠,所以我需要将其放在 2 行中。我的意思是每个 x 图例都放在两行中。例如,对于“总时间”,时间应该放在下一行
\documentclass[border=10pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{width=7cm,compat=1.8}
\usepackage{pgfplotstable}
\renewcommand*{\familydefault}{\sfdefault}
\usepackage{sfmath}
\begin{document}
\begin{tikzpicture}
\centering
\begin{axis}[
ybar, axis on top,
title={cb},
height=8cm, width=15.5cm,
bar width=0.35cm,
ymajorgrids, tick align=inside,
major grid style={draw=white},
enlarge y limits={value=.1,upper},
ymin=0, ymax=30,
axis x line*=bottom,
axis y line*=right,
y axis line style={opacity=0},
tickwidth=0pt,
enlarge x limits=true,
legend style={
at={(0.5,0.6)},
anchor=north,
legend columns=-1,
/tikz/every even column/.append style={column sep=0.5cm}
},
ylabel={Computational time ($\mu$ s )},
symbolic x coords={
ramesh india, ramu england, rama america, raman poland, ram ukrain, ramani nedherlands, total time},
xtick=data,
nodes near coords={
\pgfmathprintnumber[precision=0]{\pgfplotspointmeta}
}
]
\addplot [draw=none, fill=blue!30] coordinates {
(ramesh india,8)
(ramu england, 1)
(rama america,5.5)
(raman poland,0)
(ram ukrain,0)
(ramani nedherlands,2)
(total time,16.5) };
\addplot [draw=none,fill=red!30] coordinates {
(ramesh india,8)
(ramu england, 1)
(rama america,5)
(raman poland,7.5)
(ram ukrain,3)
(ramani nedherlands,2)
(total time,25.5) };
\addplot [draw=none, fill=brown!30] coordinates {
(ramesh india,8)
(ramu england, 1)
(rama america,5)
(raman poland,7.5)
(ram ukrain,3)
(ramani nedherlands,2)
(total time,28.5) };
\legend{C, M, P}
\end{axis}
\end{tikzpicture}
\end{document}
答案1
你\pgfmathprintnumber[precision=0]{\pgfplotspointmeta}
说的是“打印这个数字时保留零个小数”。如果你说precision=1
,你会得到一个小数。正如你所发现的,通过全部删除,你会得到相同的结果[precision=0]
。
顺便说一句,如果您想要例如8.0
而不是8
,您将需要\pgfmathprintnumber[zerofill, precision=1]{\pgfplotspointmeta}
,但似乎您并不想要那个。
关于 xticklabels,您可以添加xticklabel style={align=center}
到选项中axis
,然后\\
在您想要换行的位置添加符号坐标。
我还将条形图做得更宽一些,以便为标签留出空间。
无关注释:\centering
在 a 里面放 atikzpicture
没有多大意义。 a 的大小tikzpicture
由内容决定, 应该\centering
在 之前\begin{tikzpicture}
。
\documentclass[border=10pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{width=7cm,compat=1.8}
\usepackage{pgfplotstable}
\renewcommand*{\familydefault}{\sfdefault}
\usepackage{sfmath}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
ybar, axis on top,
title={cb},
height=8cm, width=15.5cm,
bar width=0.45cm, % <-- modified
ymajorgrids, tick align=inside,
major grid style={draw=white},
enlarge y limits={value=.1,upper},
ymin=0, ymax=30,
axis x line*=bottom,
axis y line*=right,
y axis line style={opacity=0},
tickwidth=0pt,
enlarge x limits=true,
xticklabel style={align=center}, % <-- added
legend style={
at={(0.5,0.6)},
anchor=north,
legend columns=-1,
/tikz/every even column/.append style={column sep=0.5cm}
},
ylabel={Computational time ($\mu$ s )},
symbolic x coords={
ramesh\\india, ramu\\england, rama\\america, raman\\poland, ram\\ukrain, ramani\\nedherlands, total time},
xtick=data,
nodes near coords={
\pgfmathprintnumber{\pgfplotspointmeta}
}
]
\addplot [draw=none, fill=blue!30] coordinates {
(ramesh\\india,8)
(ramu\\england, 1)
(rama\\america,5.5)
(raman\\poland,0)
(ram\\ukrain,0)
(ramani\\nedherlands,2)
(total time,16.5) };
\addplot [draw=none,fill=red!30] coordinates {
(ramesh\\india,8)
(ramu\\england, 1)
(rama\\america,5)
(raman\\poland,7.5)
(ram\\ukrain,3)
(ramani\\nedherlands,2)
(total time,25.5) };
\addplot [draw=none, fill=brown!30] coordinates {
(ramesh\\india,8)
(ramu\\england, 1)
(rama\\america,5)
(raman\\poland,7.5)
(ram\\ukrain,3)
(ramani\\nedherlands,2)
(total time,28.5) };
\legend{C, M, P}
\end{axis}
\end{tikzpicture}
\end{document}
答案2
\documentclass[border=10pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{width=7cm,compat=1.8}
\usepackage{pgfplotstable}
\renewcommand*{\familydefault}{\sfdefault}
\usepackage{sfmath}
\begin{document}
\begin{tikzpicture}
\centering
\begin{axis}[
height=8cm, width=15.5cm,
ybar=1mm, axis on top,
title={cb},
bar width=5mm,
tick align=inside,
enlarge y limits={value=.1,upper},
ymin=0, ymax=30,
axis x line*=bottom,
axis y line*=right,
enlarge x limits=true,
legend style={
at={(0.5,0.6)},
anchor=north,
legend columns=-1,
/tikz/every even column/.append style={column sep=0.5cm}
},
ylabel={Computational time ($\mu$ s )},
symbolic x coords={
ramesh india, ramu england, rama america, raman poland, ram ukrain, ramani nedherlands, total time},
xtick=data,
x tick label style = {rotate=30, anchor=east, yshift=-2ex,
align=right, font=\footnotesize},
nodes near coords={
\pgfmathprintnumber[fixed zerofill, precision=1] % <---
{\pgfplotspointmeta}
},
nodes near coords style = {font=\footnotesize}
]
\addplot [draw=none, fill=blue!30] coordinates {
(ramesh india,8)
(ramu england, 1)
(rama america,5.5)
(raman poland,0)
(ram ukrain,0)
(ramani nedherlands,2)
(total time,16.5) };
\addplot [draw=none,fill=red!30] coordinates {
(ramesh india,8)
(ramu england, 1)
(rama america,5)
(raman poland,7.5)
(ram ukrain,3)
(ramani nedherlands,2)
(total time,25.5) };
\addplot [draw=none, fill=brown!30] coordinates {
(ramesh india,8)
(ramu england, 1)
(rama america,5)
(raman poland,7.5)
(ram ukrain,3)
(ramani nedherlands,2)
(total time,28.5) };
\legend{C, M, P}
\end{axis}
\end{tikzpicture}
\end{document}
生产: