折线图和多个条形图组合,在条形之间添加空格(tikzpicture)

折线图和多个条形图组合,在条形之间添加空格(tikzpicture)

我正在尝试使用 tikzpicture 将线条与条形图结合起来。不幸的是,我不知道如何在条形之间添加空间。当我添加xbar=.05cm到轴时,线条无法正确呈现。当我将其移动到时addplot,它不起作用。此外,使用此代码,图例不正确,因为我的橙色线标有“障碍”。有什么想法吗?

这是我当前的代码:

\begin{figure}
    \centering
    \begin{tikzpicture}
        \begin{axis}[
            xlabel={Scenario},
            xmin=0, xmax=25,
            ymin=0, ymax=14,
            xtick={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25},
            ytick={0,2,4,6,8,10,12,14},
            legend pos=north west,
            ymajorgrids=true,
            grid style=dashed,
            width=1\textwidth,
            height=0.5\textwidth,
        ]
        
        \addplot[
            draw=orange,
            ultra thick,
            ]
            coordinates {
            (1,2) (2,2) (3,2) (4,3) (5,4) (6,4) (7,4) (8,4) (9,5) (10,5) (11,6) (12,6) (13,6) 
            (14,6) (15,8) (16,9) (17,9) (18,10) (19,10) (20,12) (21,12) (22,13) (23,13) (24,14) (25,13) 
            };
            \legend{Complexity}

        \addplot[
            ybar,
            bar width= 3pt,
            fill=blue,
            ]
            coordinates {
            (1,2) (2,1) (3,1) (4,1) (5,2) (6,2) (7,4) (8,2) (9,3) (10,1) (11,1) (12,4) (13,4) 
            (14,4) (15,5) (16,5) (17,3) (18,5) (19,1) (20,10) (21,6) (22,6) (23,6) (24,6) (25,7) 
            };
            \legend{Cars}

        \addplot[
            ybar,
            bar width= 3pt,
            fill=green,
            ]
            coordinates {
            (1,0) (2,1) (3,1) (4,2) (5,2) (6,2) (7,0) (8,2) (9,2) (10,4) (11,5) (12,2) (13,2) 
            (14,2) (15,3) (16,4) (17,6) (18,5) (19,9) (20,2) (21,6) (22,7) (23,7) (24,8) (25,6) 
            };
            \legend{Obstacles}
        
        \end{axis}
    \end{tikzpicture}
\end{figure}

使用此代码它看起来如下:

在此处输入图片描述

答案1

像这样?

在此处输入图片描述

要使两个条形图都可见,您需要使用 将它们相互移动bar xshift。其大小应大于 ,以使它们之间的空间足够大bar width

\documentclass{article}
\usepackage[margin=20mm]{geometry}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}


\begin{document}
\begin{figure}
    \centering
    \begin{tikzpicture}
\begin{axis}[
    width=1\textwidth,
    height=0.5\textwidth,
    enlarge x limits={abs=1,upper},
%
    ymajorgrids=true,
    grid style=dashed,
%
    xlabel={Scenario},
    xmin=0, xmax=25,    xtick={1,2,...,25},
    ymin=0, ymax=14,    ytick={0,2,4,6,8,10,12,14},
%
    legend cell align=left,   
    legend pos=north west,
            ]
%
\addplot[
    draw=orange,
    ultra thick,
    ]
    coordinates {
    (1,2) (2,2) (3,2) (4,3) (5,4) (6,4) (7,4) (8,4) (9,5) (10,5) (11,6) (12,6) (13,6)
    (14,6) (15,8) (16,9) (17,9) (18,10) (19,10) (20,12) (21,12) (22,13) (23,13) (24,14) (25,13)
    };
    \addlegendentry{Complexity}
\addplot[
    ybar,
    bar width=6pt,
    ybar legend,
    bar shift=-3.6pt,    % <---
    fill=blue,
    ]
    coordinates {
    (1,2) (2,1) (3,1) (4,1) (5,2) (6,2) (7,4) (8,2) (9,3) (10,1) (11,1) (12,4) (13,4)
    (14,4) (15,5) (16,5) (17,3) (18,5) (19,1) (20,10) (21,6) (22,6) (23,6) (24,6) (25,7)
    };
    \addlegendentry{Cars}

\addplot[
    ybar,
    bar width=6pt,
    ybar legend,
    bar shift=+3.6pt,    % <---
    fill=green,
    ]
    coordinates {
    (1,0) (2,1) (3,1) (4,2) (5,2) (6,2) (7,0) (8,2) (9,2) (10,4) (11,5) (12,2) (13,2)
    (14,2) (15,3) (16,4) (17,6) (18,5) (19,9) (20,2) (21,6) (22,7) (23,7) (24,8) (25,6)
    };
    \addlegendentry{Obstacles}

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

相关内容