如何将该直方图绘制为折线图?

如何将该直方图绘制为折线图?

假设我有这个直方图:

\documentclass{article}
\usepackage{pgfplots}

\begin{document}

\begin{tikzpicture}
\begin{axis}[
    symbolic x coords={a small bar,a medium bar,a large bar},
    xtick=data]
    \addplot[ybar,fill=blue] coordinates {
        (a small bar,42)
        (a medium bar,50)
        (a large bar,80)
    };
\end{axis}
\end{tikzpicture}

\end{document}

我如何将其转换为线图样式?

答案1

你是说像这样吗?我刚刚[ybar,fill=blue]\addplot选项中删除了。

\documentclass{article}
\usepackage{pgfplots}

\begin{document}

\begin{tikzpicture}
\begin{axis}[
    symbolic x coords={a small bar,a medium bar,a large bar},
    xtick=data]
    \addplot coordinates {
        (a small bar,42)
        (a medium bar,50)
        (a large bar,80)
    };
\end{axis}
\end{tikzpicture}

\end{document}

在此处输入图片描述

您还可以添加任意数量的线条(带有图例),如下所示:

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.12}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
    legend style={
            at={(0.5,0.8),anchor=north}
        },
    symbolic x coords={precission, recall, f1-score},
    xtick=data]
    \addplot[mark=diamond*,thick,red] coordinates {
        (precission,42)         (recall,50)         (f1-score,80)         };
    \addlegendentry{Some}
    \addplot[mark=o,mark options={solid},blue,thick,dashed] coordinates {
        (precission,48)        (recall,58)        (f1-score,89)        };
    \addlegendentry{other}
\end{axis}
\end{tikzpicture}
\end{document}

在此处输入图片描述

或者简单地添加一个节点:

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.12}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
    symbolic x coords={precission, recall, f1-score},
    xtick=data]
    \addplot[mark=diamond*,thick,red] coordinates {
        (precission,42)         (recall,50)         (f1-score,80)
                 }node[pos=0.7,below,anchor=west]{some};
    \addplot[mark=o,mark options={solid},blue,thick,dashed] coordinates {
        (precission,48)        (recall,58)        (f1-score,89)
                }node[pos=0.7,above,anchor=east]{other};
\end{axis}
\end{tikzpicture}
\end{document}

在此处输入图片描述

但请注意不要使 x 刻度标签拥挤,因为您使用的是符号 x 坐标。

相关内容