PGFPlots 转换小数分隔符

PGFPlots 转换小数分隔符

你好,xticks 的小数分隔符是一个点,我会用逗号,我该怎么办?

\documentclass[border=0.5mm,12pt]{standalone}

\usepackage{siunitx}                       % SI Package

\sisetup{%
         output-decimal-marker={,},
         table-format = 3.1%
        }

\usepackage{pgfplots}        % Grafici

\pgfplotsset{%
            mesh line legend/.style={legend image code/.code=\meshlinelegend#1},%
            /pgf/number format/use comma,%
            compat=newest,%
%            height=9cm,%
            width=12cm%
}


\begin{document}
%\begin{figure}
\begin{tikzpicture}
\pgfdeclarehorizontalshading{stefan}{50bp}{
color(0.00000000000000bp)=(violet);
color(8.33333333333333bp)=(blue);
color(16.66666666666670bp)=(cyan);
color(25.00000000000000bp)=(green);
color(33.33333333333330bp)=(yellow);
color(41.66666666666670bp)=(orange);
color(50.00000000000000bp)=(red)
}
\begin{axis}[%
grid=major,
legend pos=north west,
legend cell align=left,
no markers,
xmin=-0.3,
xmax=0.3,
ymin=0,
ymax=6,
yticklabel shift=2pt,
xticklabel shift=2pt,
point meta min={-0.3},
point meta max={0.3},
tick style={thin,black},
xticklabel style={text height=1.5ex},
xticklabels={%
        $-0.4$,
        $-0.3$,
        $-0.2$,
        $-0.1$,
        $\mu$,
        $0.1$,
        $0.2$,
        $0.3$,
        $0.4$
    },
extra x ticks={-0.4,-0.3,...,0.4},
extra x tick labels={%
        $-0.4$,
        $-0.3$,
        $-0.2$,
        $-0.1$,
        $\mu$,
        $0.1$,
        $0.2$,
        $0.3$,
        $0.4$
    },
extra x tick style={
    xticklabel pos=right,
    xticklabel style={text depth=0pt}
},
extra y ticks={0,1,...,6},
extra y tick style={
    yticklabel pos=right
},
minor tick num=4,
xlabel=\large $x$,
ylabel=\large $f_{\mu,\sigma^2}(x)$,
colorbar horizontal,
colorbar style={
    xticklabels={%
        $-0.4$,
        $-0.3$,
        $-0.2$,
        $-0.1$,
        $\mu$,
        $0.1$,
        $0.2$,
        $0.3$,
        $0.4$
    },
    xticklabel shift=2pt,
    xticklabel style={text height=1.5ex}
},
axis line style={draw=none}, after end axis/.append code={\draw (rel axis cs:0,0) rectangle (rel axis cs:1,1);},
colormap={new}{color(0cm)=(violet);color(1cm)=(blue);color(2cm)=(cyan);color(3cm)=(green);color(4cm)=(yellow);color(5cm)=(orange);color(6cm)=(red)},
after end axis/.append code={%
    \draw ({rel axis cs:0,0}-|{axis cs:0,0}) -- ({rel axis cs:0,1}-|{axis cs:0,0});
}
]
\addplot gnuplot[
    shading=stefan,
    draw=none,
    shader=interp,
    id=DoG,
    samples=1000,
    domain=-0.3:0.3,
    y domain=0:1
]{((1/(sqrt(2*pi*0.08**2)))*exp(-(x-0)**2/(2*0.08**2)))}\closedcycle;
\addlegendimage{empty legend}\addlegendentry{$\mu=27,6\si{\second}$}
\addlegendimage{empty legend}\addlegendentry{$\sigma^2=0,01\si{\second}$}
\end{axis}
\end{tikzpicture}
\end{document}

答案1

您无需提供数字和\mu作为明确列表,而是可以使用键xticklabel来检查当前刻度是否为0,并将其替换为\mu。如果不是,您可以使用打印数字\pgfmathprintnumber{\tick},可以使用/pgf/number format选项对其进行格式化。您应该使用\pgfmathapproxequalto{\tick}{0}来解释使用符号时引入的数字错误{-0.3,-0.2,...,0.3}

所以如果你设置

xticklabel={
    \pgfmathapproxequalto{\tick}{0}%
    \ifdim\pgfmathresult pt=1pt
        $\mu$
    \else
        \pgfmathprintnumber[fixed]{\tick}
    \fi
},
extra x ticks={-0.4,-0.3,...,0.4},
extra x tick label={
    \pgfmathapproxequalto{\tick}{0}%
    \ifdim\pgfmathresult pt=1pt
        $\mu$
    \else
        \pgfmathprintnumber[fixed]{\tick}
    \fi
}

你的输出将是

答案2

告诉 LaTeX 打印$-0.4$等等。只需写

$-0{,}4$

或者,由于您正在使用siunitxoutput-decimal-marker={,}选项,因此只需使用

\num{-0.4}

据我所知,刻度标签只是在一个框中排版。

相关内容