我有一个 y 轴。我必须设置什么才能在刻度末尾获取刻度标签?
顺便说一句:我想要一个“零”轴,我需要为此设置什么?看来我需要:
axis lines*=middle anchor=origin,
\documentclass[margin=5pt, tikz]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\begin{document}
\def\pqmin{-20}
\def\pqmax{20}
\def\majortickdiff{10}
\begin{tikzpicture}[]
\begin{axis}[
ymin=\pqmin,
ymax=\pqmax,
y=0.2cm,
hide x axis,
axis lines*=middle,% seems to be needed
anchor=origin,% needed
major tick length=7mm,% wanted
minor y tick num=9,% wanted
ytick={\pqmin,\pqmin+\majortickdiff,...,20},
%y tick label style={anchor=near ticklabel},
every axis y label/.style={
anchor=near ticklabel opposite,
},
]
\addplot[draw=red] coordinates {(0,\pqmin) (0,\pqmax)};
\end{axis}
\end{tikzpicture}
\end{document}
答案1
\documentclass[tikz, border=1cm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
xmin=0, xmax=0,
ymin=-20, ymax=20,
y=0.2cm,
hide x axis,
hide obscured y ticks=false,
axis lines=middle,
ytick align=inside,
major tick length=7mm,
minor y tick num=9,
ytick={-20,-10,...,20},
y tick label style={xshift=7 mm, anchor=west},
y axis line style={-, red}
]
\end{axis}
\end{tikzpicture}
\end{document}
答案2
更紧凑的代码tikz
:
\documentclass[12pt,a4paper]{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[xscale=1,yscale=.25]
\draw[red] (0,-20)--(0,20);
\foreach \i in {-20,-19,...,20}
\draw (0,\i)--(0.2,\i);
\foreach \j in {-20,-10,0,10,20}
\draw (0,\j)--(1,\j) node[right] () {\j};
\end{tikzpicture}
\end{document}
输出: