如何增加/调整 x 轴的宽度以适合 pgfplots 中的所有点?

如何增加/调整 x 轴的宽度以适合 pgfplots 中的所有点?

这是我的代码:

\documentclass{article}
\usepackage{pgfplots}

\begin{document}


\begin{tikzpicture}
\begin{axis}[scale only axis,
    symbolic x coords={v1,v3,v4,v7,v8,v9,v10,v11,v15,v16,v17,v18,v20,v23,v25,v27,v28,v29,v30,v31},
    symbolic y coords={1,2,3,Not in top 3},    
    xtick=data,
    ytick=data]
    \addplot[color=blue] coordinates {
        (v1,3)
        (v3,1)
        (v4,1)
        (v7,1)
        (v8,Not in top 3)
        (v9,2)
        (v10,1)
        (v11,2)
        (v15,1)
        (v16,1)
        (v17,Not in top 3)
        (v18,1)
        (v20,Not in top 3)
        (v23,2)
        (v25,1)
        (v27,Not in top 3)
        (v28,1)
        (v29,1)
        (v30,Not in top 3)
        (v31,1)
    };
\end{axis}
\end{tikzpicture}

\end{document}

它看起来是这样的:

在此处输入图片描述

如何让剧情恰到好处地契合?

答案1

您认为添加以下内容如何:

enlarge x limits=-1, %hack to plot on the full x-axis scale
width=13cm, %set bigger width
height=6cm,
yticklabel style={anchor= east,rotate=45 }, %rotate y-ticks to have even more space

结果

编辑: 您还可以旋转 x 刻度以使其更易读。

完整代码:

\begin{tikzpicture}
\begin{axis}[scale only axis,
enlarge x limits=-1,
    symbolic x coords={v1,v3,v4,v7,v8,v9,v10,v11,v15,v16,v17,v18,v20,v23,v25,v27,v28,v29,v30,v31},
    symbolic y coords={1,2,3,Not in top 3},
width=13cm,
height=6cm,
yticklabel style={anchor= east,rotate=45 },
    xtick=data,
    ytick=data]
    \addplot[color=blue] coordinates {
        (v1,3)
        (v3,1)
        (v4,1)
        (v7,1)
        (v8,Not in top 3)
        (v9,2)
        (v10,1)
        (v11,2)
        (v15,1)
        (v16,1)
        (v17,Not in top 3)
        (v18,1)
        (v20,Not in top 3)
        (v23,2)
        (v25,1)
        (v27,Not in top 3)
        (v28,1)
        (v29,1)
        (v30,Not in top 3)
        (v31,1)
    };
\end{axis}
\end{tikzpicture}

相关内容