在 pgfplots 中的图表中添加多个标签(年份/月份)

在 pgfplots 中的图表中添加多个标签(年份/月份)

我想在一个图中添加几个标签pgfplots:一个标签表示月份,一个标签表示年份,就像这张图一样:

在此处输入图片描述

我尝试使用extra tick label,但我不知道如何将其格式从月份更改为年份,因此额外的和正常的 x 标签具有相同的格式。此外,我无法弄清楚如何移动刻度,使它们位于月份标签的左右两侧,而不是居中。

平均能量损失

\documentclass{article}

\usepackage{pgfplots}
\usepgfplotslibrary{dateplot} 

\begin{document}

\begin{tikzpicture}
\begin{axis}[
date coordinates in=x,
xtick=data,
extra x ticks={2009-10-01, 2010-02-01},
extra x tick style={yshift=-20pt},
%extra xticklabel=\year, % does not work
xticklabel style={anchor=near xticklabel},
xticklabel=\month,
date ZERO=2009-08-01,% <- improves precision!
]
\addplot coordinates {
(2009-08-01, 050)
(2009-09-01, 100)
(2009-10-01, 100)
(2009-11-01, 100)
(2009-12-01, 040)
(2010-01-01, 020)
(2010-02-01, 000)
(2010-03-01, 035)
};
\end{axis}
\end{tikzpicture}

\end{document}

编辑

为了在不同月份标签之间获得垂直线,我尝试使用 Jakes 方法并尝试命名节点\year\month,例如 20102(2010 年 2 月)。问题在于月份(\month)包含前导零,而解析器将它们解析为八进制数。因此,当我尝试计算\month-1以获取当前节点之前的节点时,我收到解析器错误。为了解决这个问题,我尝试使用提供的方法这里,但目前为止,它们都没有起到作用。

微波辐射计

\documentclass[border=5mm]{standalone}

\usepackage{pgfplots}
\usepgfplotslibrary{dateplot} 
\usetikzlibrary{calc}

\makeatletter
\long\def\ifnodedefined#1#2#3{%
    \@ifundefined{pgf@sh@ns@#1}{#3}{#2}%
}
\makeatother

\begin{document}

\def\monthnames{{1,2,3,4,5,6,7,8,9,10,11,12}}

\begin{tikzpicture}
\begin{axis}[
date coordinates in=x,
xtick=data,
xticklabel style={
    anchor=near xticklabel,     
        % Approach 1: Does not work missing endcsname error
    %name=\ifnodedefined{start\year{\pgfcalendar{tickcal}{\tick}{\tick}{\pgfcalendarshorthand{m}{-}}}} 
        %{end\year}  % Then this could be the last month
        %{start\year}, % Otherwise, start the year      
        % Approach 2: Does not work missing endcsname error
    %name=\ifnodedefined{start\year\pgfmathparse{\monthnames[Mod(\tick-1,12)]}\pgfmathresult} % Does not work missing endcsname error
        %{end\year}  % Then this could be the last month
        %{start\year}, % Otherwise, start the year      
},
xticklabel=\month,
date ZERO=2009-08-01,% <- improves precision!
]
\addplot coordinates {
(2009-08-01, 050)
(2009-09-01, 100)
(2009-10-01, 100)
(2009-11-01, 100)
(2009-12-01, 040)
(2010-01-01, 020)
(2010-02-01, 000)
(2010-03-01, 035)
};
\end{axis}
\end{tikzpicture}

\end{document}

答案1

您几乎已经了解了:额外的刻度标签可以接受与普通刻度标签相同的所有选项,您只需通过 提供它们即可extra x tick style={<normal tick options>}。在这种情况下,您会说extra x tick style={xticklabel=\year}

如果你想让事情变得有趣一点,你可以自动绘制组线,使用一些技巧来命名标签节点,并使用来自的方法检查命名节点的存在检查节点是否已定义

\documentclass{standalone}

\usepackage{pgfplots}
\usepgfplotslibrary{dateplot} 

\makeatletter
\long\def\ifnodedefined#1#2#3{%
    \@ifundefined{pgf@sh@ns@#1}{#3}{#2}%
}
\makeatother

\begin{document}

\begin{tikzpicture}
\begin{axis}[
date coordinates in=x,
xtick=data,
extra x ticks={2009-10-01, 2010-02-01},
extra x tick style={
    yshift=-3.5ex,
    xticklabel=\year,
    xticklabel style={name={}},
    major tick length=0pt
},
xticklabel style={
    anchor=near xticklabel,
    name=\ifnodedefined{start\year} % Have we already started this year?
        {end\year}  % Then this could be the last month
        {start\year}, % Otherwise, start the year
    append after command=
        \pgfextra{\pgfmathtruncatemacro\lastyear{\year-1}}
        \ifnodedefined{end\lastyear}
        {
                {\ifnum\month=1 (start\lastyear.south west) edge (end\lastyear.south east) \pgfextra{\xdef\finalyear{\year}}\fi}}
        {}
},
after end axis/.code={
    \draw (start\finalyear.south west) -- (end\finalyear.south east);
},
xticklabel=\month,
date ZERO=2009-08-01,% <- improves precision!
]
\addplot coordinates {
(2009-08-01, 050)
(2009-09-01, 100)
(2009-10-01, 100)
(2009-11-01, 100)
(2009-12-01, 040)
(2010-01-01, 020)
(2010-02-01, 000)
(2010-03-01, 035)
};
\end{axis}
\end{tikzpicture}

\end{document}

或者,类似地,如果您想要在年份之间有垂直分隔线:

\documentclass[border=5mm]{standalone}

\usepackage{pgfplots}
\usepgfplotslibrary{dateplot} 
\usetikzlibrary{calc}

\makeatletter
\long\def\ifnodedefined#1#2#3{%
    \@ifundefined{pgf@sh@ns@#1}{#3}{#2}%
}
\makeatother

\begin{document}

\begin{tikzpicture}
\begin{axis}[
date coordinates in=x,
xtick=data,
tick pos=left,
yticklabel style={append after command={(\tikzlastnode.east) edge +(0.15cm,0)}},
major tick length=0pt,
extra x ticks={2009-10-01, 2010-02-01},
extra x tick style={
    yshift=-3.5ex,
    xticklabel=\year,
    xticklabel style={name={}},
    every x tick label/.style={}
},
xticklabel style={
    anchor=near xticklabel,
    alias=tick\ticknum,
    name=\ifnodedefined{start\year} % Have we already started this year?
        {end\year}  % Then this could be the last month
        {start\year}, % Otherwise, start the year
    append after command=
        \pgfextra{\pgfmathtruncatemacro\lastyear{\year-1}}
        \ifnodedefined{end\lastyear}
        {
                {\ifnum\month=1 ({$(end\lastyear.south west)!0.5!(start\year.south east)$}|-{rel axis cs:0,0}) edge ++(0,-7ex)
                \fi}
        }
        {}
        \ifnum\ticknum>0
                \ifnum\month>1
                    \pgfextra{\pgfmathtruncatemacro\prevticknum{\ticknum-1}}
        ($(tick\prevticknum.north east)!0.5!(tick\ticknum.north west)$) edge ++({0,-3ex})
                \fi
        \fi
},
after end axis/.code={
    \draw (rel axis cs:0,0) -- ++(0,-7ex)
            (rel axis cs:1,0) -- ++(0,-7ex);
},
xticklabel=\month,
date ZERO=2009-08-01,% <- improves precision!
]
\addplot coordinates {
(2009-08-01, 050)
(2009-09-01, 100)
(2009-10-01, 100)
(2009-11-01, 100)
(2009-12-01, 040)
(2010-01-01, 020)
(2010-02-01, 000)
(2010-03-01, 035)
};
\end{axis}
\end{tikzpicture}

\end{document}

相关内容