突出显示数据图的图例问题

突出显示数据图的图例问题

我按照以下提示在数据图中突出显示http://www.howtotex.com/tips-tricks/highlighting-in-tikz-plots/

突出显示不太适用\addlegendentry

下面显示了一个 MWE。

\documentclass{article}

\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{pgfplotstable}

\usetikzlibrary{external}
\tikzexternalize
%\tikzset{external/mode=list and make}
%\tikzset{external/check=diff}
\tikzset{external/force remake}
%\tikzsetexternalprefix{figure-build/, up to date check=md5, force remake}

\pgfplotsset{compat=1.8}
%\pgfplotsset{compat=newest} % necessary for new features

% start code to enable highlighting
\pgfkeys{%
  /tikz/on layer/.code={
    \pgfonlayer{#1}\begingroup
    \aftergroup\endpgfonlayer
    \aftergroup\endgroup
  }
}

\pgfplotsset{
    highlight/.code args={#1:#2}{
        \fill [every highlight] ({axis cs:#1,0}|-{rel axis cs:0,0}) rectangle ({axis cs:#2,0}|-{rel axis cs:0,1});
    },
    /tikz/every highlight/.style={
        on layer=\pgfkeysvalueof{/pgfplots/highlight layer},
        blue!20 % default color of highlighted area
    },
    /tikz/highlight style/.style={
        /tikz/every highlight/.append style=#1
    },
    highlight layer/.initial=axis background    % default depth
}

\usepackage{filecontents}

\begin{filecontents}{data}
1790 -0.002923 0.005438 0.005438 0.001646 0.010007 0.004569 4.647375 4.720160 4.721806 0.018075 0.005438
1791 -0.000842 0.002737 0.002737 0.001249 0.004828 0.002091 4.651944 4.730167 4.731415 0.020812 0.002737
1792 0.001305 0.002307 0.002307 0.002377 0.003379 0.001072 4.654035 4.734995 4.737372 0.023119 0.002307
1793 -0.005790 0.006761 0.006761 0.000710 0.013261 0.006500 4.655107 4.738374 4.739084 0.029880 0.006761
1794 -0.001449 0.002468 0.002468 0.000567 0.004484 0.002016 4.661608 4.751634 4.752201 0.032348 0.002468
1795 0.004092 0.004121 0.004121 0.005267 0.005296 0.001175 4.663623 4.756118 4.761385 0.036469 0.004121
1796 0.001135 0.001165 0.001165 0.003397 0.003427 0.002262 4.664798 4.761414 4.764811 0.037634 0.001165
1797 -0.000894 -0.000894 -0.000861 0.000118 0.000151 0.001012 4.667060 4.764841 4.764959 0.036773 -0.000861
1798 -0.002721 -0.002721 -0.002688 0.000569 0.000602 0.003290 4.668073 4.764992 4.765561 0.034085 -0.002688
1799 -0.003568 -0.003568 -0.003541 0.000188 0.000215 0.003756 4.671363 4.765594 4.765782 0.030544 -0.003541
1800 -0.000879 -0.000879 -0.000848 0.000709 0.000740 0.001588 4.675119 4.765809 4.766518 0.029696 -0.000848
1801 0.001929 0.001961 0.001961 0.003127 0.003159 0.001198 4.676707 4.766549 4.769676 0.031657 0.001961
1802 -0.000059 -0.000059 -0.000020 0.001011 0.001050 0.001070 4.677906 4.769708 4.770719 0.031637 -0.000020
1803 -0.000024 0.000017 0.000017 0.001097 0.001138 0.001121 4.678976 4.770758 4.771855 0.031654 0.000017
1804 -0.001896 -0.001896 -0.001859 0.000068 0.000105 0.001964 4.680097 4.771896 4.771964 0.029795 -0.001859
1805 -0.002589 -0.002589 -0.002555 0.000066 0.000100 0.002655 4.682062 4.772001 4.772067 0.027240 -0.002555
1806 -0.000969 -0.000969 -0.000937 0.000032 0.000064 0.001001 4.684717 4.772101 4.772133 0.026303 -0.000937
1807 -0.001087 -0.001087 -0.001051 0.001283 0.001319 0.002370 4.685718 4.772165 4.773449 0.025252 -0.001051
1808 -0.001766 -0.001766 -0.001728 0.000454 0.000492 0.002220 4.688088 4.773484 4.773938 0.023524 -0.001728
1809 -0.000970 -0.000970 -0.000938 0.000032 0.000064 0.001002 4.690308 4.773977 4.774009 0.022586 -0.000938
1810 -0.000526 -0.000526 -0.000490 0.000516 0.000552 0.001042 4.691310 4.774041 4.774557 0.022096 -0.000490
1811 -0.004600 -0.004600 -0.004559 0.000362 0.000403 0.004962 4.692353 4.774593 4.774954 0.017537 -0.004559
1812 -0.005381 -0.005381 -0.005347 0.000323 0.000357 0.005704 4.697314 4.774996 4.775318 0.012190 -0.005347
1813 -0.005619 -0.005619 -0.005587 0.000032 0.000064 0.005651 4.703018 4.775353 4.775385 0.006603 -0.005587
1814 -0.000906 -0.000906 -0.000869 0.000101 0.000138 0.001007 4.708669 4.775417 4.775518 0.005734 -0.000869
1815 0.000603 0.000641 0.000641 0.001719 0.001757 0.001116 4.709676 4.775555 4.777273 0.006375 0.000641
1816 -0.001316 -0.001316 -0.001278 0.000086 0.000124 0.001402 4.710791 4.777312 4.777397 0.005097 -0.001278
1817 -0.001534 -0.001534 -0.001494 0.000304 0.000344 0.001838 4.712193 4.777436 4.777739 0.003603 -0.001494
1818 -0.001896 -0.001896 -0.001855 0.000077 0.000118 0.001973 4.714032 4.777780 4.777857 0.001748 -0.001855
1819 -0.001642 -0.001642 -0.001608 0.000125 0.000159 0.001767 4.716004 4.777897 4.778022 0.000140 -0.001608
1820 -0.001103 -0.001103 -0.000141 0.000358 0.001320 0.001461 4.717771 4.778056 4.778414 0.000000 -0.000140
\end{filecontents}

\begin{document}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
When highlight does not work with addlegendentry, the display is fine.
\vspace{0.5cm}

\tikzsetnextfilename{myfig1}
\begin{tikzpicture}
    \begin{axis}[
            set layers,
            xmode=linear,
            ymode=linear,
            axis x line*=bottom,
            axis y line*=left,
            tick label style={font=\small},
            grid=both,
            tick align=outside, 
            tickpos=left,
            xlabel=x,
            ylabel=y,
            width=0.45\textwidth,
            height=0.4\textwidth,
        ]
        \addplot [raw gnuplot, color=red] gnuplot {
                plot "data.tex" using 1:3 w lines;
        };
        \addlegendentry{z1};
        \addplot [raw gnuplot, color=blue] gnuplot {
                plot "data.tex" using 1:4 w lines;
        };
        \addlegendentry{z2};
        \addplot+[%
                no marks, raw gnuplot, color=green,
                % first highlighted area (in blue by default) 
                highlight=1790:1800,          % range
                % second highlighted area 
                highlight=1805:1807,              % range
        ] gnuplot {
                plot "data.tex" using 1:11 w lines;
        };
        %\addlegendentry{z3}
        \addplot+[%
                no marks, raw gnuplot, color=yellow,
                highlight style={red!20}, % color
                % first highlighted area
                highlight=1810:1820,          % range
        ] gnuplot {
                plot "data.tex" using 1:12;
        };
        %\addlegendentry{z4}
    \end{axis}
\end{tikzpicture}

\vspace{1.5cm}
When highlight works with addlegendentry, the display is bad.
\vspace{0.5cm}

\tikzsetnextfilename{myfig2}
\begin{tikzpicture}
    \begin{axis}[
            set layers,
            xmode=linear,
            ymode=linear,
            axis x line*=bottom,
            axis y line*=left,
            tick label style={font=\small},
            grid=both,
            tick align=outside, 
            tickpos=left,
            xlabel=x,
            ylabel=y,
            width=0.45\textwidth,
            height=0.4\textwidth,
        ]
        \addplot [raw gnuplot, color=red] gnuplot {
                plot "data.tex" using 1:3 w lines;
        };
        \addlegendentry{z1};
        \addplot [raw gnuplot, color=blue] gnuplot {
                plot "data.tex" using 1:4 w lines;
        };
        \addlegendentry{z2};
        \addplot+[%
                no marks, raw gnuplot, color=green,
                % first highlighted area (in blue by default) 
                highlight=1790:1800,          % range
                % second highlighted area 
                highlight=1805:1807,              % range
        ] gnuplot {
                plot "data.tex" using 1:11 w lines;
        };
        \addlegendentry{z3}
        \addplot+[%
                no marks, raw gnuplot, color=yellow,
                highlight style={red!20}, % color
                % first highlighted area
                highlight=1810:1820,          % range
        ] gnuplot {
                plot "data.tex" using 1:12;
        };
        \addlegendentry{z4}
    \end{axis}
\end{tikzpicture}
\end{document}

此文件 main.tex 由以下程序编译

pdflatex -shell-escape main.tex

每当使用突出显示时\addplot,图例都会变成一个覆盖主数据图的大框。您有什么建议来处理这种情况吗?谢谢。

答案1

我遇到了同样的错误,我找到了一种解决方法,即从中删除所有突出显示关键字,\addplots然后\addplots在最后添加一个,用打印一个不可见的图forget plot, draw=none, no marks,这次使用所有突出显示选项而没有图例。

这个错误出现在修改自这里

在此处输入图片描述

\documentclass[tikz]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}

\pgfkeys{%
  /tikz/on layer/.code={
    \pgfonlayer{#1}\begingroup
    \aftergroup\endpgfonlayer
    \aftergroup\endgroup
  }
}

\pgfplotsset{
    highlight/.code args={#1:#2}{
        \fill [every highlight] ({axis cs:#1,0}|-{rel axis cs:0,0}) rectangle ({axis cs:#2,0}|-{rel axis cs:0,1});
    },
    /tikz/every highlight/.style={
        on layer=\pgfkeysvalueof{/pgfplots/highlight layer},
        blue!20
    },
    /tikz/highlight style/.style={
        /tikz/every highlight/.append style=#1
    },
    highlight layer/.initial=axis background
}


\begin{document}
\begin{tikzpicture}
\begin{axis}[
    set layers,
    domain=0:10,
    grid=both
]
\addplot+[no marks,
%highlight 1
highlight=1:4] {sin(deg(x))};
\addlegendentry{legend1}
\addplot+[no marks,
%highlight 2
highlight layer=axis ticks,
highlight style={red!20},
highlight=6:9] {cos(deg(x))};
\addlegendentry{legend2}

\end{axis}
\end{tikzpicture}
\end{document}

虽然这给出了正确的结果:

在此处输入图片描述

\documentclass[tikz]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}

\pgfkeys{%
  /tikz/on layer/.code={
    \pgfonlayer{#1}\begingroup
    \aftergroup\endpgfonlayer
    \aftergroup\endgroup
  }
}

\pgfplotsset{
    highlight/.code args={#1:#2}{
        \fill [every highlight] ({axis cs:#1,0}|-{rel axis cs:0,0}) rectangle ({axis cs:#2,0}|-{rel axis cs:0,1});
    },
    /tikz/every highlight/.style={
        on layer=\pgfkeysvalueof{/pgfplots/highlight layer},
        blue!20
    },
    /tikz/highlight style/.style={
        /tikz/every highlight/.append style=#1
    },
    highlight layer/.initial=axis background
}


\begin{document}
\begin{tikzpicture}
\begin{axis}[
    set layers,
    domain=0:10,
    grid=both
]
\addplot+[no marks] {sin(deg(x))};
\addlegendentry{legend1}
\addplot+[no marks] {cos(deg(x))};
\addlegendentry{legend2}
\addplot+[forget plot,draw=none, no marks,
%highlight 1
highlight=1:4,
%highlight 2
highlight layer=axis ticks,
highlight style={red!20},
highlight=6:9
] coordinates {(0,0)};
%% NO LEGEND HERE !
\end{axis}
\end{tikzpicture}
\end{document}

相关内容