我试图在水平条形图上显示数字数据,但是当我尝试在 y 轴上添加字符串标签而不是数字时,它不起作用 - 我做错了什么?
代码
\documentclass{minimal}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\tikzstyle{every node}=[font=\large]
\begin{axis} [xbar = .05cm,
bar width = 12pt,
xmin = 0,
xmax = 4,
enlarge y limits = {abs = .8},
enlarge x limits = {value = .25, upper},
title={Execution time log scale}{\large},
xlabel = {time[ms]},
]
\addplot coordinates {(3.66,0) (3.66,1) (3.66,2) (2.55,3)};
\addplot coordinates {(1.35,0) (1.51,1) (1.37,2) (1.35,3)};
\legend{reference,author}
\end{axis}
\end{tikzpicture}
\end{document}
现在我尝试修改代码以使用符号名称
\documentclass{minimal}
\usepackage{pgfplots}
\begin{document}
\tikzstyle{every node}=[font=\large]
\begin{axis} [xbar = .05cm,
bar width = 12pt,
xmin = 0,
xmax = 4,
enlarge y limits = {abs = .8},
enlarge x limits = {value = .25, upper},
title={Execution time log scale}{\large},
xlabel = {time[ms]},
ymbolic y coords = {HD vs Monai,robust HD,mean HD,HD vs Oliviera}
]
\addplot coordinates {(3.66,HD vs Monai),(3.66,robust HD) (3.66,mean HD) (2.55,HD vs Oliviera)};
\addplot coordinates {(1.35,HD vs Monai) (1.51,robust HD) (1.37,mean HD) (1.35,HD vs Oliviera)};
\legend{reference,author}
\end{axis}
\end{tikzpicture}
\end{document}
这就是我得到的
答案1
有一个拼写错误,ymbolic y coords
应该是symbolic y coords
。你这里多了一个逗号,(3.66,HD vs Monai),(3.66,robust HD)
应该是(3.66,HD vs Monai) (3.66,robust HD)
。而且你不能使用enlarge y limits = {abs = .8}
符号 y 坐标。你必须给数字一个单位。例如enlarge y limits = {abs = .8cm}
\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\begin{document}
\begin{tikzpicture}
\tikzstyle{every node}=[font=\large]
\begin{axis} [xbar,
bar width = 12pt,
xmin = 0,
xmax = 4,
enlarge y limits={abs=0.8cm},
enlarge x limits = {value = .25, upper},
title={Execution time log scale}{\large},
xlabel = {time[ms]},
symbolic y coords = {HD vs Monai,robust HD,mean HD,HD vs Oliviera},
]
\addplot coordinates{
(3.66,HD vs Monai) (3.66,robust HD) (3.66,mean HD) (2.55,HD vs Oliviera)};
\addplot coordinates{
(1.35,HD vs Monai) (1.51,robust HD) (1.37,mean HD) (1.35,HD vs Oliviera)};
\legend{reference,author}
\end{axis}
\end{tikzpicture}
\end{document}