绘制分组条形图时出现问题

绘制分组条形图时出现问题
  1. 我正在打印没有空格的 X 轴标签。
  2. 图例存在问题,它影响了延迟值
  3. 我如何保持值水平并放大它们以便更好地查看?

梅威瑟:

\documentclass{article}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\pgfplotsset{compat=newest}
\usepackage{caption}
\usepackage{mathtools}
\begin{document}
\thispagestyle{empty}
\pgfplotsset
 {
 compat=1.11,
 /pgfplots/ybar legend/.style={
 /pgfplots/legend image code/.code={%
 \draw[##1,/tikz/.cd,yshift=-0.25em]
 (0cm,0cm) rectangle (20pt,0.8em);},
 },
}
 \begin{figure}[h]
 \pgfplotstableread{
 %Proposed         %Sheng Lin
 0   43.5             127.6     
 1   15.2             39
 2   57.3             135.9
 }\dataset
 \begin{tikzpicture}
 \begin{axis}[
    ybar,
    bar width = 0.2,
    width=16cm,
    height=14cm,
    ymin=0,
    ymax=140,        
    ylabel={Delay in ps},
    xtick=data,
    xticklabels = 
    {
        \strut  Read 0
        \strut  Read 1
        \strut  Read 2
    },
    %xticklabel style={yshift=-10ex},
    major x tick style = {opacity=0},
    minor x tick num = 1,
    minor tick length=2ex,
    every node near coord/.append style=
    {
            anchor=west,
            rotate=90
    },
    legend entries={Delay of Proposed SRAM ,Delay of [1]},
    legend columns=3,
    legend style={draw=none,nodes={inner sep=3pt}},
    width=1.2\textwidth, 
    ]
  \addplot[draw=black,fill=blue!60, nodes near coords] table[x index=0,y index=1]    \dataset; %ano de 2013-2014
  \addplot[draw=black,fill=red!60, nodes near coords] table[x index=0,y index=2] \dataset; %ano de 2012-2013

  \end{axis}
  \end{tikzpicture}
  \captionsetup{justification=centerlast, margin=10ex, labelfont=sc, textfont=sc,   format=hang, labelformat=default, labelsep=endash, font=Large, name=Graph\,}


   \end{figure}

   \end{document}

答案1

您可以通过按键控制图例的位置at,如下所示

legend style={at={(0.5,1)},anchor=north,draw=none,nodes={inner sep=3pt}},   %% changed here

(0.5,1)根据需要调整值。

你缺少逗号,作为分隔符

xticklabels =
    {
        \strut  Read 0,     %% you need comma (,) at the end here and next 2 lines
        \strut  Read 1,
        \strut  Read 2,
    },

要使值保持水平,请将 90 更改为 0

every node near coord/.append style=
    {
            anchor=south,  %% changed
            rotate=0       %% changed
    },

还有其他一些小错误/变化,我已经在代码本身中进行了评论。

\documentclass[12pt]{article}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\pgfplotsset{compat=1.11}
\usepackage{caption}
\usepackage{mathtools}
\begin{document}
\thispagestyle{empty}
\pgfplotsset
 {
 /pgfplots/ybar legend/.style={
 /pgfplots/legend image code/.code={%
 \draw[##1,/tikz/.cd,yshift=-0.25em]
 (0cm,0cm) rectangle (20pt,0.8em);},
 },
}
\begin{figure}[h]
\pgfplotstableread{
%Proposed         %Sheng Lin
0   43.5             127.6
1   15.2             39
2   57.3             135.9
}\dataset
\begin{tikzpicture}
\begin{axis}[
    ybar,
    bar width = 1cm,
    width=\textwidth,                  %%% changed
%    height=14cm,                %% better not needed
    ymin=0,
    ymax=150,                    %% changed here
    ylabel={Delay in ps},
    xtick=data,
    xticklabels =
    {
        \strut  Read 0,     %% you need comma (,) at the end here and next 2 lines
        \strut  Read 1,
        \strut  Read 2,
    },
    %xticklabel style={yshift=-10ex},
    major x tick style = {opacity=0},
    minor x tick num = 1,
    minor tick length=2ex,
    every node near coord/.append style=
    {
            anchor=south,  %% changed
            rotate=0       %% changed
    },
    legend entries={Delay of Proposed SRAM, Delay of [1]},
    legend columns=2,      %% 2 instead of 3 here
    legend style={at={(0.5,1)},,anchor=north,draw=none,nodes={inner sep=3pt}},   %% changed here
%    width=1.2\textwidth,   Why second time?
    ]
  \addplot[draw=black,fill=blue!60, nodes near coords] table[x index=0,y index=1]    \dataset; %ano de 2013-2014
  \addplot[draw=black,fill=red!60, nodes near coords] table[x index=0,y index=2] \dataset; %ano de 2012-2013

  \end{axis}
  \end{tikzpicture}
  \captionsetup{justification=centerlast, margin=10ex, labelfont=sc, textfont=sc,   format=hang, labelformat=default, labelsep=endash, font=Large, name=Graph\,}


   \end{figure}

   \end{document}

在此处输入图片描述

相关内容