我正在尝试绘制某城市某一年的平均室外温度。因此,我希望数值(月份)位于 xtick 线之间,而不是线上。有人能帮我吗?
这是我的 Latex 代码,我添加了两张图片,分别是它当前的样子和我希望它看起来的样子。
\documentclass{article}
\usepackage{siunitx}
\usepackage{tikz} % To generate the plot from csv
\usepackage{pgfplots}
\pgfplotsset{compat=newest, every axis/.append style={line width=1.5pt}}
\usepgfplotslibrary{units} % Allows to enter the units nicely
\usepgfplotslibrary{dateplot}
\sisetup{round-mode = places, round-precision = 2,}
\begin{figure}[h!]
\begin{center}
\begin{tikzpicture}
\begin{axis}[
date coordinates in=x,date ZERO=2017-01-01,xticklabel=\month,
xmin=2017-01-01,xmax=2018-01-01,
xtick = {2017-01-01, 2017-02-01, 2017-03-01, 2017-04-01, 2017-05-01, 2017-06-01, 2017-07-01, 2017-08-01, 2017-09-01, 2017-10-01, 2017-11-01, 2017-12-01},
width= \linewidth, height = 8cm,
grid=major,% Display a grid
grid style={dashed,gray!30},
xlabel= Month of year,
ylabel= Temperature,
y unit=\si{^\circ C},
legend style={at={(1,1)},anchor=north east},
no markers,
]
\addplot table[x=Day,y=T_min, col sep=comma] {tables/Temperatures.csv};
\addlegendentry{\emph{$T_{min}$}}
\end{axis}
\end{tikzpicture}
\caption{Average Outside Temperature between 2007 - 2017}
\end{center}
\end{figure}
\end {document}
目前的情况如下:
我希望它看起来像这样:
答案1
一个简单的xshift
方法也可以。我还做了一些小的额外更改,并试图解决整个问题。问题是,据我所知,在需要时,刻度之间的(平均)距离是未知的。原则上,如果你们人类每个月的天数没有不同,那么在这种情况下答案会很简单。这就是标签没有完全居中的原因,也是为什么这在二月最为明显。我还添加了一些例程来检索最后两个刻度之间的距离,但在目前的情况下,它只告诉你十二月(或一月)有 31 天,你可能之前就知道了。而且,正如我提到的,这些信息来得太晚了。尽管如此,我还是决定把它留在这里,因为它可能对另一个问题有用。
\documentclass{article}
\usepackage{siunitx}
\usepackage{tikz} % To generate the plot from csv
\usepackage{pgfplots}
\makeatletter
\newcommand{\GetXDistance}[1]{\edef#1{\pgfplots@tick@distance@x}}
\makeatother
\pgfplotsset{compat=newest, every axis/.append style={line width=1.5pt}}
\usepgfplotslibrary{units} % Allows to enter the units nicely
\usepgfplotslibrary{dateplot}
\sisetup{round-mode = places, round-precision = 2,}
\begin{document}
\begin{figure}[h!]
\makeatletter
\begin{center}
\begin{tikzpicture}
\begin{axis}[
date coordinates in=x,date ZERO=2017-01-01,xticklabel=\month,
xmin=2017-01-01,xmax=2018-01-01,
xtick = {2017-01-01, 2017-02-01, 2017-03-01, 2017-04-01, 2017-05-01, 2017-06-01, 2017-07-01, 2017-08-01, 2017-09-01, 2017-10-01, 2017-11-01, 2017-12-01},
width=\linewidth, height = 8cm,
grid=major,% Display a grid
grid style={dashed,gray!30},
xticklabel style={xshift=15,anchor=north},
xlabel= Month of year,
ylabel= Temperature,
y unit=\si{\celsius}, % ^\circ and \si ?
legend style={at={(1,1)},anchor=north east},
no markers,
]
\addplot coordinates {(2017-01-01,1) (2017-02-01,2)
(2017-03-01,2) (2017-04-01,3) (2017-05-01,3)
(2017-06-01,4) (2017-07-01,4) (2017-08-01,4)
(2017-09-01,3) (2017-10-01,3) (2017-11-01,2)
(2017-12-01,1) (2018-01-01,1)} \pgfextra{\GetXDistance{\mydist}
\typeout{\mydist}}
;
% \addplot table[x=Day,y=T_min, col sep=comma] {tables/Temperatures.csv};
\addlegendentry{$T_{min}$} % \emph didn't have any effect here
\end{axis}
\end{tikzpicture}
\caption{Average Outside Temperature between 2007 - 2017}
\end{center}
\makeatother
\end{figure}
\end{document}
更新:只是为了好玩,利用标签是节点的事实,可以将 xtick 标签居中。
\documentclass{article}
\usepackage{siunitx}
\usepackage{tikz} % To generate the plot from csv
\usepackage{pgfplots}
\newcounter{tickindex}
\makeatletter
\newcommand{\GetXDistance}[1]{\edef#1{\pgfplots@tick@distance@x}}
\makeatother
\pgfplotsset{compat=newest, every axis/.append style={line width=1.5pt}}
\usepgfplotslibrary{units} % Allows to enter the units nicely
\usepgfplotslibrary{dateplot}
\sisetup{round-mode = places, round-precision = 2,}
\begin{document}
\begin{figure}[h!]
\makeatletter
\begin{center}
\begin{tikzpicture}
\begin{axis}[
date coordinates in=x,date ZERO=2017-01-01,xticklabel=\month,
xmin=2017-01-01,xmax=2018-01-01,
xtick = {2017-01-01, 2017-02-01, 2017-03-01, 2017-04-01, 2017-05-01, 2017-06-01, 2017-07-01, 2017-08-01, 2017-09-01, 2017-10-01, 2017-11-01, 2017-12-01,2018-01-01},
width=\linewidth, height = 8cm,
grid=major,% Display a grid
grid style={dashed,gray!30},
xticklabel style={opacity=0,anchor=north east,name=x-\tick},
xlabel= Month of year,
ylabel= Temperature,
y unit=\si{\celsius}, % ^\circ and \si ?
legend style={at={(1,1)},anchor=north east},
no markers,
]
\addplot coordinates {(2017-01-01,1) (2017-02-01,2)
(2017-03-01,2) (2017-04-01,3) (2017-05-01,3)
(2017-06-01,4) (2017-07-01,4) (2017-08-01,4)
(2017-09-01,3) (2017-10-01,3) (2017-11-01,2)
(2017-12-01,1) (2018-01-01,1)};
% \addplot table[x=Day,y=T_min, col sep=comma] {tables/Temperatures.csv};
\addlegendentry{$T_{min}$} % \emph didn't have any effect here
\end{axis}
\foreach \X [count=\Y] in {2017-01-01,2017-02-01,2017-03-01,2017-04-01,2017-05-01,2017-06-01,2017-07-01,2017-08-01,2017-09-01,2017-10-01,2017-11-01,2017-12-01,2018-01-01}
{\ifnum\Y=1
\else
\pgfmathtruncatemacro{\Month}{\Y-1}
\path (x-\LastTick)--(x-\X.east) node[midway]{\Month};
\fi
\xdef\LastTick{\X}
}
\end{tikzpicture}
\caption{Average Outside Temperature between 2007 - 2017}
\end{center}
\makeatother
\end{figure}
\end{document}
答案2
由于我没有 .csv 文件,因此我无法重现您的图表,但有一个想法是制作两个重叠的轴。您可以将一个轴设置为ybar interval
,这会将您的 x 轴标签放在刻度线之间的间隔中。在另一个轴上,您可以绘制数据但隐藏刻度和标签。
\documentclass{article}
\usepackage{siunitx}
\usepackage{tikz} % To generate the plot from csv
\usepackage{pgfplots}
\pgfplotsset{compat=1.15, every axis/.append style={line width=1.5pt},width= \linewidth, height = 8cm}
\usepgfplotslibrary{units} % Allows to enter the units nicely
\usepgfplotslibrary{dateplot}
\sisetup{round-mode = places, round-precision = 2,}
\begin{document}
\begin{figure}[h!]
\begin{center}
\begin{tikzpicture}
\begin{axis}[ybar interval,
date coordinates in=x,
date ZERO=2017-01-01,
xticklabel=\month,
xmin=2017-01-01,
xmax=2018-01-01,
xtick = {2017-01-01, 2017-02-01, 2017-03-01, 2017-04-01, 2017-05-01, 2017-06-01, 2017-07-01, 2017-08-01, 2017-09-01, 2017-10-01, 2017-11-01, 2017-12-01},
xlabel= Month of year,
ylabel= Temperature,
y unit=\si{^\circ C},
grid=major,% Display a grid
grid style={dashed,gray!30},
]
\end{axis}
\begin{axis}[
xticklabel=\empty,
legend style={at={(1,1)},anchor=north east},
no markers,
axis lines=none,
]
% \addplot table[x=Day,y=T_min, col sep=comma] {tables/Temperatures.csv};
\addlegendentry{\emph{$T_{min}$}}
\end{axis}
\end{tikzpicture}
\caption{Average Outside Temperature between 2007 - 2017}
\end{center}
\end{figure}
\end{document}
答案3
extra x ticks
另一种可能性是每月 15 日使用/添加并xticklabels
在那里显示。有关详细信息,请查看代码中的注释。
% used PGFPlots v1.16
\documentclass[border=5pt]{standalone}
\usepackage{siunitx}
\sisetup{
round-mode=places,
round-precision=2,
}
\usepackage{pgfplots}
\usepgfplotslibrary{
dateplot,
units,
}
\pgfplotsset{
compat=1.3,
every axis/.append style={
line width=1.5pt,
},
}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
width=\linewidth,
height=8cm,
date coordinates in=x,
date ZERO=2017-01-01,
xlabel=Month of year,
ylabel=Temperature,
y unit=\si{\celsius},
xmin=2017-01-01,
xmax=2018-01-01,
xtick={2017-01-01, 2017-02-01, 2017-03-01, 2017-04-01, 2017-05-01, 2017-06-01, 2017-07-01, 2017-08-01, 2017-09-01, 2017-10-01, 2017-11-01, 2017-12-01},
% don't show the labels here ...
xticklabels={},
% ... but at the 15th of every month ...
extra x ticks={2017-01-15, 2017-02-15, 2017-03-15, 2017-04-15, 2017-05-15, 2017-06-15, 2017-07-15, 2017-08-15, 2017-09-15, 2017-10-15, 2017-11-15, 2017-12-15},
% ... with the corresponding month number
extra x tick label=\month,
grid=major,
grid style={dashed,gray!30},
% but don't show the grid or the tick for the `extra ticks'
extra x tick style={
grid=none,
major tick length=0pt,
},
no markers,
]
% \addplot table[x=Day,y=T_min, col sep=comma] {tables/Temperatures.csv};
% (borrowed from marmot's answer)
\addplot coordinates {
(2017-01-01,1) (2017-02-01,2) (2017-03-01,2)
(2017-04-01,3) (2017-05-01,3) (2017-06-01,4)
(2017-07-01,4) (2017-08-01,4) (2017-09-01,3)
(2017-10-01,3) (2017-11-01,2) (2017-12-01,1)
(2018-01-01,1)
};
\legend{$T_{\min}$}
\end{axis}
\end{tikzpicture}
\end{document}