条形图方向 -

条形图方向 -

我是使用 Latex 创建图表的新手。右侧的图表是原始图表,然后我决定如果更改每种分类器类型的颜色并在顶部添加条形图的值,它会看起来更好。我很难将标签放在底部。

\documentclass[12pt, oneside, landspace, leqno]{report}
\usepackage{tikz}

\begin{document}
    \begin{figure}[H]
    \centering
      \footnotesize{
     \begin{tikzpicture}[thick,scale=0.8, every node/.style={scale=0.8}]
            \begin{axis}[
                ybar,
                title=Precision Rate,
                symbolic x coords={NB1,  NB2, SVM1, SVM2, DT1, DT2, LR1, LR2},
                xtick=data,
               nodes near coords,
                every node near coord/.append style={font=\tiny},
                nodes near coords align={vertical},
                nodes/.style={font=\footnotesize},
              ]
                \addplot[fill=blue!30] plot coordinates { 
                    (NB1,   0.36)
                    (NB2,   0.96)
                    };
                \addplot[fill=teal!30] plot coordinates { 
                    (SVM1,  0.93)
                    (SVM2,  0.98)
                    };
              \addplot[fill=cyan!30] plot coordinates { 
                    (DT1,   0.93)
                    (DT2,   0.94)
                    };
               \addplot[fill=orange!30] plot coordinates { 
                     (LR1,   0.94)
                    (LR2,   0.99) 
                };
            \end{axis}
        \end{tikzpicture}%
    \end{document}

当前条形图

谢谢!

答案1

除了使示例 TeXable 之外,你还需要定义一个值每个栏,这样xtick就不会混淆。因为你想忽略我使用的“零” ybar stacked。最后,我将标签移到了栏的顶部,否则标签会位于栏的中心。我认为这就是你想要的输出,对吗?

\documentclass{standalone}
\usepackage{tikz}
\usepackage{pgfplots}
    \pgfplotsset{compat=1.13}
\begin{document}
    \begin{tikzpicture}[
        thick,
        scale=0.8,
        font=\footnotesize,
    ]
        \begin{axis}[
            ybar stacked,
            width=9cm,
            title=Precision Rate,
            symbolic x coords={NB1, NB2, SVM1, SVM2, DT1, DT2, LR1, LR2},
            xtick=data,
            nodes near coords,
            % move `nodes near coords' to the top of the bars
            every node near coord/.style={
                at={(axis cs:{[normalized]\pgfkeysvalueof{/data point/x}},\pgfkeysvalueof{/data point/y})},
                anchor=south,
            },
        ]
            \addplot[fill=blue!30] plot coordinates {
                (NB1, 0.36) (NB2, 0.96) (SVM1,0)    (SVM2,0)
                (DT1, 0)    (DT2, 0)    (LR1, 0)    (LR2, 0)
            };
            \addplot[fill=teal!30] plot coordinates {
                (NB1, 0)    (NB2, 0)    (SVM1,0.93) (SVM2,0.98)
                (DT1, 0)    (DT2, 0)    (LR1, 0)    (LR2, 0)
            };
            \addplot[fill=cyan!30] plot coordinates {
                (NB1, 0)    (NB2, 0)    (SVM1,0)    (SVM2,0)
                (DT1, 0.93) (DT2, 0.94) (LR1, 0)    (LR2, 0)
            };
            \addplot[fill=orange!30] plot coordinates {
                (NB1, 0)    (NB2, 0)    (SVM1,0)    (SVM2,0)
                (DT1, 0)    (DT2, 0)    (LR1, 0.94) (LR2, 0.99)
            };
        \end{axis}
    \end{tikzpicture}
\end{document}

显示上述代码结果的图像

相关内容