pgfplots 中是否有内置方法来制作地毯图?地毯是指轴内侧的刻度标记。图中每个数据点都有一个刻度标记。
像这样:
(来源:pydata.org)
或者甚至是这样的:
?
我不知道是不是我没有正确阅读手册,或者是它不存在。
谢谢
。
编辑:这是我制作的一些直方图数据。我添加了我认为应该产生刻度的代码:绘制二维高斯样本并且我改变了填充不透明度来查看是否有帮助,但并没有帮助。
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
xlabel={$x$},
ylabel={Count},
axis lines = left,
every outer x axis line/.append style= {-},
every outer y axis line/.append style= {-},
xmin=-3, xmax=3,
ybar=0pt, bar width=1,
xtick=data,
xticklabel=\empty,
extra x ticks={-3,...,3},
every extra x tick/.style={
tick align=outside,
xticklabel=\pgfmathprintnumber{\tick}
},
]
\addplot[
black,
fill=red,
fill opacity=0.02,
hist={bins=12},
]
table[row sep=\\,y index=0] {
data\\
-0.3662\\ 0.8510\\ -0.6391\\ -1.8496\\ 1.3160\\ -0.3006\\ -0.1287\\ 0.9324\\ 0.4970\\ -2.1474\\
0.0184\\ 0.2656\\ -0.4691\\ 0.4598\\ -0.3074\\ -0.9293\\ 0.6791\\ -0.9676\\ -0.5833\\ 2.7928\\
0.4582\\ 0.7967\\ 0.8900\\ -0.9640\\ 0.1757\\ -0.8618\\ 0.8212\\ 0.0399\\ 0.9268\\ -0.5465\\
1.4702\\ -2.0286\\ 0.4796\\ -1.3354\\ -1.8256\\ -0.9834\\ 0.5041\\ -0.5414\\ 1.1379\\ 1.2965\\
0.1740\\ 0.4718\\ 0.9251\\ -0.2399\\ 0.0854\\ -2.7529\\ -0.1187\\ 0.8095\\ -0.3766\\ 0.8983\\};
\end{axis}
\end{tikzpicture}
\end{document}
答案1
“问题”在于您只绘制了直方图,因此您只能获得xtick
它们的 s。要显示xtick
数据的 s,您必须先绘制它们。
要了解更多详细信息和改进,请查看代码中的注释。
% used PGFPlots v1.15
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
% use this `compat' level or higher to use the "advanced" positioning
% of the axis labels
\pgfplotsset{compat=1.3}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
xlabel={$x$},
ylabel={Count},
axis lines=left,
every outer x axis line/.append style={-},
every outer y axis line/.append style={-},
xmin=-3,
xmax=3,
ybar=0pt,
bar width=1,
xtick=data,
xticklabel=\empty,
extra x ticks={-3,...,3},
% ---------------------------------------------------------------------
% to distinguish between the ticks drawn for the labels and the one
% to mark data points, use different styles for them, e.g.
tick align=inside,
xtick style={
draw=red,
},
extra x tick style={
tick align=outside,
xticklabel=\pgfmathprintnumber{\tick},
xtick style={
draw=black!50,
},
},
% draw axis on top of the plots, so they are not "overdrawn"
axis on top,
% ---------------------------------------------------------------------
]
% plot the raw data (without the `hist') to produce the ticks
% (`xtick=data' only accounts for the *first* plot)
\addplot [
draw=none,
] table [row sep=\\,y expr=0] {
data\\
-0.3662\\ 0.8510\\ -0.6391\\ -1.8496\\ 1.3160\\ -0.3006\\ -0.1287\\ 0.9324\\ 0.4970\\ -2.1474\\
0.0184\\ 0.2656\\ -0.4691\\ 0.4598\\ -0.3074\\ -0.9293\\ 0.6791\\ -0.9676\\ -0.5833\\ 2.7928\\
0.4582\\ 0.7967\\ 0.8900\\ -0.9640\\ 0.1757\\ -0.8618\\ 0.8212\\ 0.0399\\ 0.9268\\ -0.5465\\
1.4702\\ -2.0286\\ 0.4796\\ -1.3354\\ -1.8256\\ -0.9834\\ 0.5041\\ -0.5414\\ 1.1379\\ 1.2965\\
0.1740\\ 0.4718\\ 0.9251\\ -0.2399\\ 0.0854\\ -2.7529\\ -0.1187\\ 0.8095\\ -0.3766\\ 0.8983\\
};
% then plot the hist
\addplot[
draw=black,
fill=red,
fill opacity=0.2,
hist={bins=12},
] table [row sep=\\,y index=0] {
data\\
-0.3662\\ 0.8510\\ -0.6391\\ -1.8496\\ 1.3160\\ -0.3006\\ -0.1287\\ 0.9324\\ 0.4970\\ -2.1474\\
0.0184\\ 0.2656\\ -0.4691\\ 0.4598\\ -0.3074\\ -0.9293\\ 0.6791\\ -0.9676\\ -0.5833\\ 2.7928\\
0.4582\\ 0.7967\\ 0.8900\\ -0.9640\\ 0.1757\\ -0.8618\\ 0.8212\\ 0.0399\\ 0.9268\\ -0.5465\\
1.4702\\ -2.0286\\ 0.4796\\ -1.3354\\ -1.8256\\ -0.9834\\ 0.5041\\ -0.5414\\ 1.1379\\ 1.2965\\
0.1740\\ 0.4718\\ 0.9251\\ -0.2399\\ 0.0854\\ -2.7529\\ -0.1187\\ 0.8095\\ -0.3766\\ 0.8983\\
};
\end{axis}
\end{tikzpicture}
\end{document}