我的代码如下:
\documentclass[convert={density=300,size=1080x800,outext=.png}]{standalone}
\usepackage{tikz}
\usetikzlibrary{math}
\usepackage{pgfplots}
\usepackage{graphicx} %kleinere Textgröße
\makeatletter
\def\tikz@math@if@@doif{%
\pgfmathparse{\tikz@math@if@condition}%
\ifpgfmathfloatparseactive% <--- Notice this
\pgfmathfloattofixed{\pgfmathresult}% <--- Notice this
\fi% <--- Notice this
\ifdim\pgfmathresult pt=0pt\relax%
\else%
\expandafter\tikz@math\expandafter{\tikz@math@if@trueaction}%
\fi%
\tikz@math@parse%
}
\def\tikz@math@if@@doifelse{%
\pgfmathparse{\tikz@math@if@condition}%
\let\tikz@math@if@falseaction=\tikz@math@collected%
\message{^^JCheck this: \pgfmathresult^^J}% <--- Notice this
\ifpgfmathfloatparseactive% <--- Notice this
\pgfmathfloattofixed{\pgfmathresult}% <--- Notice this
\fi% <--- Notice this
\message{^^JCheck again: \pgfmathresult^^J}%<--- Notice this
\ifdim\pgfmathresult pt=0pt\relax%
\expandafter\tikz@math\expandafter{\tikz@math@if@falseaction}%
\else%
\expandafter\tikz@math\expandafter{\tikz@math@if@trueaction}%
\fi%
\tikz@math@parse%
}
\tikzmath
{
function testfunction(\x)
{
if (0 <= \x && \x <= sqrt(3)/24) then
{
return (0.002 * 24 * \x / sqrt(3));
}
else
{
if (sqrt(3)/24 <= \x && \x <= sqrt(3) / 12) then
{
return (0.002 * 24 * (sqrt(3)/12 - \x) / sqrt(3));
}
else
{
return (0.002 * 24 * (\x - sqrt(3)/12));
};
};
};
\a = testfunction(0);
\b = testfunction(2*0.07217);
\c = testfunction(4*0.07217);
}
\begin{document}
\begin{tikzpicture}
\begin{axis}
[
,width=7cm
,xtick= {0,0.04,0.05,0.06,0.07,0.08,0.09,0.1,0.11,0.12,0.14433756729,0.16867512,0.17867512,0.18867512,0.19867512,0.20867512,0.21867512,0.22867512,0.23867512,0.24867512,0.25867512}
,xticklabels={\,,\scalebox{.4}{$d_m$}, \,, \,, \,,\,,\,,\,,\,,\scalebox{.4}{$d_{r+1}$},\scalebox{.4}{$d_r$}, \scalebox{.4}{$d_{r-1}$},\,,\,,\,,\,,\,,\,,\,,\scalebox{.4}{$d_{1}$},\scalebox{.4}{$d_{0}$}}
]
\addplot[blue, domain = 0:sqrt(3)/6]{testfunction(x)};
\end{axis}
\end{tikzpicture}
\end{document}
输出为:
我想要的修改选项如下:
我想在两点之间添加括号以突出显示某些距离并提供更多信息。此外,我希望$d_k$
每个标记的标签位于 x 轴上方,如第二张图片所示。
我查看了 pgfplot 手册,但仍然没有找到我想要的内容。谢谢你的帮助!
答案1
那么你想做以下的事情吗?请查看代码中的注释以了解更多详细信息。
% used PGFPlots v1.15
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
\usetikzlibrary{
calc,
math,
}
% use this `compat' level or higher to use the "advanced" features for
% setting the axis labels
\pgfplotsset{compat=1.3}
\makeatletter
\def\tikz@math@if@@doif{%
\pgfmathparse{\tikz@math@if@condition}%
\ifpgfmathfloatparseactive% <--- Notice this
\pgfmathfloattofixed{\pgfmathresult}% <--- Notice this
\fi% <--- Notice this
\ifdim\pgfmathresult pt=0pt\relax%
\else%
\expandafter\tikz@math\expandafter{\tikz@math@if@trueaction}%
\fi%
\tikz@math@parse%
}
\def\tikz@math@if@@doifelse{%
\pgfmathparse{\tikz@math@if@condition}%
\let\tikz@math@if@falseaction=\tikz@math@collected%
\message{^^JCheck this: \pgfmathresult^^J}% <--- Notice this
\ifpgfmathfloatparseactive% <--- Notice this
\pgfmathfloattofixed{\pgfmathresult}% <--- Notice this
\fi% <--- Notice this
\message{^^JCheck again: \pgfmathresult^^J}%<--- Notice this
\ifdim\pgfmathresult pt=0pt\relax%
\expandafter\tikz@math\expandafter{\tikz@math@if@falseaction}%
\else%
\expandafter\tikz@math\expandafter{\tikz@math@if@trueaction}%
\fi%
\tikz@math@parse%
}
\makeatletter
\tikzmath{
function testfunction(\x){
if (0 <= \x && \x <= sqrt(3)/24) then {
return (0.002 * 24 * \x / sqrt(3));
} else {
if (sqrt(3)/24 <= \x && \x <= sqrt(3) / 12) then {
return (0.002 * 24 * (sqrt(3)/12 - \x) / sqrt(3));
} else {
return (0.002 * 24 * (\x - sqrt(3)/12));
};
};
};
\a = testfunction(0);
\b = testfunction(2*0.07217);
\c = testfunction(4*0.07217);
}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
width=7cm,
xtick={
0,0.04,0.05,0.06,0.07,0.08,0.09,0.1,0.11,0.12,
0.14433756729,0.16867512,0.17867512,0.18867512,0.19867512,
0.20867512,0.21867512,0.22867512,0.23867512,0.24867512,0.25867512
},
xticklabels={
,\scalebox{.4}{$d_m$},,,,,,,,\scalebox{.4}{$d_{r+1}$},
\scalebox{.4}{$d_r$},\scalebox{.4}{$d_{r-1}$},,,,,,,,
\scalebox{.4}{$d_{1}$},\scalebox{.4}{$d_{0}$}
},
xticklabel style={
% move the ticklabels to the inside of the plot ...
above,
yshift=\pgfkeysvalueof{/pgfplots/major tick length},
% ... and give each of them a name
name=tickLabel-\ticknum,
},
% set the `ylabel'
ylabel={$\varphi(t)$},
% don't draw any `yticklabels'
yticklabels={},
% don't draw the scaling factor
scaled ticks=false,
% don't clip TikZ coordinates
clip mode=individual,
]
\addplot [blue,domain=0:sqrt(3)/6] {testfunction(x)};
\pgfplotsinvokeforeach {0,...,20} {
% create another set of coordinates that is shifted below the
% axis line (this is done by the "3pt")
\coordinate (TickLabel-#1)
at (tickLabel-#1 |- {xticklabel cs:0,3pt});
% % -----------------------------------------------------------------
% % for debugging purposes only
% \node [node font=\tiny,red] at (TickLabel-#1) {#1};
% % -----------------------------------------------------------------
}
% draw horizontal arrows below the axis
\begin{scope}[
stealth-stealth,
]
\draw (TickLabel-0) -- coordinate (rho half) (TickLabel-1);
\draw (TickLabel-7) -- coordinate (cM-1) (TickLabel-9);
\draw (TickLabel-9) -- coordinate (square) (TickLabel-11);
\draw (TickLabel-11) -- coordinate (cM-2) (TickLabel-13);
\draw (TickLabel-18) -- (TickLabel-20);
\end{scope}
% draw dots
\node at ($ (TickLabel-1)!0.5!(TickLabel-7) $) {$\cdots$};
\node at ($ (TickLabel-13)!0.5!(TickLabel-18) $) {$\cdots$};
% draw labels
\begin{scope}[
every node/.append style={
node font=\scriptsize,
yshift=-5ex,
},
]
\node (label rho half) at (rho half)
{$\frac{\rho}{2}$};
\node [xshift=-6ex] (label cM-1) at (cM-1)
{$\frac{c_2}{\sqrt{M}}$};
\node (label square) at (square)
{$2 c_1 \sqrt{\frac{\log(M)}{M}}$};
\node [xshift=6ex] (label cM-2) at (cM-2)
{$\frac{c_2}{\sqrt{M}}$};
\end{scope}
% draw arrows between labels
\pgfplotsforeachungrouped \i in {
rho half,
cM-1,
square,
cM-2%
} {
\edef\temp{\noexpand%
\draw [->] (label \i) -- ([yshift=-2pt]\i);
}\temp
}
\end{axis}
\end{tikzpicture}
\end{document}