我有一个简单的情节,包含两行:
\nextgroupplot[
no markers,
ymin=-0.25,
ymax=2.25,
ytick={0, 1, 2},
yticklabels={state one, other state ,third state},
]
\addplot+ [
const plot mark right,
% name this path to later be able to find an intersection on it
name path=first,
] coordinates { (0,0) (200,0) (300,1) (500,2) };
\addplot+ [
const plot mark right,
% name this path to later be able to find an intersection on it
name path=second,
] coordinates { (0,1) (150,0) (250,1) (250,2) (450,2) (500, 1) };
看起来像:
我希望当它们具有相同状态时,这些线条堆叠在一起(使线条看起来变得更粗):
(如果有 100 条线,它会变得更高,如果线条更粗,它看起来就像彩虹线 =))
如何使用 tikz 和 pgf 来实现?
答案1
我想不会有自动的在 LaTeX(及其同类产品)中,跟踪线条重叠的时间,并仅在重叠区域相应地移动它们。这就是为什么建议只移动所有的線。
有关更多详细信息,请查看代码中的注释。
% used PGFPlots v1.14
% (this is an answer to the follow-up question of
% <http://tex.stackexchange.com/q/357358>)
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
width=10cm,
height=2cm,
scale only axis,
axis lines=left,
xmin=0,
xmax=500,
no markers,
ymin=-0.25,
ymax=2.25,
ytick={0, 1, 2},
yticklabels={state one, other state ,third state},
% (moved common `\addplot' options here)
const plot mark right,
]
% -----------------------------------------------------------------
% for now please ignore these commands.
% (Later it will be referenced and then you can/should read the
% comments on these commands)
% -----------------------------------------------------------------
% define a variable to count the number of `\addplot's
% and initialize it here to zero
\pgfmathtruncatemacro{\PlotNum}{0}
% -----------------------------------------------------------------
\addplot+ [
% -----------------------------------------------------------------
% please skip this option too, if you first come here
% -----------------------------------------------------------------
% of course this option should be used at *every* `\addplot'
% command, also the first one. This has the advantage that you
% could also apply a negative number to the initial value of
% `\PlotNum', so that the "middle" `\addplot' command is at the
% same height as the corresponding `yticks'
yshift=\PlotNum*\pgflinewidth,
% -----------------------------------------------------------------
] coordinates { (0,0) (200,0) (300,1) (400,2) (500,0) };
% now just increase the value by one each time a new `addplot' follows
\pgfmathtruncatemacro{\PlotNum}{\PlotNum+1}
\addplot+ [
% when you don't change the linewidth, you can simply shift the
% plot by the (default/set) linewidth
yshift=\pgflinewidth,
% % (for the default linewidth `thin' this would be 0.4pt, as is
% % written in the pgfmanual (v3.0.1a) in section 15.3.1 (on page 166))
% yshift=0.4pt,
] coordinates { (0,1) (150,0) (450,2) (500, 1) };
\pgfmathtruncatemacro{\PlotNum}{\PlotNum+1}
\addplot+ [
green,
% % with changing `linewidth' one would have to do a manual approach
% % like the following ...
% % -----
% thick,
% % half linewidth of the first `\addplot'
% % + sum of all linewidth of the "middle" `\addplot's
% % + half the linewidth of this `\addplot'
% yshift=0.2pt + 0.4pt + 0.4pt,
% -----------------------------------------------------------------
% % ... but if the `linewidth' doesn't change you can simply do ...
% yshift=\plotnum*\pgflinewidth,
% ... which unfortunately doesn't work, because in this context
% `\plotnum' doesn't seem to be not known. This is, why we simulate
% this by defining `\PlotNum' which can be found before each
% `\addplot' command and use it here
yshift=\PlotNum*\pgflinewidth,
] coordinates { (0,1) (100,0) (250,1) (350,2) (500, 0) };
\end{axis}
\end{tikzpicture}
\end{document}