考虑以下图表(下面的 MWE):
我有一些疑问。
- [解决了]
1
中场休息后为什么还有一段额外时间[10,11)
? - [请参阅下面的编辑]如何使最后一个间隔关闭?我的意思是,我希望有
[9,10]
值4
(我只是将其[10,11)
用于计算最后一次出现的情况)。 - [解决了]如果可能的话,我希望将值准确地写在条形图上,即对齐在中心(稍微向右移动)。
我尝试寻找一些类似的解决方案,但没有进展。
平均能量损失
\documentclass[border=1pt]{standalone}
\usepackage[utf8]{inputenc}
\usepackage{pgfplots}
\usepgfplotslibrary{statistics}
\pgfplotsset{%
compat = 1.12, % I am not sure
major grid style = {color = blue!80, line width = .6pt},
every axis title/.append style = {font = \bfseries},
}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
title={Title Here},
ybar interval,
height = 6cm,
width = 18cm,
xmajorgrids = false,
ymajorgrids = true,
major tick length = 0pt,
minor tick length = 0pt,
xticklabel={%
$ [ \pgfmathprintnumber\tick , \pgfmathprintnumber\nexttick ) $%
},
axis y line = left,
axis x line = bottom,
every outer y axis line/.append style = {-},
every outer x axis line/.append style = {-},
xlabel = {faa},
xlabel style = {at = {(.5,-.2)}, anchor = north},% I'd like bold also
ylabel = {foo},
every node near coord/.append style = {%
anchor = south west,
black,
% I'd like bold/italic
},
nodes near coords,
ymin = 0, ymax = 9,
xmin = 0, xmax = 11.5,
]
%%
\addplot+[%
hist={% bins = 9, data max = 10 destroy everything
bins = 10,
data min = 1,
data max = 11,
},
fill = cyan!50,
]
table[row sep=\\, y index=0]{%
data\\%
1\\%
2\\2\\%
3\\3\\3\\%
4\\4\\%
5\\5\\5\\5\\5\\5\\5\\5\\%
6\\6\\6\\6\\6\\6\\%
7\\7\\7\\7\\%
8\\8\\8\\%
9\\9\\9\\
10\\%
};
\end{axis}
\end{tikzpicture}
\end{document}
编辑:对于 2. 我做的这个技巧并不是非常漂亮,但它非常合适。
\node[fill=white,anchor=north,inner sep=1pt] at (11.05,-2pt) {$[9,10]$};
答案1
如果这个答案有用,请去这里并标记那些解决上述问题中描述的关键问题的答案。
Salim 刚刚发布的评论提供了指向您问题的重要指导的链接。在那里,的作者pgfplots
写道,该额外标签的存在很可能是一个错误。不过,其他人(esdd 和 jesse)能够开发出解决方法。我在这里回收了该代码,以生成我认为您试图生成的内容,以及其他请求。
一些更改包括\pgfplotsset
来自所引用问题的代码,指定ymin
和ymax
,更改nodes near coords
为hist nodes near coords
,添加font=\bfseries
定义xlabel style
,更改bins=10
为bins=9
并删除data max = 10
。
通过这些我们得到:
这是代码:
\documentclass[border=1pt]{standalone}
\usepackage[utf8]{inputenc}
\usepackage{pgfplots}
\usepgfplotslibrary{statistics}
%https://tex.stackexchange.com/questions/181061/pgfplots-clipping-everything-outside-a-specific-area
\newcommand*\NNC{\pgfmathprintnumber{\pgfkeysvalueof{/data point/y}}}
\newcommand*\enlargexlimits{.1} % default value for enlarge x limits
\pgfplotsset{%
compat=newest,
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={1}% if you set data max explicitly it will be 1
}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
title={Title Here},
ybar interval,
ymin=0,
ymax=10,
height = 6cm,
width = 18cm,
enlarge x limits=\enlargexlimits, %<-- setup for label alignment
xmajorgrids = false,
ymajorgrids = true,
major tick length = 0pt,
minor tick length = 0pt,
xticklabel={(\pgfmathprintnumber\tick--\pgfmathprintnumber\nexttick)},
axis y line = left,
axis x line = bottom,
x tick label as interval,
every outer y axis line/.append style = {-},
every outer x axis line/.append style = {-},
xlabel = {faa},
xlabel style = {at = {(.5,-.2)}, anchor = north,font=\bfseries}, % <-- font=\bfseries
ylabel = {foo},
ylabel style = {font=\bfseries}
]
%%
\addplot+[%
hist={%
bins = 9, <-- changed 10 to 9
data min = 1,
% data max = 10, % <-- remove
},
fill = cyan!50,
hist nodes near coords % <-- new
]
table[row sep=\\, y index=0]{%
data\\
1\\
2\\2\\
3\\3\\3\\
4\\4\\
5\\5\\5\\5\\5\\5\\5\\5\\
6\\6\\6\\6\\6\\6\\
7\\7\\7\\7\\
8\\8\\8\\
9\\9\\9\\
10\\
};
\end{axis}
\end{tikzpicture}
\end{document}