我怎样才能在这个 pgfplots 中均匀分布条形图?

我怎样才能在这个 pgfplots 中均匀分布条形图?

我制作了一个带有两个轴的条形图,因此我必须手动调整条形之间的间距以使其适合。我想将橙色支撑条移到右侧,我知道我可以使用条形移位来实现这一点。但是,对于最后一组条形,我如何在右侧留出空间,以便图表中可以容纳最后一个橙色支撑条?

\documentclass{article}

\usepackage{pgfplots}

\begin{document}
    
\definecolor{bblue}{HTML}{4F81BD}
\definecolor{rred}{HTML}{C0504D}
\definecolor{ggreen}{HTML}{9BBB59}
\definecolor{ppurple}{HTML}{9F4C7C}
\definecolor{oorange}{HTML}{FF8C00}

\begin{tikzpicture}
    \begin{axis}[
        ybar,
        ymajorgrids = true,
        ymin = 0, ymax = 1,
        width  = .95\linewidth, height = 7cm,
        symbolic x coords = {STYLE \& BEAUTY, WEDDINGS, POLITICS, DIVORCE, HOME \& LIVING},
        bar width = 0.45cm, 
        xtick = data,
        major x tick style = transparent,
        nodes near coords,
        every node near coord/.append style={rotate=90, anchor=west},
        axis y line*=left,
        ylabel = {F$_1$ Score}
    ]
        \addplot[style={bblue,fill=bblue,mark=none}] coordinates {
            (STYLE \& BEAUTY,0.84) (WEDDINGS,0.83) (POLITICS,0.79) (DIVORCE,0.78) (HOME \& LIVING,0.78)
        };\label{plt:albert}

        \addplot[style={rred,fill=rred,mark=none}] coordinates {
            (STYLE \& BEAUTY,0.75) (WEDDINGS,0.68) (POLITICS,0.60) (DIVORCE,0.64) (HOME \& LIVING,0.62)
        };\label{plt:distilbert}

        \addplot[style={ggreen,fill=ggreen,mark=none}] coordinates {
            (STYLE \& BEAUTY,0.74) (WEDDINGS,0.72) (POLITICS,0.73) (DIVORCE,0.70) (HOME \& LIVING,0.63)
        };\label{plt:svm}

        \addplot[style={ppurple,fill=ppurple,mark=none}] coordinates {
            (STYLE \& BEAUTY,0.69) (WEDDINGS,0.68) (POLITICS,0.71) (DIVORCE,0.65) (HOME \& LIVING,0.59)
        };\label{plt:mnb}
                    
    \end{axis}
    
    \begin{axis}[
        ybar,
        axis x line=none,%axis on top,
        ymin = 0, ymax = 8000,
        width  = .95\linewidth, height = 7cm,
        symbolic x coords = {STYLE \& BEAUTY, WEDDINGS, POLITICS, DIVORCE, HOME \& LIVING},
        bar width = 0.45cm, 
        % bar shift = 5*0.1cm,
        xtick = data,
        nodes near coords,
        every node near coord/.append style={rotate=90, anchor=west},
        legend style = {at={(0.5,1.14)},
        anchor = north,legend columns=-1},
        axis y line*=right,
        ylabel = {Support}
    ]
    \addlegendimage{/pgfplots/refstyle=plt:albert}\addlegendentry{ALBERT}
    \addlegendimage{/pgfplots/refstyle=plt:distilbert}\addlegendentry{DistilBERT}
    \addlegendimage{/pgfplots/refstyle=plt:svm}\addlegendentry{SVM}
    \addlegendimage{/pgfplots/refstyle=plt:mnb}\addlegendentry{MNB}
    
    \addplot[style={oorange,fill=oorange,mark=none}] coordinates {
            (STYLE \& BEAUTY,1955) (WEDDINGS,747) (POLITICS,6477) (DIVORCE,693) (HOME \& LIVING,827)
        };\label{plt:support}

    \addlegendimage{/pgfplots/refstyle=plt:support}\addlegendentry{Support}
    
    \end{axis}
\end{tikzpicture}
\end{document}

这是条形图现在,正如您所看到的,右侧已经没有更多空间来容纳最后一个橙色条。

答案1

如果我正确理解了您的问题,那么以下 MWE(最小工作示例)可以作为起点:

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\definecolor{bblue}{HTML}{4F81BD}
\definecolor{rred}{HTML}{C0504D}
\definecolor{ggreen}{HTML}{9BBB59}
\definecolor{ppurple}{HTML}{9F4C7C}
\definecolor{oorange}{HTML}{FF8C00}

\begin{document}

    \begin{tikzpicture}
    \pgfplotsset{set layers}
\begin{axis}[width=\linewidth,
    ybar,
    bar width=3mm,
    enlarge x limits=0.15,
    ymajorgrids,
        nodes near coords,
        every node near coord/.append style={rotate=90, anchor=west},
% y ticks style and label
    axis y line*=left,
    ylabel={F$_1$ Score},
    ymin=0, ymax=1,
% x axis ticks and style
    xtick=data,
    symbolic x coords = {STYLE \& BEAUTY, WEDDINGS, 
                         POLITICS, DIVORCE, HOME \& LIVING},
    x tick label style = {rotate=45, anchor=east},
% legends and labels
    legend style = {at={(0.5,1.02)},
                    legend columns=-1,
                    anchor=south,
                    /tikz/every even column/.append style={column sep=1em}
                    },
    ]
\addplot [draw=blue, fill=bblue] 
         coordinates {  (STYLE \& BEAUTY,0.84)  (WEDDINGS,0.83)
                        (POLITICS,0.79)         (DIVORCE,0.78)
                        (HOME \& LIVING,0.78)
                    };
\addplot [draw=rred, fill=rred]
         coordinates {  (STYLE \& BEAUTY,0.75)  (WEDDINGS,0.68)
                        (POLITICS,0.60)         (DIVORCE,0.64)
                        (HOME \& LIVING,0.62)
                    };

\addplot [draw=oorange, fill=oorange]   % only reserve space for plot in second exis
         coordinates {  (STYLE \& BEAUTY,0)  (WEDDINGS,0)
                        (POLITICS,0)         (DIVORCE,0)
                        (HOME \& LIVING,0)
                    };

\addplot [draw=ggreen, fill=ggreen]
         coordinates {  (STYLE \& BEAUTY,0.74)  (WEDDINGS,0.72)
                        (POLITICS,0.73)         (DIVORCE,0.70)
                        (HOME \& LIVING,0.63)
                    };
\addplot [draw=ppurple, fill=ppurple]
         coordinates {  (STYLE \& BEAUTY,0.69)  (WEDDINGS,0.68)
                        (POLITICS,0.71)         (DIVORCE,0.65)
                        (HOME \& LIVING,0.59)
                    };
\legend{ALBERT, DistilBERT, Support, SVM, MNB}
\end{axis}
\begin{axis}[width=\linewidth,
    ybar,
    bar width=3mm,
    enlarge x limits=0.15,
    ymajorgrids,
        nodes near coords,
        every node near coord/.append style={rotate=90, anchor=west},
% y ticks style and label
    axis y line*=right,
    ymin=0, ymax=8000,
    ylabel={Support},
% x axis ticks
    xtick=\empty,
    symbolic x coords = {STYLE \& BEAUTY, WEDDINGS,
                         POLITICS, DIVORCE, HOME \& LIVING},
            ]
\addplot [draw=oorange, fill=oorange]
         coordinates {  (STYLE \& BEAUTY,1955)  (WEDDINGS,747)
                        (POLITICS,647)          (DIVORCE,693)
                        (HOME \& LIVING,827)
                    };
\end{axis}
    \end{tikzpicture}
    
\end{document}

在此处输入图片描述

编辑: 如您所见,在左轴环境中为“支持”预留了空间ybar,后来在第二个右轴环境中(由真实空间)透支了空间。

答案2

为了将橙色条移到右侧,我在第二个轴环境中使用了 4 个占位条。然后我删除了这些条顶部的值,否则它们会与第一个轴的条重叠。

\documentclass{article}
\usepackage{xcolor}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}

\definecolor{ts_dgreen}{HTML}{38761d}
\definecolor{ts_lgreen}{HTML}{93c47d}
\definecolor{ts_dblue}{HTML}{1155cc}
\definecolor{ts_lblue}{HTML}{a4c2f4}
\definecolor{ts_orange}{HTML}{ff9900}

\begin{document}
\begin{tikzpicture}
    \pgfplotsset{set layers}
    \begin{axis}[
        ybar,
        ymajorgrids = true,
        ymin = 0, ymax = 1,
        width  = .95\linewidth, height = 7cm,
        symbolic x coords = {STYLE \& BEAUTY, WEDDINGS,
                             POLITICS, DIVORCE, HOME \& LIVING},
        bar width = 4.5mm, 
        enlarge x limits=0.15,
        xtick = data,
        major x tick style = transparent,
        nodes near coords,
        every node near coord/.append style={rotate=90, anchor=west},
        legend style = {at={(0.5,1.02)},
        anchor = south,legend columns=-1,
        /tikz/every even column/.append style={column sep=1em}
        },
        axis y line*=left,
        ylabel = {F$_1$ Score}
    ]
        \addplot[draw=ts_dgreen, fill=ts_dgreen] coordinates {
            (STYLE \& BEAUTY,0.84) (WEDDINGS,0.83)
            (POLITICS,0.79)        (DIVORCE,0.78)
            (HOME \& LIVING,0.78)
        };

        \addplot[draw=ts_lgreen, fill=ts_lgreen] coordinates {
            (STYLE \& BEAUTY,0.75) (WEDDINGS,0.68)
            (POLITICS,0.60)        (DIVORCE,0.64) 
            (HOME \& LIVING,0.62)
        };

        \addplot[draw=ts_dblue, fill=ts_dblue] coordinates {
            (STYLE \& BEAUTY,0.74) (WEDDINGS,0.72)
            (POLITICS,0.73)        (DIVORCE,0.70)
            (HOME \& LIVING,0.63)
        };

        \addplot[draw=ts_lblue, fill=ts_lblue] coordinates {
            (STYLE \& BEAUTY,0.69) (WEDDINGS,0.68)
            (POLITICS,0.71)        (DIVORCE,0.65)
            (HOME \& LIVING,0.59)
        };
        
        \addplot[draw=ts_orange, fill=ts_orange] coordinates {
            (STYLE \& BEAUTY,0) (WEDDINGS,0)
            (POLITICS,0)        (DIVORCE,0)
            (HOME \& LIVING,0)
        };   
        
        \legend{DistilBert, ALBERT, SVM, MNB, Support}
    \end{axis}
    
    \begin{axis}[
        ybar,
        axis x line=none,%axis on top,
        ymin = 0, ymax = 8000,
        width  = .95\linewidth, height = 7cm,
        symbolic x coords = {STYLE \& BEAUTY, WEDDINGS,
                             POLITICS, DIVORCE, HOME \& LIVING},
        bar width = 4.5mm,
        enlarge x limits=0.15,
        xtick = \empty,
        axis y line*=right,
        ylabel = {Support}
    ]
        \addplot[draw=ts_dgreen, fill=ts_dgreen] coordinates {
            (STYLE \& BEAUTY,0) (WEDDINGS,0)
            (POLITICS,0)        (DIVORCE,0)
            (HOME \& LIVING,0)
        };

        \addplot[draw=ts_lgreen, fill=ts_lgreen] coordinates {
            (STYLE \& BEAUTY,0) (WEDDINGS,0)
            (POLITICS,0)        (DIVORCE,0)
            (HOME \& LIVING,0)
        };

        \addplot[draw=ts_dblue, fill=ts_dblue] coordinates {
            (STYLE \& BEAUTY,0) (WEDDINGS,0)
            (POLITICS,0)        (DIVORCE,0)
            (HOME \& LIVING,0)
        };

        \addplot[draw=ts_lblue, fill=ts_lblue] coordinates {
            (STYLE \& BEAUTY,0) (WEDDINGS,0)
            (POLITICS,0)        (DIVORCE,0)
            (HOME \& LIVING,0)
        };
        
        \addplot[draw=ts_orange, fill=ts_orange, nodes near coords,
                 node near coords style={rotate=90, anchor=west}] coordinates {
            (STYLE \& BEAUTY,1955) (WEDDINGS,747)
            (POLITICS,6477)        (DIVORCE,693)
            (HOME \& LIVING,827)
        };

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

由此得出以下图表: 条形图

相关内容