组合条形图和折线图

组合条形图和折线图

我正在尝试在 LaTeX 中同时制作条形图和折线图。条形图表示游客人数(值将位于左侧的 y 轴上,标记为“百万人”),折线图表示收入(值将位于右侧的 y 轴上,标记为“十亿美元”)。

我不知道如何制作折线图。我还需要此图表的图例。

我当前的图表是

\documentclass{standalone}
\usepackage{tikz}
\tikzset{
  font={\fontsize{8.5pt}{12}\selectfont}}
\usepackage{adjustbox}
\usepackage{pgfplots}
\usepackage[proportional]{erewhon}

\begin{document}

\pgfplotstableread[row sep=\\,col sep=&]{
    Year & Tourists \\
    2010     & 957 \\
    2011     & 1004 \\
    2012     & 1055 \\
    2013     & 1107  \\
    2014     & 1151 \\
    2015     & 1208 \\
    2016     & 1250 \\
    2017     & 1339 \\     
    2018     & 1413 \\
    2019     & 1466 \\
    2020     & 409 \\
    2021     & 446 \\
    }\mydata

\begin{tikzpicture}
    \begin{axis}[
            ybar,
            bar width=.5cm,
            width=4.6in,
            height=2in,
            legend style={at={(0.5,0.95)},
                anchor=north,legend columns=-1},
            symbolic x coords={2010,2011,2012,2013,2014,2015,2016,2017,2018,2019,2020,2021},
            xtick align=inside,
            xtick=data,
            nodes near coords,
            nodes near coords align={vertical},
            ymin=0,ymax=2000,
            ylabel={million of people},
            /pgf/number format/.cd,
            use comma,
        ]
        \addplot [draw=black,fill=black!25] table[x=Year,y=Tourists]{\mydata};
    \end{axis}
\end{tikzpicture}
\end{document}

我的折线图数据是:

\pgfplotstableread[row sep=\\,col sep=&]{
    Year & Receipts \\
    2010     & 1001 \\
    2011     & 1119 \\
    2012     & 1162 \\
    2013     & 1255  \\
    2014     & 1314 \\
    2015     & 1219 \\
    2016     & 1248 \\
    2017     & 1344 \\     
    2018     & 1455 \\
    2019     & 1483 \\
    2020     & 548 \\
    2021     & 621 \\
    }\mydata2

非常感谢你的帮助。

答案1

我最终得到这个:

\documentclass{standalone}
\usepackage[proportional]{erewhon}
\usepackage{tikz}
\tikzset{
  font={\fontsize{8.5pt}{12}\selectfont}}
\usepackage{adjustbox}
\usepackage{pgfplots}

\pgfplotsset{
    compat=1.16,
    % created a style for the common `axis' options
    my axis style/.style={
        width=4.6in,
        height=2in,
        bar width=0.25in,
        symbolic x coords={2010,2011,2012,2013,2014,2015,2016,2017,2018,2019,2020,2021},
        ymin=0,
        legend style={
            at={(0.5,1.05)},    % <-- adapted
            anchor=south,       % <-- changed from `north'
            legend columns=2,
        },
        ylabel={million arrivals},
        xtick=data,
        xtick align=inside,
        xtick=data,
        %axis lines*=left,
        %ymajorgrids,
        %
        ymin=0,ymax=1750,
        ytick distance=250,
        /pgf/number format/.cd,
        use comma,
    },
    % new style for axis on right
    my axis style 2/.style={
        my axis style,
        axis lines*=right,
        xticklabels={},
        ylabel={billion US\$},
        tickwidth=0pt,
        ymin=0,ymax=1750,
        ytick distance=250,
        grid=none,
        /pgf/number format/.cd,
        use comma,
    },
    % created a style for the common `ybar' options
    my ybar style/.style={
        ybar,
        my ybar legend,            % <-- change legend image accordingly
        #1!50!black,
        fill=white!70!black,
    },
    my temp plot/.style={
       thick,
       black
    },
    my ybar legend/.style={
        /pgfplots/legend image code/.code={
            \draw [
            ##1,
            /tikz/.cd,
            yshift=-0.25em,
            ] (0cm,0cm) rectangle (3pt,0.8em);
        },
    },
}

\pgfplotstableread{
    x     Tourists
    2010  957
    2011  1004
    2012  1055
    2013  1107
    2014  1151
    2015  1208
    2016  1250
    2017  1339
    2018  1413
    2019  1466
    2020  409
    2021  446
}{\loadedtablerefpr}

\pgfplotstableread{
    x     Receipts
    2010  1001
    2011  1119
    2012  1162
    2013  1255
    2014  1314
    2015  1219
    2016  1248
    2017  1344     
    2018  1455
    2019  1483
    2020  548
    2021  621
}{\loadedtablereftemp}

\begin{document}
        \begin{tikzpicture}
        \begin{axis}[
        my axis style,
        ]
        \addplot [
        my ybar style=red!50!black,
        ] table [y=Tourists] {\loadedtablerefpr}; \ref{bars}

        \addlegendimage{my temp plot}
        \legend{Number of Arrivals, Receipts}
        \end{axis}
        \begin{axis}[my axis style 2]
        \addplot [my temp plot, draw=black, y = Receipts] table {\loadedtablereftemp};
        \end{axis}
        \end{tikzpicture}

\end{document}

相关内容