当坐标轴包含负值时,在axis环境内使用相对坐标出现异常

发布于 2025-07-14 21:21:34

image.png

代码如下

\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}
4 条评论

发布
问题