如何透過函數計算「額外 y 刻度」值而不是給出固定值

如何透過函數計算「額外 y 刻度」值而不是給出固定值

程式碼如下:

\documentclass[border=2pt]{standalone}

\usepackage{pgfplots}

\begin{document}

\tikzset{
    declare function={
        func(\x)= \x*\x;
    }
}

\begin{tikzpicture}
\def \xmin {-3}
\def \xmax {3}
\begin{axis}[
    xmin={\xmin}, xmax={\xmax},
    extra y ticks={9},
    extra y tick labels={$c$}]
\addplot [samples=100,domain={\xmin}:{\xmax}] {func(\x)};
\end{axis}
\end{tikzpicture}
\end{document}

結果

我添加了extra y ticks={9},但我真正想要的是添加,因此當值更改extra y ticks={func(\xmin)}時,可以將額外的 y 刻度標籤放置在正確的位置。\xmin如何編寫程式碼來extra y ticks透過函數計算值而不是給出固定值?謝謝!

答案1

來自評論的CW:

將計算設為 PGF 數學巨集並使用該巨集作為extra y ticks設定:

\documentclass[border=2pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.12}

\begin{document}

\tikzset{
    declare function={
        func(\x)= \x*\x;
    }
}

\begin{tikzpicture}
\def \xmin {-3}
\def \xmax {3}
\pgfmathsetmacro\myextratick{func(\xmin)}
\begin{axis}[
    xmin={\xmin}, xmax={\xmax},
    extra y ticks={\myextratick},
    extra y tick labels={$c$}]
\addplot [samples=100,domain={\xmin}:{\xmax}] {func(\x)};
\end{axis}
\end{tikzpicture}
\end{document}

在此輸入影像描述

相關內容