两个彼此相邻的条形图,宽度不同,高度相同

两个彼此相邻的条形图,宽度不同,高度相同

嗨,我正在尝试在一页上并排创建两个图表。我希望第二个图表的宽度小于第一个图表,但高度不小于第一个图表。我希望宽度更小,因为第二个图表中的 x 轴刻度较少。

由于某种原因,图表决定将条形图一直向左和向右移动,而不是在图表中线性分布,我也希望对此进行修复。

这是我的main.tex文件:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{pdflscape}
\usepackage[table]{xcolor}
\usepackage[landscape, a4paper, bottom=0.0in, margin=0in]{geometry}
\usepackage{pgfplots}
\usepackage{xcolor}
\usepackage{array}
\usepackage{caption}
\usepackage{subcaption}


%Customization
\newcolumntype{L}[1]{>{\raggedright\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}
\newcolumntype{C}[1]{>{\centering\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}
\newcolumntype{R}[1]{>{\raggedleft\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}
\pgfplotsset{compat=newest} % Allows to place the legend below plot
\usepgfplotslibrary{units} % Allows to enter the units nicely


\pagenumbering{gobble}
\definecolor{customblue}{RGB}{21,155,151}
\definecolor{custompink}{RGB}{230,55,148}


\title{test}
\author{sinasohi}
\date{January 2017}

\begin{document}
%\maketitle

\include{Page1}
\include{Page3}

\end{document}

这是我的代码Page3.tex,将其包含在内main.tex

\pgfplotstableread[row sep=\\,col sep=&]{
    interval    & a     & b     \\
    K74         & 32.9  & 27.5  \\
    K76         & 23.5  & 22.5  \\
    K75         & 25.7  & 25.0  \\
    K77         & 22.3  & 30    \\
    K89-90      & 30.6  & 30    \\
    K92         & 19.3  & 15    \\
    }\mydata

\pgfplotstableread[row sep=\\,col sep=&]{
    interval        & a     & b     \\
    K78             & 18.8  & 20  \\
    K85, K86, K87   & 66.3  & 60  \\
    }\yourdata

\begin{figure}
\centering
\begin{minipage}{.4\textwidth}
\caption*{\textbf{Kardiovaskulære diagnoser}}
\begin{tikzpicture}
    \begin{axis}[
            legend style={at={(0.5,1)}, text width=6em, text height=1.5ex, anchor=north,legend columns=-1},
            ybar,
            bar width=.6cm,
            symbolic x coords={K74,K76,K75,K77,K89-90,K92},
            xtick=data,
            ymin=0, %ymax=40,
            width=1\textwidth,
            nodes near coords,
            every node near coord/.append style={color=black}
        ]

        \addplot[red!20!customblue,fill=customblue!80!white] table[x=interval,y=a]{\mydata};
        \addplot[red!20!custompink,fill=custompink!80!white] table[x=interval,y=b]{\mydata};
        \legend{Klinik 12345, Landsrapport}
    \end{axis}
\end{tikzpicture}
\end{minipage}
\begin{minipage}{.4\textwidth}
\caption*{\textbf{Yderligere diagnoser}}
\begin{tikzpicture}
    \begin{axis}[
            %legend style={legend columns=1,at={(1,1)},anchor=north west},
            legend style={at={(0.5,1)}, text width=6em, text height=1.5ex, anchor=north,legend columns=-1},
            ybar,
            bar width=.6cm,
            symbolic x coords={K78, {K85, K86, K87}},
            xtick=data,
            ymin=0, %ymax=40,
            width=1\textwidth,
            nodes near coords,
            every node near coord/.append style={color=black}
        ]

        \addplot[red!20!customblue,fill=customblue!80!white] table[x=interval,y=a]{\yourdata};
        \addplot[red!20!custompink,fill=custompink!80!white] table[x=interval,y=b]{\yourdata};
        \legend{Klinik 12345, Landsrapport}
    \end{axis}
\end{tikzpicture}
\end{minipage}
\end{figure}

它看起来是这样的:

在此处输入图片描述

答案1

height除了 之外,您还可以设置width。对于第二轴上的条形图的放置,请使用 之类的东西enlarge x limits=0.7。不知道为什么它最初做得不好。

我将使用虽然来设置它groupplot,删除minipages 并使用而title不是。axis\caption*

另外,尽量避免多次加载包。您有xcolor两次。不过,它也是通过 加载的pgfplots,因此,除非您colortbl在文档中的其他位置使用这些功能,否则删除它也不会有什么坏处\usepackage[table]{xcolor}。我还从 更改为compat=newest。Christian compat=1.14Feuersänger 建议使用显式值,这样如果您更新 ,您的图形就不会改变pgfplots

在此处输入图片描述

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{pdflscape}
\usepackage[table]{xcolor}
\usepackage[landscape, a4paper, bottom=0.0in, margin=0in]{geometry}
\usepackage{pgfplots}

\pgfplotsset{compat=1.14} % Allows to place the legend below plot
\usepgfplotslibrary{
  units, % Allows to enter the units nicely
  groupplots
}

\definecolor{customblue}{RGB}{21,155,151}
\definecolor{custompink}{RGB}{230,55,148}

\begin{document}
\pgfplotstableread[row sep=\\,col sep=&]{
    interval    & a     & b     \\
    K74         & 32.9  & 27.5  \\
    K76         & 23.5  & 22.5  \\
    K75         & 25.7  & 25.0  \\
    K77         & 22.3  & 30    \\
    K89-90      & 30.6  & 30    \\
    K92         & 19.3  & 15    \\
    }\mydata

\pgfplotstableread[row sep=\\,col sep=&]{
    interval        & a     & b     \\
    K78             & 18.8  & 20  \\
    K85, K86, K87   & 66.3  & 60  \\
    }\yourdata

\begin{figure}
\centering
\begin{tikzpicture}
    \begin{groupplot}[
            group style={group size=2 by 1},
            legend style={at={(0.5,1)}, text width=6em, text height=1.5ex, anchor=north,legend columns=-1},
            ybar,
            /tikz/bar width=.6cm,            
            xtick=data,
            ymin=0, %ymax=40,
            ,height=10cm,
            nodes near coords,
            every node near coord/.append style={color=black}            
        ]

\nextgroupplot[
   title=\textbf{Kardiovaskulære diagnoser},
   width=.55\textwidth,
   symbolic x coords={K74,K76,K75,K77,K89-90,K92}
]
        \addplot[red!20!customblue,fill=customblue!80!white] table[x=interval,y=a]{\mydata};
        \addplot[red!20!custompink,fill=custompink!80!white] table[x=interval,y=b]{\mydata};
        \legend{Klinik 12345, Landsrapport}

\nextgroupplot[
    symbolic x coords={K78, {K85, K86, K87}},
    width=.3\textwidth,
    enlarge x limits=0.7,
    title=\textbf{Yderligere diagnoser}
    ]

        \addplot[red!20!customblue,fill=customblue!80!white] table[x=interval,y=a]{\yourdata};
        \addplot[red!20!custompink,fill=custompink!80!white] table[x=interval,y=b]{\yourdata};
        \legend{Klinik 12345, Landsrapport}
\end{groupplot}
\end{tikzpicture}
\end{figure}
\end{document}

相关内容