强制所有轴标签显示在图中

强制所有轴标签显示在图中

下图中仅显示了一些 x 轴标签(例如 HH1、HH3、HH5 等)。但我想显示所有 x 轴标签(例如 HH1、HH2、HH3、HH4......)

我最初以为这是由于空间限制而发生的,并尝试更改字体大小和旋转标签。但这些方法都没有奏效。

\pgfplotsset{every tick label/.append style={font=\tiny}}
\begin{tikzpicture}
\begin{axis}[
    /pgf/number format/fixed,
    /pgf/number format/precision=2,
    title={Proportion of agents in households},
    xlabel={Household types},
    ylabel={Proportion of agents},
    symbolic x coords={HH1, HH2, HH3, HH4, HH5, HH6, HH7, HH8, HH9},
    ytick={0.0,0.05,0.1,0.15,0.2,0.25,0.3},
    legend pos=north east,
    ymajorgrids=true,
    grid style=dashed,
]

\addplot[
    color=blue,
    mark=square,
    ]
    coordinates {
    (HH1,0.1026)(HH2,0.2505)(HH3,0.2258)(HH4,0.1915)(HH5,0.1242) 
    (HH6,0.06644)(HH7,0.02514)(HH8,0.0095)(HH9,0.0040)
    };
    \legend{Merged population}

 \end{axis}
\end{tikzpicture}

结果图

答案1

添加xtick=data到轴选项。

在此处输入图片描述

\documentclass{article}
\usepackage{pgfplots}
\begin{document}
\pgfplotsset{every tick label/.append style={font=\tiny}}
\begin{tikzpicture}
\begin{axis}[
    /pgf/number format/fixed,
    /pgf/number format/precision=2,
    title={Proportion of agents in households},
    xlabel={Household types},
    ylabel={Proportion of agents},
    symbolic x coords={HH1, HH2, HH3, HH4, HH5, HH6, HH7, HH8, HH9},
    xtick=data,
    ytick={0.0,0.05,0.1,0.15,0.2,0.25,0.3},
    legend pos=north east,
    ymajorgrids=true,
    grid style=dashed,
]

\addplot[
    color=blue,
    mark=square,
    ]
    coordinates {
    (HH1,0.1026)(HH2,0.2505)(HH3,0.2258)(HH4,0.1915)(HH5,0.1242) 
    (HH6,0.06644)(HH7,0.02514)(HH8,0.0095)(HH9,0.0040)
    };
    \legend{Merged population}

 \end{axis}
\end{tikzpicture}
\end{document}

相关内容