![线帽延伸到tikz pgfplot中的网格和线轴上](https://linux22.com/image/444294/%E7%BA%BF%E5%B8%BD%E5%BB%B6%E4%BC%B8%E5%88%B0tikz%20pgfplot%E4%B8%AD%E7%9A%84%E7%BD%91%E6%A0%BC%E5%92%8C%E7%BA%BF%E8%BD%B4%E4%B8%8A.png)
我遇到了一个问题,但尚未找到解决方案。我正在创建一个情节:
\documentclass[tikz, class=scrreprt, fontsize=10pt]{standalone}
\usepackage[T1]{fontenc}
\usepackage{libertine}
\usepackage[libertine]{newtxmath}
\usepackage[stretch=10, shrink=10, final]{microtype}
\usepackage[detect-family, detect-weight]{siunitx}
\usepackage{pgfplots}
\usetikzlibrary{shapes, arrows.meta, positioning, calc, decorations.pathreplacing, calligraphy, intersections, backgrounds}
\tikzset{every picture/.style={/utils/exec={\sffamily\normalsize}}}
\definecolor{dark}{RGB}{215,48,31} % dunkelrot
\definecolor{yellow}{RGB}{253,204,138} % gelb
\definecolor{light}{RGB}{254,240,217} % hellgelb
\begin{document}
\begin{tikzpicture}
\pgfplotsset{every axis/.append style={
compat = 1.18,
width = 20cm,
height = 10cm,
xmajorgrids,
ymajorgrids,
axis y line = left,
axis x line = bottom,
x axis line style = {line width = 1pt, line cap = round, -{Triangle[width = 6pt, length = 6pt, round, line width = 1pt]}, shorten >= -0.75cm},
y axis line style = {line width = 1pt, line cap = round, -{Triangle[width = 6pt, length = 6pt, round, line width = 1pt]}, shorten >= -0.75cm},
grid style = {line width = 0.2pt, draw = light, opacity = 0.5, line cap = round},
legend style = {draw = black, line width = 0.5pt, legend cell align = left, at = {(1, 1)}, anchor = north east}
}}
\begin{axis}[%
xmin = 115, xmax = 135,
ymin = -1.2, ymax = 1,
xtick = {115, 120, 125, 130, 135},
ytick = {-1.2, -1, 0, 1},
xticklabels = {0, 5, 10, 15, 20},
yticklabels = {\hphantom{$\mathsf{-20}$}, $\mathsf{-1}$, $\mathsf{0}$, $\mathsf{1}$},
xlabel = {Zeit (in \si{\second)}},
ylabel = {Blinkerstellung \vphantom{(in \si{\percent)}}},
rounded corners = 1pt,
clip = false
]
\addplot[draw = black, line width = 2pt, line cap = round] table[col sep = comma] {%
x y
115,0
119.76,0
};
\addlegendentry{keine Blinkerbetätigung}
\addplot[draw = dark, line width = 2pt, line cap = round] table[col sep = comma] {%
x y
119.76,0
119.760000000000001,1
123,1
123.000000000000001,0
};
\addlegendentry{Blinker links}
\addplot[draw = black, line width = 2pt, line cap = round] table[col sep = comma] {%
x y
125.400000000000001,0
135,0
};
\end{axis}
\end{tikzpicture}
\end{document}
如果我正在使用clip=false
示例中的圆形线帽,则圆线帽会在两侧延伸到 x 轴。我尝试不使用剪辑功能来做到这一点,但由于线宽较粗,红线会在上侧被切断,我不想改变。我尝试的另一种解决方案是使用shorten
,但它也会扩展网格。
是否可以只剪切 x 轴网格而不剪切 y 轴上的红线(y = 1)?或者我可以延伸 y 轴,这样 y 轴的网格就不会延伸吗?
答案1
这是非常基于观点的答案...
我将按照以下方式绘制您的图像(不考虑我评论中的建议):
\documentclass[tikz, class=scrreprt, fontsize=10pt]{standalone}
\usepackage[T1]{fontenc}
\usepackage{libertine}
\usepackage[libertine]{newtxmath}
\usepackage{sansmath} % <--- new
\usepackage[stretch=10, shrink=10, final]{microtype}
\usepackage[detect-family, detect-weight]{siunitx}
\usepackage{pgfplots}
\pgfplotsset{compat = 1.18,
width = 20cm,
height = 10cm,
% if yo prefer you can add settings for layers,
% but for this particular picture are not needed
}
\usetikzlibrary{arrows.meta,
backgrounds,
calc,
decorations.pathreplacing,
calligraphy,
intersections,
positioning,
shapes}
\tikzset{every picture/.style={/utils/exec={\sffamily}}}
\definecolor{black}{RGB}{0,0,0} % schwarz
\definecolor{dark}{RGB}{215,48,31} % dunkelrot
\definecolor{orange}{RGB}{252,141,89} % orange
\definecolor{yellow}{RGB}{253,204,138} % gelb
\definecolor{light}{RGB}{254,240,217} % hellgelb
\definecolor{white}{RGB}{255,255,255} % weiß
\begin{document}
\begin{tikzpicture}
\begin{axis}[%
axis y line = left,
axis x line = bottom,
axis line style = {line width = 1pt, line cap = round,
-{Triangle[width = 6pt, length = 6pt, round, line width = 1pt]}},
%
enlarge x limits = {upper,value=0.05},
enlarge y limits = {upper,value=0.1},
%
xmin = 115, xmax = 135,
ymin = -1.2, ymax = 1,
xtick = {115, 120, ...,135},
ytick = {-1.2, $-1$, 0, 1},
xticklabels = {0, 5, ..., 20},
yticklabels = {,$\mathsf{-1}$ , 0 , 1},
xlabel = {Zeit (in \si{\second)}},
ylabel = {Blinkerstellung},
rounded corners = 1pt,
%
tick label style = {font=\sansmath\sffamily,
/pgf/number format/.cd, fixed,
precision = 2,
use comma,
1000 sep = {}},
every axis plot post/.append style={line width = 2pt, line cap = round}
]
\addplot[draw = black] table[col sep = comma] {%
x y
115,0
119.76,0
};
\addlegendentry{keine Blinkerbetätigung}
\addplot[draw = dark] table[col sep = comma] {%
x y
119.76,0
119.760000000000001,1
123,1
123.000000000000001,0
};
\addlegendentry{Blinker links}
\addplot[draw = black] table[col sep = comma] {%
x y
125.400000000000001,0
135,0
};
\end{axis}
\end{tikzpicture}
\end{document}