代码如下
\documentclass[tikz,border=5pt]{standalone}
\usepackage[UTF8]{ctex} % 加载中文支持
\usepackage{pgfplots} % 核心绘图库
\usepackage{bm} % 或
\usepackage{amsmath}
\usepackage{silence}
% 定义 Times New Roman 字体命令
\WarningFilter{latexfont}{} % 过滤所有字体警告
\pgfplotsset{compat=1.18} % 版本兼容性设置
\usetikzlibrary{positioning,arrows.meta,angles,quotes,matrix,calc,fillbetween,patterns,patterns.meta,math}
\usepgfplotslibrary{fillbetween}
\usepackage{newtxtext, newtxmath} % Times 风格字体
\usepackage{bm} % 加粗数学符号
\newcommand{\bt}[1]{\bm{\mathit{#1}}} % 自定义加粗倾斜命令
\newcommand{\axisarrow}
{axis line style = {-{Triangle[length=8pt, width=3pt]}},} % 修正箭头语法
\pgfplotsset{
tick_empty/.style={
xtick=\empty,
ytick=\empty,
tick style={draw=none},
major tick length=0pt
}
}
\begin{document}
\begin{tikzpicture}%3.1
\begin{axis}[
axis lines = middle,
xlabel = $x$,
ylabel = $y$,
width=10cm,
height=7cm,
xlabel style={at={(ticklabel* cs:1)}, anchor=north, below=1pt},
ylabel style={at={(ticklabel* cs:1)}, anchor=east, left=1pt},
xmin = -2, xmax = 18,
ymin = -2, ymax = 10,
\axisarrow % 已经有逗号
xtick={3, 5.56, 12.54, 16},
xticklabels={$a$, $\xi_1$, $\xi_2$, $b$},
tick style={draw=none}, % 隐藏所有刻度线
ytick=\empty
]%不能空行
\coordinate (O) at (axis cs:1,0);
%比较:这是在axis环境内(当包含负数就会出现异常xmin = -2, xmax = 18, ymin = -2, ymax = 10,)
\draw[dash pattern=on 3pt off 2pt] (O) --++ (0,5);
\draw[dash pattern=on 5pt off 5pt, red] (O) --++ (axis cs:0,5);
\node[below left] at (O) {$\bt{O}$};%原点
\end{axis}
%比较:这是在axis环境外(正常)
\draw[dash pattern=on 5pt off 2pt] (O) --++ (0,5);
\node[below=10pt of current bounding box.south, anchor=north]{图\,3.1};
\end{tikzpicture}
\end{document}
同样有必要分开两份代码简化你想表述的观点,不然别人看到混在一起的代码很难理解
在描述内容的时候请多为读者着想,下面这些内容也都和你的问题毫无关系,他们理应被删去(也必须被删去)
问题重述与简化:
第一份代码
xmin=-1
这将得到:

第二份代码
xmin=0
:核心点
前后两个代码的坐标系似乎有所差异。其背后的原理探究。
事实上,「当坐标轴包含负值时」并不是问题的关键,看下面的例子:
其中
xmin=1
,效果同样有差异:meybe related link of the distortion of two coordinate systems of
axis
and outsidetikzpicture
.IMHO, we should do all the drawing concerning with the axis systems inside the
axis
environment, do them out of theaxis
will destory the completeness of whataxis
should do, thus, we should never drawing the vertical dashed line outside theaxis
in this case, that is exactly, what should be done insideaxis
environment.事实上,此事在文档中亦有记载(Page.357)
要想在
axis
内部使用「相对坐标」,需要使用提供了「direction」信息的axis direction cs
,仔细比较下面的例子: