如何使用 tikz 在同一图形中绘制两个图表?

如何使用 tikz 在同一图形中绘制两个图表?

我想用 tikz 绘制两个图表(一个在左边,另一个在右边)。这是我的代码,但它只能在一个图中绘制一个图表。非常感谢

\begin{figure}[htb] 
    \centering
    \begin{tikzpicture}


    \begin{axis}[
        ybar,
        xlabel = X,
        xmin = -0.3,xmax = 4.3,
        ymin = 0, ymax = 10,
        axis x line* = bottom,
        axis y line* = left,
        ylabel= Y,
        width= 0.9\textwidth,
        height = 0.6\textwidth,
        ymajorgrids = true,
        bar width = 0.5mm,
        xticklabels = \empty,
        extra x ticks = {0,1,2,3,4},
        extra x tick labels = {0,1,2,3,4},
        legend pos=north east,
        ]

        \addplot coordinates {
            (0,2)
            (1,6)
            (2,6)
            (3,6)
            (4,6)
        };
        \addplot coordinates {
            (0,5)
            (1,5)
            (2,5)
            (3,5)
            (4,5)
        };
        \addlegendimage{empty legend}
        \addlegendentry{$A$}
        \addlegendentry{$B$}
    \end{axis} 
    \end{tikzpicture}
    \caption{chart}
\end{figure}

预期输出喜欢 在此处输入图片描述

答案1

这显示了如何向一个图形添加两个(子标题包)子图。有许多不同的包带有子图,但它们都是美化的迷你页面。

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}% older version
\usepackage{subcaption}
\begin{document}
\begin{figure}[htb] 
  \begin{subfigure}{0.5\textwidth}
    \centering
    \begin{tikzpicture}

    \begin{axis}[
        ybar,
        xlabel = X,
        xmin = -0.3,xmax = 4.3,
        ymin = 0, ymax = 10,
        axis x line* = bottom,
        axis y line* = left,
        ylabel= Y,
        width= 0.9\textwidth,
        height = 0.6\textwidth,
        ymajorgrids = true,
        bar width = 0.5mm,
        xticklabels = \empty,
        extra x ticks = {0,1,2,3,4},
        extra x tick labels = {0,1,2,3,4},
        legend pos=north east,
        ]

        \addplot coordinates {
            (0,2)
            (1,6)
            (2,6)
            (3,6)
            (4,6)
        };
        \addplot coordinates {
            (0,5)
            (1,5)
            (2,5)
            (3,5)
            (4,5)
        };
        \addlegendimage{empty legend}
        \addlegendentry{$A$}
        \addlegendentry{$B$}
    \end{axis} 
    \end{tikzpicture}
    \caption{Subfigure}
  \end{subfigure}%
  \begin{subfigure}{0.5\textwidth}
    \centering
    \begin{tikzpicture}

    \begin{axis}[
        ybar,
        xlabel = X,
        xmin = -0.3,xmax = 4.3,
        ymin = 0, ymax = 10,
        axis x line* = bottom,
        axis y line* = left,
        ylabel= Y,
        width= 0.9\textwidth,
        height = 0.6\textwidth,
        ymajorgrids = true,
        bar width = 0.5mm,
        xticklabels = \empty,
        extra x ticks = {0,1,2,3,4},
        extra x tick labels = {0,1,2,3,4},
        legend pos=north east,
        ]

        \addplot coordinates {
            (0,2)
            (1,6)
            (2,6)
            (3,6)
            (4,6)
        };
        \addplot coordinates {
            (0,5)
            (1,5)
            (2,5)
            (3,5)
            (4,5)
        };
        \addlegendimage{empty legend}
        \addlegendentry{$A$}
        \addlegendentry{$B$}
    \end{axis} 
    \end{tikzpicture}
    \caption{Subfigure}
  \end{subfigure}
  \caption{Main Caption}
\end{figure}
\end{document}

演示

答案2

这是一份包含群体地块的提案。

\documentclass[tikz,border=3.14mm]{standalone}
\usepackage{pgfplots} 
\usetikzlibrary{pgfplots.groupplots}
\pgfplotsset{compat=1.16}
\begin{document}
\begin{tikzpicture}
  \begin{groupplot}[
    group style={
      group name=my plots,
      group size=2 by 1,
      ylabels at=edge left,
      xlabels at=edge bottom,
      horizontal sep=2cm,
    },width= 0.45\textwidth,
     height = 0.6\textwidth,
    ]

    \nextgroupplot[
    axis x line=top,
        ybar,
        xlabel = $X$,
        xmin = -0.3,xmax = 4.5,
        ymin = 0, ymax = 10,
        axis x line* = bottom,
        axis y line* = left,
        ylabel= $Y$,       
        ymajorgrids = true,
        bar width = 3mm,
        xticklabels = \empty,
        extra x ticks = {0,1,2,3,4},
        extra x tick labels = {0,1,2,3,4},
        legend pos=north east,
    ]
        \addplot coordinates {
            (0,2)
            (1,6)
            (2,6)
            (3,6)
            (4,6)
        };
        \addplot coordinates {
            (0,2)
            (1,6)
            (2,6)
            (3,6)
            (4,6)
        };
        \addlegendimage{empty legend}
        \addlegendentry{$A$}
        \addlegendentry{$B$}
    \nextgroupplot[
    axis x line=top,
        ybar,
        xlabel =$X$,
        xmin = -0.3,xmax = 4.5,
        ymin = 0, ymax = 10,
        axis x line* = bottom,
        axis y line* = left,
        ylabel=$Y$,
        ymajorgrids = true,
        bar width = 3mm,
        xticklabels = \empty,
        extra x ticks = {0,1,2,3,4},
        extra x tick labels = {0,1,2,3,4},
        legend pos=north east,
    ]
        \addplot coordinates {
            (0,5)
            (1,5)
            (2,5)
            (3,5)
            (4,5)
        };
        \addplot coordinates {
            (0,2)
            (1,6)
            (2,6)
            (3,6)
            (4,6)
        };
        \addlegendimage{empty legend}
        \addlegendentry{$A$}
        \addlegendentry{$B$}
    \end{groupplot} 
    \end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容