带标记、标记节点和图例的直方图无法正确显示所有内容

带标记、标记节点和图例的直方图无法正确显示所有内容

我对 TEX 还不太熟悉。我目前正在撰写一篇论文,但是我无法在同一张图中正确组合标记、标记的节点标签、图例和直方图。这在 MATLAB 中很容易做到,但在 TEX 中却不那么容易做到。

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{automata,positioning}
\usepackage{graphicx}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}

\begin{figure}
\begin{tikzpicture}
\begin{axis}[
    legend entries={$\textit{my}_{data}$,$\textit{sigma}_{3}$,$\textit{testpoint}+2$,$\textit{testpoint}+1$,$\textit{testpoint}$},
    ymin=0, ymax=300,
    minor y tick num = 4,
    %ybar,
    legend style={align=left}
    %legend pos=outer north
    ]
\addlegendimage{ybar interval, fill=blue!30, draw=blue}
\addlegendimage{only marks, mark=o}
\addlegendimage{only marks, mark=square*}
\addlegendimage{only marks, mark=diamond*}
\addlegendimage{only marks, mark=triangle*}

%histogram plot
\addplot+[ybar interval,mark=no, fill=blue!30,draw=blue] plot coordinates { (-.71, 1) (-.705,0)(-.68,0)(-.675, 1) (-.67, 1) (-.665, 4) (-.66, 5) (-.655, 11)(-.65,34)(-.645,62)(-.64,80)(-.635,108)(-.63,182)(-.625,236)(-.62,220)(-.615,261)(-.61,193)(-.605,143)(-.6,99)(-.595,57)(-.59,20)(-.585,10)(-.58,1)(-.575,0)(-.56,0)(-.555,1)(-.55,1)};

%sigma
\addplot[only marks,mark=o,mark size=2pt,black,nodes near coords = \rotatebox{90}{{\pgfmathprintnumber[fixed zerofill,precision=4]{\pgfplotspointmeta}}},
        nodes near coords align={vertical},
        point meta=x,
        every node near coord/.append style={font=\small, yshift=30pt},
        ]  coordinates {
    (-.6609,0)
};

%testpoint +2
\addplot[only marks,mark=square*,mark size=2pt,black,nodes near coords = \rotatebox{90}{{\pgfmathprintnumber[fixed zerofill,precision=4]{\pgfplotspointmeta}}},
        nodes near coords align={vertical},
        point meta=x,
        every node near coord/.append style={font=\small, yshift=30pt},
        ]  coordinates {
    (-.6721,0)
};

%testpoint +1
\addplot[only marks,mark=diamond*,mark size=2pt,black,nodes near coords = \rotatebox{90}{{\pgfmathprintnumber[fixed zerofill,precision=4]{\pgfplotspointmeta}}},
        nodes near coords align={vertical},
        point meta=x,
        every node near coord/.append style={font=\small, yshift=30pt},
        ]  coordinates {
    (-.7059,0)
};

%testpoint
\addplot[only marks,mark=triangle*,mark size=2pt,black,nodes near coords = \rotatebox{90}{{\pgfmathprintnumber[fixed zerofill,precision=4]{\pgfplotspointmeta}}},
        nodes near coords align={vertical},
        point meta=x,
        every node near coord/.append style={font=\small, yshift=30pt},
        ]  coordinates {
    (-.5529,0)
};

\end{axis}
\end{tikzpicture}
\caption{Example Distribution}
\end{figure}
\end{document}

我遇到的问题是,当我使用当前代码时,它无法正确显示图例中的数据箱。另一方面,如果我将其包含ybaraxis选项中,它会弄乱标记 x 坐标的节点,并使图例中的标记加倍。

有什么想法可以修复这个问题但仍然只有 1 个图例吗?

答案1

添加ybar interval legend到情节选项和/或\addlegendimage。 (我不太明白你为什么使用\addlegendimage,它似乎没有必要。)

在此处输入图片描述

\documentclass{article}
\usepackage{pgfplots} % loads tikz which loads graphicx
\pgfplotsset{compat=1.16}

\begin{document}
\begin{figure}
\centering
\begin{tikzpicture}
\begin{axis}[
    legend entries={$\textit{my}_{data}$,$\textit{sigma}_{3}$,$\textit{testpoint}+2$,$\textit{testpoint}+1$,$\textit{testpoint}$},
    ymin=0, ymax=300,
    minor y tick num = 4,
    %ybar,
    legend style={align=left},
    legend pos=north west,
    legend cell align=left
    ]
%\addlegendimage{ybar interval, ybar interval legend, fill=blue!30, draw=blue}
%\addlegendimage{only marks, mark=o}
%\addlegendimage{only marks, mark=square*}
%\addlegendimage{only marks, mark=diamond*}
%\addlegendimage{only marks, mark=triangle*}

%histogram plot
\addplot [ybar interval,ybar interval legend,fill=blue!30,draw=blue] coordinates { (-.71, 1) (-.705,0)(-.68,0)(-.675, 1) (-.67, 1) (-.665, 4) (-.66, 5) (-.655, 11)(-.65,34)(-.645,62)(-.64,80)(-.635,108)(-.63,182)(-.625,236)(-.62,220)(-.615,261)(-.61,193)(-.605,143)(-.6,99)(-.595,57)(-.59,20)(-.585,10)(-.58,1)(-.575,0)(-.56,0)(-.555,1)(-.55,1)};

%sigma
\addplot[only marks,mark=o,mark size=2pt,black,nodes near coords = \rotatebox{90}{{\pgfmathprintnumber[fixed zerofill,precision=4]{\pgfplotspointmeta}}},
        nodes near coords align={vertical},
        point meta=x,
        every node near coord/.append style={font=\small, yshift=30pt},
        ]  coordinates {
    (-.6609,0)
};

%testpoint +2
\addplot[only marks,mark=square*,mark size=2pt,black,nodes near coords = \rotatebox{90}{{\pgfmathprintnumber[fixed zerofill,precision=4]{\pgfplotspointmeta}}},
        nodes near coords align={vertical},
        point meta=x,
        every node near coord/.append style={font=\small, yshift=30pt},
        ]  coordinates {
    (-.6721,0)
};

%testpoint +1
\addplot[only marks,mark=diamond*,mark size=2pt,black,nodes near coords = \rotatebox{90}{{\pgfmathprintnumber[fixed zerofill,precision=4]{\pgfplotspointmeta}}},
        nodes near coords align={vertical},
        point meta=x,
        every node near coord/.append style={font=\small, yshift=30pt},
        ]  coordinates {
    (-.7059,0)
};

%testpoint
\addplot[only marks,mark=triangle*,mark size=2pt,black,nodes near coords = \rotatebox{90}{{\pgfmathprintnumber[fixed zerofill,precision=4]{\pgfplotspointmeta}}},
        nodes near coords align={vertical},
        point meta=x,
        every node near coord/.append style={font=\small, yshift=30pt},
        ]  coordinates {
    (-.5529,0)
};

\end{axis}
\end{tikzpicture}
\caption{Example Distribution}
\end{figure}
\end{document}

相关内容