如何放大条形图?

如何放大条形图?

我想放大条形图,我的图表看起来很丑。

在此处输入图片描述

\documentclass[12pt,oneside]{book} 

\usepackage{siunitx}
\usepackage{pgfplots}
\pgfplotsset{compat=1.15}
\usetikzlibrary{arrows.meta,
                backgrounds,
                calc, chains,
                decorations.pathreplacing,
                fit,
                matrix,
                positioning,
                }

\usepackage{tabularx,seqsplit, caption} %for table spacing to second row
\usepackage{booktabs, ragged2e}       % Use booktabs rules and get rid of vertical rules, ragged2e to ragged text
\usepackage{makecell,siunitx} %for table spacing to second row
\usepackage{threeparttable} %to add footnote below table
\renewcommand{\TPTtagStyle}{\itshape} % optional
\usepackage{lipsum} % for filler text
\usepackage{makecell} % for bold in table using \small
\renewcommand\theadfont{\small\bfseries} % for bold in table using \small
\usepackage{tabulary,siunitx} 
\renewcommand\theadgape{}
\usepackage{graphicx}

\usepackage{tikz}
\usetikzlibrary{decorations.pathreplacing,calc, positioning,matrix,fit,calc, arrows.meta,
                backgrounds,
                chains}
\usepackage{pgfplots}
\pgfplotsset{compat=1.15}              

\begin{document}


\begin{figure}
\begin{tikzpicture}
\pgfplotstableread{
interval  carT  carD
    1    68    32
    2    94   6
    3    84   16
    4   100    0
    5   100     0
    6   83      17
    7   100     0
    8   100     0
    9   100     0
    10  89      11 
        }\mydata

    \begin{axis}[
    width=\linewidth,   % <---
    height=.6\linewidth,
    ybar=3mm,         % distance between bars (shift bar)
    bar width=5mm,    % width of bars
    legend style={legend columns=-1,    % <---
      outer sep=1mm,    % define space around legend
      font=\scriptsize, % see if `tiny is better suited 
      anchor=north,     % <---
      at={(0.5,1)},     % coordinate of legend (at top side of diagram)
                  },
    nodes near coords,
    nodes near coords style={font=\scriptsize, inner sep=2pt}, 
    nodes near coords align={vertical},
     ylabel={In ABC (\%)}, 
     xlabel={Test Case},
    ymin=0, ymax=120,   % <---
    xmin=0, xmax=21,    % <---
    xtick=data,
    %scale only axis,    
        ]
    \addplot table[x=interval,y=carT]{\mydata};
    \addplot table[x=interval,y=carD]{\mydata};
    \legend{Accuracy, Inaccuracy}
    \end{axis}
\end{tikzpicture}
\end{figure}


\begin{tikzpicture}
\begin{axis}[
    x tick label style={
        /pgf/number format/1000 sep=},
    ylabel=Year,
    enlargelimits=0.05,
    legend style={at={(0.5,-0.1)},
    anchor=north,legend columns=-1},
    ybar interval=0.7,
]
\addplot 
    coordinates {(2012,408184) (2011,408348)
         (2010,414870) (2009,412156)};
\addplot 
    coordinates {(2012,388950) (2011,393007) 
        (2010,398449) (2009,395972)};
\legend{Men,Women}
\end{axis}
\end{tikzpicture}

\end{document}

答案1

感谢@Schrödinger 的猫,我添加了评论。

  • 您可以简单地替换xmax=21xmax=11使图表水平方向变短。
  • 在为条形图添加标题方面,您可以title = My Graph Title添加\begin{axis}[...]
  • 如果您想在标题中添加逗号,则需要用括号将标题文本括起来,如下所示:title = {My Graph Title, Some Other Text}

这是一个完整且最小的例子:

输出

\documentclass[12pt]{article} 
\usepackage{pgfplots}
\pgfplotsset{compat=1.15}

\begin{document}
\begin{figure}
\begin{tikzpicture}
\pgfplotstableread{
interval  carT  carD
    1    68    32
    2    94   6
    3    84   16
    4   100    0
    5   100     0
    6   83      17
    7   100     0
    8   100     0
    9   100     0
    10  89      11 
        }\mydata

    \begin{axis}[
    title={The Graph Title, with, commas}, %%%%%%%%%%%%%% ADDED!
    width=\linewidth,   % <---
    height=.6\linewidth,
    ybar=3mm,         % distance between bars (shift bar)
    bar width=5mm,    % width of bars
    legend style={legend columns=-1,    % <---
      outer sep=1mm,    % define space around legend
      font=\scriptsize, % see if `tiny is better suited 
      anchor=north,     % <---
      at={(0.5,1)},     % coordinate of legend (at top side of diagram)
                  },
    nodes near coords,
    nodes near coords style={font=\scriptsize, inner sep=2pt}, 
    nodes near coords align={vertical},
     ylabel={In ABC (\%)}, 
     xlabel={Test Case},
    ymin=0, ymax=120,   % <---
    xmin=0, xmax=11,                      %%%%%%%%%%%%%%%% CHANGED!
    xtick=data,
    %scale only axis,    
        ]
    \addplot table[x=interval,y=carT]{\mydata};
    \addplot table[x=interval,y=carD]{\mydata};
    \legend{Accuracy, Inaccuracy}
    \end{axis}
\end{tikzpicture}
\end{figure}


\begin{tikzpicture}
\begin{axis}[
    x tick label style={
        /pgf/number format/1000 sep=},
    ylabel=Year,
    enlargelimits=0.05,
    legend style={at={(0.5,-0.12)}, % I also played with this (changed -0.1 to -0.12).
    anchor=north,legend columns=-1},
    ybar interval=0.7,
]
% add a "dummy" coordinate that will not show on the graph (2008, 0).
% If there is a cleaner approach, please edit the post :)
\addplot coordinates {(2012,408184) (2011,408348) (2010,414870) (2009,412156) (2008, 0)};
\addplot coordinates {(2012,388950) (2011,393007) (2010,398449) (2009,395972) (2008, 0)};
\legend{Men,Women}
\end{axis}
\end{tikzpicture}

\end{document}

相关内容