删除 x 标签和混淆

删除 x 标签和混淆

我对 LaTeX 还很陌生。我正在尝试弄清楚如何完全摆脱 x 值标签(因为我有密钥)。

\begin{tikzpicture}
\begin{axis}[
    width  = 0.85*\textwidth,
    height = 8cm,
    major x tick style = transparent,
    ybar=2*\pgflinewidth,
    xticklabels=\empty,
    bar width=14pt,
    ymajorgrids = true,
    ylabel = {Percentage},
    xlabel = {},
    symbolic x coords={Doramus,Refaltido,Misanit,Fanatha,Sonphay,Laoli,Tidanme},
    scaled y ticks = false,
    enlarge x limits=2,
    ymin=0,
    ymax=100,
    legend cell align=left,
    legend style={
            at={(.872,.5)},
            anchor=south,
            column sep=1ex
    }  
]

\addplot[style={green,fill=green}]
    coordinates {(Doramus,60)};
\addplot[style={orange,fill=orange}]
    coordinates {(Refaltido,17)};
\addplot[style={yellow,fill=yellow}]
    coordinates {(Misanit,6)};
\addplot[style={blue,fill=blue}]
    coordinates {(Fanatha,6)};
\addplot[style={purple,fill=purple}]
    coordinates {(Sonphay,5)};
\addplot[style={black,fill=black}]
    coordinates {(Laoli,4)};
\addplot[style={black,fill=white}]
    coordinates {(Tidanme,2)};
\legend{Doramus,Refaltido,Misanit,Fanatha,Sonphay,Laoli,Tidanme}
\end{axis}
\end{tikzpicture}

结果图像如下:

渲染图

我已经查找了解决方案并尝试执行xlabel = {}和,xticklabels=\empty但都不起作用。另外,为什么会显示重复的名称(Doramus 和 Tidanme)?

答案1

要删除xticklabels你必须使用xticklabel=\empty symbolic x coords={...}

在此处输入图片描述

\begin{document}
\begin{tikzpicture}
\begin{axis}[
    width  = 0.85*\textwidth,
    height = 8cm,
    major x tick style = transparent,
    ybar=2*\pgflinewidth,
    bar width=14pt,
    ymajorgrids = true,
    ylabel = {Percentage},
    xlabel = {},
    symbolic x coords={Doramus,Refaltido,Misanit,Fanatha,Sonphay,Laoli,Tidanme},
    scaled y ticks = false,
    xticklabel=\empty,% <- after symbolic x coords
    enlarge x limits=2,
    ymin=0,
    ymax=100,
    legend cell align=left,
    legend style={
            at={(.872,.5)},
            anchor=south,
            column sep=1ex
    }  
]
\addplot[style={green,fill=green}]
    coordinates {(Doramus,60)};
\addplot[style={orange,fill=orange}]
    coordinates {(Refaltido,17)};
\addplot[style={yellow,fill=yellow}]
    coordinates {(Misanit,6)};
\addplot[style={blue,fill=blue}]
    coordinates {(Fanatha,6)};
\addplot[style={purple,fill=purple}]
    coordinates {(Sonphay,5)};
\addplot[style={black,fill=black}]
    coordinates {(Laoli,4)};
\addplot[style={black,fill=white}]
    coordinates {(Tidanme,2)};
\legend{Doramus,Refaltido,Misanit,Fanatha,Sonphay,Laoli,Tidanme}
\end{axis}
\end{tikzpicture}
\end{document}

或者您可以使用xtick=\empty并删除major x tick style=transparentxticklabel=\empty

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.14}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
    width  = 0.85*\textwidth,
    height = 8cm,
    xtick=\empty,% <- added
    ybar=2*\pgflinewidth,
    bar width=14pt,
    ymajorgrids = true,
    ylabel = {Percentage},
    xlabel = {},
    symbolic x coords={Doramus,Refaltido,Misanit,Fanatha,Sonphay,Laoli,Tidanme},
    scaled y ticks = false,
    enlarge x limits=2,
    ymin=0,
    ymax=100,
    legend cell align=left,
    legend style={
            at={(.872,.5)},
            anchor=south,
            column sep=1ex
    }  
]
\addplot[style={green,fill=green}]
    coordinates {(Doramus,60)};
\addplot[style={orange,fill=orange}]
    coordinates {(Refaltido,17)};
\addplot[style={yellow,fill=yellow}]
    coordinates {(Misanit,6)};
\addplot[style={blue,fill=blue}]
    coordinates {(Fanatha,6)};
\addplot[style={purple,fill=purple}]
    coordinates {(Sonphay,5)};
\addplot[style={black,fill=black}]
    coordinates {(Laoli,4)};
\addplot[style={black,fill=white}]
    coordinates {(Tidanme,2)};
\legend{Doramus,Refaltido,Misanit,Fanatha,Sonphay,Laoli,Tidanme}
\end{axis}
\end{tikzpicture}
\end{document}

结果和上面一样。


但请注意,如果一个轴上有多个条形图,则图会移动。因此,如果你添加xticklabel style={rotate=90}到轴选项,你将获得

在此处输入图片描述

如果你不想这样转变,你可以添加

bar shift=0pt,

之后ybar减少enlargelimits到例如0.3

在此处输入图片描述

然后你可以添加

xtickmin=Doramus,
xtickmax=Tidanme,

要得到

在此处输入图片描述

代码:

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.14}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
    width  = 0.85*\textwidth,
    height = 8cm,
    major x tick style = {draw=none},
    ybar=2*\pgflinewidth,
    bar shift=0pt,
    bar width=14pt,
    ymajorgrids = true,
    ylabel = {Percentage},
    xlabel = {},
    symbolic x coords={Doramus,Refaltido,Misanit,Fanatha,Sonphay,Laoli,Tidanme},
    xtickmin=Doramus,
    xtickmax=Tidanme,
    scaled y ticks = false,
    xticklabel style={rotate=90},
    enlarge x limits=.3,
    ymin=0,
    ymax=100,
    legend cell align=left,
    legend style={
            at={(.872,.5)},
            anchor=south,
            column sep=1ex
    }  
]
\addplot[style={green,fill=green}]
    coordinates {(Doramus,60)};
\addplot[style={orange,fill=orange}]
    coordinates {(Refaltido,17)};
\addplot[style={yellow,fill=yellow}]
    coordinates {(Misanit,6)};
\addplot[style={blue,fill=blue}]
    coordinates {(Fanatha,6)};
\addplot[style={purple,fill=purple}]
    coordinates {(Sonphay,5)};
\addplot[style={black,fill=black}]
    coordinates {(Laoli,4)};
\addplot[style={black,fill=white}]
    coordinates {(Tidanme,2)};
\legend{Doramus,Refaltido,Misanit,Fanatha,Sonphay,Laoli,Tidanme}
\end{axis}
\end{tikzpicture}
\end{document}

要删除 xticklabels,请major x tick style = {draw=none},用替换xtick=\empty,。然后xtickminxtickmax就可以被删除了。

答案2

这是 x 轴上没有标签的版本。对于您的问题:

  1. 重复的标签来自该enlarge x axis部分。如果将其移除,则不会有重复。
  2. 可以使用 删除 x 标签xtick=\empty

图形

\documentclass{article}
\usepackage{tikz}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
    width  = 0.85*\textwidth,
    height = 8cm,
    major x tick style = transparent,
    ybar=2*\pgflinewidth,
    bar width=14pt,
    ymajorgrids = true,
    ylabel = {Percentage},
    xlabel = {},
    symbolic x coords={Doramus,Refaltido,Misanit,Fanatha,Sonphay,Laoli,Tidanme},
    enlarge x limits=2,
    xticklabels=\empty,
    xtick=\empty,
    scaled y ticks = false,
    ymin=0,
    ymax=100,
    legend cell align=left,
    legend style={
            at={(.872,.5)},
            anchor=south,
            column sep=1ex
    }  
]

\addplot[style={green,fill=green}]
    coordinates {(Doramus,60)};
\addplot[style={orange,fill=orange}]
    coordinates {(Refaltido,17)};
\addplot[style={yellow,fill=yellow}]
    coordinates {(Misanit,6)};
\addplot[style={blue,fill=blue}]
    coordinates {(Fanatha,6)};
\addplot[style={purple,fill=purple}]
    coordinates {(Sonphay,5)};
\addplot[style={black,fill=black}]
    coordinates {(Laoli,4)};
\addplot[style={black,fill=white}]
    coordinates {(Tidanme,2)};
\legend{Doramus,Refaltido,Misanit,Fanatha,Sonphay,Laoli,Tidanme}
\end{axis}
\end{tikzpicture}
\end{document}

相关内容