给出这样的图片:
\documentclass[12pt]{article}
\usepackage{pgfplots}
\usepgfplotslibrary{external}
\tikzexternalize
\pgfplotsset{compat=newest}
\pgfplotstableread[col sep=comma]{
netname,index,gxccmean,gxprmean,gxssspmean
COMLJ,1,431.14445,919.70278,266.85096
SOCSINAWEIBO,2,20723.41791,nan,2827.05371
RGGN224S0,3,nan,12293.6209,340.84044
COMORKUT,4,1139.28039,2503.06515,622.09671
}\realtable
\begin{document}
\begin{figure}[h]
\centering
\begin{tikzpicture}
\begin{axis}[
columns/realname/.style={string type},
legend style={at={(1.5,1)},anchor=north},
xtick style={draw=none},
ybar=0pt,
log origin=infty,
xtick style={draw=none},
xticklabel style={rotate=90,inner sep=0mm},
xtick=data,
xticklabels from table={\realtable}{netname},
ymode=log,
]
\addplot table [x=index, y=gxccmean] {\realtable};
\addlegendentry{Connected Components};
\addplot table [x=index, y=gxprmean] {\realtable};
\addlegendentry{Page Rank};
\addplot table [x=index, y=gxssspmean] {\realtable};
\addlegendentry{Single Source Shortest Paths};
\end{axis}
\end{tikzpicture}
\end{figure}
\end{document}
每当“gxccmean”y 值为 NaN 时,xticklabels 就不会被绘制。似乎 pgfplots 只考虑第一个 addplot 来绘制刻度标签。如果我更改 addplot 表命令的顺序,标签就会正确放置(第二个 addplot 的 y 值不是 NaN)。此外,pgfplots 将标签移至右侧的刻度。
我不想改变 addplots 的顺序。因此,我想知道是否有办法强制 xticklabels 绘图:
答案1
如果您的数据要按照表格中出现的顺序绘制,并且index
不使用该列来调整条形的顺序或位置,则您可以简单地使用
xtick = {1,...,20}
在哪里20
只是一个大于表中行数的任意数字。这将在 x 的每个整数值(介于 1 到 20 之间)处生成一个刻度位置。
请注意,在这种情况下,您也可以完全取消该index
列,并用它\addplot table [x expr=\coordindex] {...}
来自动生成 x 位置。
\documentclass[12pt]{article}
\usepackage{pgfplots}
\usepgfplotslibrary{external}
\tikzexternalize
\pgfplotsset{compat=newest}
\pgfplotstableread[col sep=comma]{
netname,index,gxccmean,gxprmean,gxssspmean
COMLJ,1,431.14445,919.70278,266.85096
SOCSINAWEIBO,2,20723.41791,nan,2827.05371
RGGN224S0,3,nan,12293.6209,340.84044
COMORKUT,4,1139.28039,2503.06515,622.09671
}\realtable
\begin{document}
\begin{figure}[h]
\centering
\begin{tikzpicture}
\begin{axis}[
columns/realname/.style={string type},
legend style={at={(1.5,1)},anchor=north},
xtick style={draw=none},
ybar=0pt,
log origin=infty,
xtick style={draw=none},
xticklabel style={rotate=90,inner sep=0mm},
xtick={1,...,20},
xticklabels from table={\realtable}{netname},
ymode=log,
]
\addplot table [x=index, y=gxccmean] {\realtable};
\addlegendentry{Connected Components};
\addplot table [x=index, y=gxprmean] {\realtable};
\addlegendentry{Page Rank};
\addplot table [x=index, y=gxssspmean] {\realtable};
\addlegendentry{Single Source Shortest Paths};
\end{axis}
\end{tikzpicture}
\end{figure}
\end{document}