pgfplots:修复重叠的 X 轴标签

pgfplots:修复重叠的 X 轴标签

我正在尝试创建时间序列图,但我的X轴标签重叠:

\documentclass{article}
\usepackage{pgfplots}   
\pgfplotsset{width=14cm,compat=newest}   
\usepgfplotslibrary{dateplot}     

\begin{filecontents*}{data.csv}
count  date
38  2015-01-28
5  2015-02-11
21  2015-02-15
1  2015-02-16
21  2015-02-17
21  2015-02-18
62  2015-02-19
16  2015-02-25
4  2015-02-26
1  2015-02-30
1  2015-03-07
44  2015-04-07
\end{filecontents*}

\begin{document}
\begin{tikzpicture}
\pgfplotstableread{data.csv}\table
\begin{axis}[
    date coordinates in = x,
    date ZERO = 2015-01-28,
    ymin = 0,
    x tick label style = {font = \small, text width = 1.7cm, align = center, rotate = 70, anchor = north east},
    xtick = data,
    xticklabels from table = \table{date}
    ]
\addplot [only marks, blue] table [x = date, y = count] \table;%
\addplot [green] table [x = date, y = count] \table;%
\end{axis}
\end{tikzpicture}
\end{document} 

删除该xtick = data,线可以解决重叠问题,但蓝点和轴上的刻度X不再对齐。

如何使X轴标签保持恒定距离?我也尝试过,xticklabel style={inner sep=0pt, anchor=north east, rotate=70}但没有解决问题。

答案1

这似乎有效。我删除了dateplot内容,并使用了而x=date不是。这使用坐标的索引,即 0,1,2,...,这给出了刻度之间的恒定距离。将刻度放在正确的位置。您需要加载才能使用。\addplotx expr=\coordindexxtick=datapgfplotstablex expr=\coordindex

顺便说一句,compat=newest这不一定是件好事,看看\pgfplotsset{compat=newest} 的注意事项

在此处输入图片描述

\documentclass{article}
\usepackage{pgfplots,pgfplotstable}
\usepackage{filecontents}
\pgfplotsset{width=14cm,compat=1.12}     

\begin{filecontents*}{data.csv}
count  date
38  2015-01-28
5  2015-02-11
21  2015-02-15
1  2015-02-16
21  2015-02-17
21  2015-02-18
62  2015-02-19
16  2015-02-25
4  2015-02-26
1  2015-02-30
1  2015-03-07
44  2015-04-07
\end{filecontents*}

\begin{document}
\begin{tikzpicture}
\pgfplotstableread{data.csv}\data
\begin{axis}[
    ymin = 0,
    x tick label style = {font = \small, text width = 1.7cm, align = center, rotate = 70, anchor = north east},
    xtick=data,
    xticklabels from table = \data{date}
    ]
\addplot [green,mark=*, mark options={blue}] table [x expr=\coordindex, y = count] \data;
\end{axis}
\end{tikzpicture}
\end{document} 

相关内容