pgfplots:剪切特定区域之外的所有内容

pgfplots:剪切特定区域之外的所有内容

我想向直方图的每一列添加信息。every node near coord可能是最好的方法。它适用于条形图,但不适用于直方图:

\documentclass{article}

\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usetikzlibrary{pgfplots.statistics}

\begin{document}

\begin{tikzpicture}
\begin{axis}[
ybar interval,
xticklabel=\pgfmathprintnumber\tick--\pgfmathprintnumber\nexttick,
% Code to generate labels
nodes near coords={
\pgfmathprintnumber{\pgfkeysvalueof{/data point/y}}
},
every node near coord/.style={
anchor=south,xshift=9mm,
},
%
]
\addplot+[hist={bins=3}]
table[row sep=\\,y index=0] {
data\\
1\\ 2\\ 1\\ 5\\ 4\\ 10\\
7\\ 10\\ 9\\ 8\\ 9\\ 9\\
};
\end{axis}
\end{tikzpicture}

\end{document}

我以前常常every node near coord/.style转移标签。

问题是我有四个标签为了三列/条

在此处输入图片描述

此外,我还想将标签居中。解决方法是进行手动调整(every node near coord/.style),就像所示示例一样。

更新

或许剪裁有可能。有没有办法剪掉标签(本例中是右边的 7)?

像这样:

在此处输入图片描述

答案1

标签所需的 xshift可以通过轴的、的数量和的值(在新的宏中定义)nodes near coords来计算。widthbinsenlarge x limits\enlargexlimits

nodes near coords*/.add code={}{\tikzset{every node/.append style={xshift={
  (\pgfkeysvalueof{/pgfplots/width}-45pt) % every plot is 45pt smaller then the width
  /(1+2*\enlargexlimits) % correction for enlarge x limits
  /\pgfkeysvalueof{/pgfplots/hist/bins} % number of bins
  /2% shift only half of bin width
}}}},

如果您想要剪切绘图框右侧的所有内容,您可以用页面颜色填充该区域并重新计算整个绘图的边界框。

因此我定义一个宏

\newcommand\clipright[1][white]{
  \fill[#1](current axis.south east)rectangle(current axis.north-|current axis.outer east);
  \pgfresetboundingbox
  \useasboundingbox(current axis.outer south west)rectangle(current axis.outer north-|current axis.east);
}

第二个宏用于显示绘图的结果边界框

\newcommand\showbb{
  \draw[red,dashed](current bounding box.south west)rectangle(current bounding box.north east);
}

在此处输入图片描述

\documentclass[margin=5mm]{standalone}

\usepackage{pgfplots}
\pgfplotsset{compat=newest} % notice: version 1.10 or later is needed

\newcommand*\enlargexlimits{.1} % default value for enlarge x limits 

\pgfplotsset{%
  hist nodes near coords/.style={%
    nodes near coords*/.add code={}{\tikzset{every node/.append style={xshift={
      (\pgfkeysvalueof{/pgfplots/width}-45pt) % every plot is 45pt smaller then the width
      /(1+2*\enlargexlimits) % correction for enlarge x limits
      /\pgfkeysvalueof{/pgfplots/hist/bins} % number of bins
      /2% shift only half of bin width
    }}}},
    nodes near coords={%
      \pgfmathprintnumber{\pgfkeysvalueof{/data point/y}}
    }}
}

\newcommand\clipright[1][white]{
  \fill[#1](current axis.south east)rectangle(current axis.north-|current axis.outer east);
  \pgfresetboundingbox
  \useasboundingbox(current axis.outer south west)rectangle(current axis.outer north-|current axis.east);
}

\newcommand\showbb{
  \draw[red,dashed](current bounding box.south west)rectangle(current bounding box.north east);
}

\begin{document}

\begin{tikzpicture}

  \begin{axis}[
    ybar interval,
    xticklabel=\pgfmathprintnumber\tick--\pgfmathprintnumber\nexttick,
    enlarge x limits=\enlargexlimits
  ]
    \addplot+[hist={bins=3},hist nodes near coords]
      table[row sep=\\,y index=0] {
      data\\
      1\\ 2\\ 1\\ 5\\ 4\\ 10\\
      7\\ 10\\ 9\\ 8\\ 9\\ 9\\
    };
  \end{axis}
  \clipright
  \showbb
\end{tikzpicture}
\end{document}

但是如果增加箱子的数量,就会出现问题。以下是结果bin=9

在此处输入图片描述

为了解决这个问题,你可以enlarge x limits通过重新定义宏来减少\enlargexlimits

\begin{tikzpicture}
  \def\enlargexlimits{0.04} % changing the value of `enlarge x limits`
  \begin{axis}[
    ybar interval,
    xticklabel=\pgfmathprintnumber\tick--\pgfmathprintnumber\nexttick,
    enlarge x limits=\enlargexlimits
  ]

然后你会得到

在此处输入图片描述

或者\def\enlargexlimits{0}你将得到

在此处输入图片描述

注意:标签再次自动居中。


我以前的建议

为了抑制附加标签,pgfmath使用了 if-then-else 函数。语法x ? y : z含义是“如果X然后别的

\documentclass{article}

\usepackage{pgfplots}
\pgfplotsset{compat=newest} % notice: version 1.10 or later is needed

\newcommand*\NNC{\pgfmathprintnumber{\pgfkeysvalueof{/data point/y}}}
\newcommand*\enlargexlimits{.1} % default value for enlarge x limits 

\pgfplotsset{%
  hist nodes near coords/.style={%
    nodes near coords*/.add code={}{\tikzset{every node/.append style={xshift={
      (\pgfkeysvalueof{/pgfplots/width}-45pt) % every plot is 45pt smaller then the width
      /(1+2*\enlargexlimits) % correction for enlarge x limits
      /\pgfkeysvalueof{/pgfplots/hist/bins} % number of bins
      /2% shift only half of bin width
    }}}},
    nodes near coords={% 
      \pgfmathparse{
        \pgfkeysvalueof{/data point/x}<#1*\pgfkeysvalueof{/pgfplots/hist/data max}?%
          "\noexpand\NNC"%  if true print nodes near coords
          :% if false suppress the additional node near coords
      }\pgfmathresult%
    }},
  hist nodes near coords/.default={0.9}% if you set data max explicitly it will be 1
}

\begin{document}

\begin{tikzpicture}
  \begin{axis}[
    ybar interval,
    xticklabel=\pgfmathprintnumber\tick--\pgfmathprintnumber\nexttick,
    enlarge x limits=\enlargexlimits
  ]
    \addplot+[hist={bins=3},hist nodes near coords]
      table[row sep=\\,y index=0] {
      data\\
      1\\ 2\\ 1\\ 5\\ 4\\ 10\\
      7\\ 10\\ 9\\ 8\\ 9\\ 9\\
    };
  \end{axis}
\end{tikzpicture}
\begin{tikzpicture}
  \begin{axis}[
    ybar interval,
    xticklabel=\pgfmathprintnumber\tick--\pgfmathprintnumber\nexttick,
    enlarge x limits=\enlargexlimits
  ]
    \addplot+[hist={bins=2},hist nodes near coords]
      table[row sep=\\,y index=0] {
      data\\
      1\\ 2\\ 1\\ 5\\ 4\\ 10\\
      7\\ 10\\ 9\\ 8\\ 9\\ 9\\
    };
  \end{axis}
\end{tikzpicture}

\end{document}

在此处输入图片描述

答案2

另一种方法是使用clip命令。

由于原帖未提供data point/y信息,本解决方案使用自制替代品进行演示。 切片图像中的框架再次用于演示,可在命令中进行调整clippreaction=drawclip选项中删除以删除框架。clip=false解决方案中需要注意。

在此处输入图片描述

代码

\documentclass[border=10pt,varwidth]{standalone}%{article}

\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usetikzlibrary{pgfplots.statistics}

\begin{document}

Before:

\begin{tikzpicture}
\begin{axis}[
ybar interval,
clip=false,
xticklabel=\pgfmathprintnumber\tick--\pgfmathprintnumber\nexttick,
% Code to generate labels
%nodes near coords={
%\pgfmathprintnumber{\pgfkeysvalueof{/data point/y}}
%},
nodes near coords={\pgfmathprintnumber\pgfplotspointmeta\%},
every node near coord/.style={
anchor=south,xshift=9mm,
},
%
]
\begin{scope}
\addplot+[hist={bins=3}]
table[row sep=\\,y index=0] {
data\\
1\\ 2\\ 1\\ 5\\ 4\\ 10\\
7\\ 10\\ 9\\ 8\\ 9\\ 9\\
};
\end{scope}
\end{axis}
\end{tikzpicture}

After:

\begin{tikzpicture}
\begin{axis}[
ybar interval,
clip=false,
xticklabel=\pgfmathprintnumber\tick--\pgfmathprintnumber\nexttick,
% Code to generate labels
%nodes near coords={
%\pgfmathprintnumber{\pgfkeysvalueof{/data point/y}}
%},
nodes near coords={\pgfmathprintnumber\pgfplotspointmeta\%},
every node near coord/.style={
anchor=south,xshift=9mm,
},
%
]
\begin{scope}
\clip[preaction=draw] (axis cs: -1,-0.1) rectangle (axis cs:11,8);  % remove preaction=draw to remove the frame
\addplot+[hist={bins=3}]
table[row sep=\\,y index=0] {
data\\
1\\ 2\\ 1\\ 5\\ 4\\ 10\\
7\\ 10\\ 9\\ 8\\ 9\\ 9\\
};
\end{scope}
\end{axis}
\end{tikzpicture}

\end{document}

相关内容