如何让标签移动到图表中间

如何让标签移动到图表中间

1 如何让整个图表在我的页面中亮起?我正在使用\documentclass[12pt,oneside]{book}

  1. 如何使图表标签居中?我已将south east其从 改为east,但没有效果。

    legend style={cells={align=right, font=\small}}, legend pos=north,

图表 在此处输入图片描述

梅威瑟:

\documentclass[12pt,oneside]{book} 

% \documentclass[tikz,border={10pt 20pt 5pt 5pt}]{standalone}

\usepackage{pgfplots}
% \pgfplotsset{compat=1.5}   % <---
\begin{document}
 \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 
    11  100     0
    12  100     0
    13  100     0
    14  95      5
    15  95      5
    16  84      16
    17  68      32
    18  84      16
    19  95      5   
    20  89      11
        }\mydata

  \begin{axis}[
    width=0.9\linewidth,
    height=.8\linewidth,
    ybar=0.5mm,         % <--- distance between bars (shift bar)
    bar width=2mm,    % <--- width of bars
    % legend style={legend columns=-1,
    %   outer sep=1mm,    % <---
    %   font=\scriptsize, % <---
    %   anchor=south,
    %   at={(1.2,1)},
    %               },
   legend style={cells={align=right, font=\small}}, 
   legend pos=north,                  
    nodes near coords,
    nodes near coords style={font=\scriptsize, inner sep=2pt}, % <---
    nodes near coords align={vertical},
    ylabel={Classification Accuracy (\%)},
    xlabel={Test Case},
    ymin=0, ymax=100,     % <---              
    xtick=data,
    scale only axis,     % <---
        ]
    \addplot table[x=interval,y=carT]{\mydata};
    \addplot table[x=interval,y=carD]{\mydata};
    \legend{Correct, Incorrect}
    \end{axis}

    \end{tikzpicture}

\subsection{The Results}
Testing123 Testing123 Testing123

\end{document}

答案1

让我详细阐述一下我的评论。代码中的注释表明了代码的更改及其含义:

\documentclass[12pt,oneside]{book} 
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}              % <---

%---------------- show page layout. don't use in a real document!
\usepackage{showframe}
\renewcommand\ShowFrameLinethickness{0.15pt}
\renewcommand*\ShowFrameColor{\color{red}}
%---------------------------------------------------------------%

\begin{document}
    \begin{center}
        \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
    11  100      0
    12  100      0
    13  100      0
    14   95      5
    15   95      5
    16   84     16
    17   68     32
    18   84     16
    19   95      5
    20   89     11
        }\mydata

    \begin{axis}[
    width=\linewidth,   % <---
    height=.8\linewidth,
    ybar=0.3mm,         % distance between bars (shift bar)
    bar width=2.2mm,    % 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={\%},
    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{Trips, Distance}
    \end{axis}
\end{tikzpicture}
    \end{center}
\end{document}

这使:

在此处输入图片描述

(红线表示页面布局)

相关内容