![水平条形图标题和符号标签调整](https://linux22.com/image/414959/%E6%B0%B4%E5%B9%B3%E6%9D%A1%E5%BD%A2%E5%9B%BE%E6%A0%87%E9%A2%98%E5%92%8C%E7%AC%A6%E5%8F%B7%E6%A0%87%E7%AD%BE%E8%B0%83%E6%95%B4.png)
我创建了一个具有一些自定义的水平条形图,但遇到了一些问题:
- 如何正确对齐符号标签?
- 如何自动将标题置于图形中央?我猜标题位于条形图中央,而不是整个图形中央。(创建了一个简单的宏来设计边界框,以便更好地进行视觉检查)
- 如何在图例上放置 3 个点(\ldots 对我来说太宽了)?
这是一张图片和一个 MWE:
\documentclass{article}
\usepackage{pgfplots}
\begin{document}
\begin{figure}[H]
\newcommand{\SSSa}[0]{{Feeling active, vital, alert or wide awake}}
\newcommand{\SSSb}[0]{{Functioning at high levels, but not at peak; able to concentrate}}
\newcommand{\SSSc}[0]{{Awake, but relaxed; responsive but not fully alert}}
\newcommand{\SSSd}[0]{{Somewhat foggy, let down}}
\newcommand{\SSSe}[0]{{Foggy; losing interest in remaining awake; slowed down}}
\newcommand{\SSSf}[0]{{Sleepy, woozy, fighting sleep; prefer to lie down}}
\newcommand{\SSSg}[0]{{No longer fighting sleep, sleep onset soon; having dream-like thoughts}}
\newcommand{\myBoundingBox}[0]{(current bounding box.north east) -- (current bounding box.north west) -- (current bounding box.south west) -- (current bounding box.south east) -- cycle;}
\centering
\begin{tikzpicture}%[scale=0.7]
\pgfplotsset{plot coordinates/math parser=false}
\begin{axis}[title = Degree of Sleepiness,
%every axis title/.style={below right,at={(0,1)}},
%title style={at={(1,0)}, anchor=},
xbar,
y axis line style = {opacity=0,draw=none},
axis x line = none,
axis y line = right,
tickwidth = 0pt,
%bar width = 5pt,
%enlarge y limits = 0.2,
%enlarge x limits = 0.02,
enlargelimits=true,
symbolic y coords = {\SSSa,\SSSb,\SSSc,\SSSd,\SSSe,\SSSf},
ytick distance=1.1,
%xmin=0,
y tick label style={font=\footnotesize,text width=6cm,align=left},
nodes near coords,
legend cell align={right},
legend style={at={(0.2,0.925)}, anchor=north},
x dir=reverse,
every node near coord/.append style={shift={(axis direction cs:0.7,0)}}
]
\addplot coordinates{%
(1,\SSSa)
(4,\SSSb)
(8,\SSSc)
(1,\SSSd)
(3,\SSSe)
(0,\SSSf)
};
\addplot coordinates{%
(1,\SSSa)
(2,\SSSb)
(8,\SSSc)
(2,\SSSd)
(3,\SSSe)
(1,\SSSf)
};
\legend{Before experiment,After experiment}
\end{axis}
\draw \myBoundingBox
\end{tikzpicture}
\caption{Some caption.}
\label{fig:somelabel}
\end{figure}
\end{document}
背面也有一些错误:
! Package pgfplots Error: Sorry, the input coordinate `0' has not been defined
with 'symbolic y coords={\SSSa ,\SSSb ,\SSSc ,\SSSd ,\SSSe ,\SSSf }... Maybe it
has been misspelled? Or did you mean something like [normalized]0?.
答案1
您使用了很多手动换档。所有这些都可以通过 避免ytick=data
。错误也通过这种方式得到修复。这解决了主要问题,即错误,以及您列表中的第 1 点。由于我是每个问题一个问题的粉丝,所以我就此打住,更重要的是,我认为 Torbjørn T. 在最近的一个答案中已经解决了第 2 点,我对此表示赞同,但再也找不到了。第 3 点我不明白,但我想请您将问题分开。
\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.17}
\begin{document}
\begin{figure}%[H]
\newcommand{\SSSa}[0]{{Feeling active, vital, alert or wide awake}}
\newcommand{\SSSb}[0]{{Functioning at high levels, but not at peak; able to concentrate}}
\newcommand{\SSSc}[0]{{Awake, but relaxed; responsive but not fully alert}}
\newcommand{\SSSd}[0]{{Somewhat foggy, let down}}
\newcommand{\SSSe}[0]{{Foggy; losing interest in remaining awake; slowed down}}
\newcommand{\SSSf}[0]{{Sleepy, woozy, fighting sleep; prefer to lie down}}
\newcommand{\SSSg}[0]{{No longer fighting sleep, sleep onset soon; having dream-like thoughts}}
\newcommand{\myBoundingBox}[0]{(current bounding box.north east) -- (current bounding box.north west) -- (current bounding box.south west) -- (current bounding box.south east) -- cycle;}
\centering
\begin{tikzpicture}%[scale=0.7]
%\pgfplotsset{plot coordinates/math parser=false}
\begin{axis}[title = Degree of Sleepiness,
xbar,width=0.55\textwidth,height=0.6\textwidth,
y axis line style = {opacity=0,draw=none},
axis x line = none,
axis y line = right,
tickwidth = 0pt,
%bar width = 5pt,
%enlarge y limits = 0.2,
%enlarge x limits = 0.02,
enlargelimits=true,
symbolic y coords = {\SSSa,\SSSb,\SSSc,\SSSd,\SSSe,\SSSf},
ytick distance=1.1,
ytick=data,
%xmin=0,
yticklabel style={font=\footnotesize,text width=6cm,align=left},
%point meta=rawx,
nodes near coords,
nodes near coords style={anchor=east},
legend cell align={right},
legend style={at={(0.2,1.02)}, anchor=north},
x dir=reverse,
%every node near coord/.append style={shift={(axis direction cs:0.7,0)}}
]
\addplot coordinates{(1,\SSSa)
(4,\SSSb)
(8,\SSSc)
(1,\SSSd)
(3,\SSSe)
(0,\SSSf)
};
\addplot coordinates{(1,\SSSa)
(2,\SSSb)
(8,\SSSc)
(2,\SSSd)
(3,\SSSe)
(1,\SSSf)
};
\legend{Before experiment,After experiment}
\end{axis}
\draw \myBoundingBox
\end{tikzpicture}
\caption{Some caption.}
\label{fig:somelabel}
\end{figure}
\end{document}