如何跳过一些点而不跳过 x 刻度标签?十二月在哪里?
\documentclass{article}
\usepackage{filecontents}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\begin{document}
\begin{filecontents}{quant.csv}
month;2017;2018
January;nan;474500
February;457370;476580
March;443320;454930
April;353570;377600
May;400070;410190
June;392270;423280
July;343680;359340
August;271590;409120
September;312700;311910
October;388600;463960
November;367510;222960
December;291360;nan
\end{filecontents}
\pgfplotstableread[col sep=semicolon, header = true]
{quant.csv}\wastetable
\begin{tikzpicture}
\begin{axis}[
width = \textwidth,
height = 0.5625\textwidth,
major x tick style = transparent,
smooth,
% stack plots=y,
% area style,
line width = 1 pt,
ymajorgrids = true,
yminorgrids = true,
ylabel = {kg},
axis x line=center,
axis y line=left,
xtick = data,
scaled y ticks = false,
xticklabels from table = {\wastetable}{month},
enlarge x limits=0.05,
unbounded coords=jump,
ylabel style={rotate=0, xshift=5mm},
x tick label style={font=\small,align=right, rotate = 45, anchor = east},%text width=3.0cm},
legend cell align=left,
legend style={%
at={(0.98,1.1)},
anchor=south east,
column sep=1ex,
font = \scriptsize}%%
]
\addplot[style={red, mark=*}] table[x expr = \coordindex, y index = 1] {\wastetable};
\addlegendentry{2017};
\addplot[style={blue, mark=*}] table[x expr = \coordindex, y index = 2] {\wastetable};
\addlegendentry{2018};
\end{axis}
\end{tikzpicture}
\end{document}
答案1
跳过的原因December
是第一个图中对应的 y 值为nan
。因此,pgfplots 的标准键似乎不能满足您的要求,可以类似地修复此问题这个帖子。这里的函数element
与上一个答案的版本略有不同,它们都不是完美的。这与我无法\pgfplotstablegetcolumn
详细了解什么有关。所以我最终得到了
\documentclass[tikz,border=3mm]{standalone}
\usepackage{filecontents}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\pgfplotsset{compat=1.17}
\pgfmathdeclarefunction{element}{3}{\begingroup
\pgfkeys{/pgf/fpu,/pgf/fpu/output format=fixed}%
\expandafter\pgfplotstablevertcat\expandafter\mytable\csname#3\endcsname%
\pgfmathtruncatemacro{\myindex}{max(0,#1)}%
\pgfmathtruncatemacro{\mycol}{#2}%
\pgfplotstablegetelem{\myindex}{[index]\mycol}\of\mytable%#1=row, #2=column
\edef\pgfmathresult{\pgfplotsretval}%
\pgfmathsmuggle\pgfmathresult
\endgroup}
\begin{filecontents}{quant.csv}
month;2017;2018
January;nan;474500
February;457370;476580
March;443320;454930
April;353570;377600
May;400070;410190
June;392270;423280
July;343680;359340
August;271590;409120
September;312700;311910
October;388600;463960
November;367510;222960
December;291360;nan
\end{filecontents}
\begin{document}
\begin{tikzpicture}
\pgfplotstableread[col sep=semicolon, header = true]
{quant.csv}\wastetable
\pgfplotstablegetrowsof{\wastetable}%
\pgfmathtruncatemacro{\numrows}{\pgfplotsretval-1}%
\begin{axis}[
width = \textwidth,
height = 0.5625\textwidth,
major x tick style = transparent,
smooth,
line width = 1 pt,
ymajorgrids = true,
yminorgrids = true,
ylabel = {kg},
axis x line=center,
axis y line=left,
scaled y ticks = false,
xtick={0,...,\numrows},
xticklabel={\pgfmathparse{element(\ticknum,0,"wastetable")}\pgfmathresult},
enlarge x limits=0.05,
enlarge y limits=0.05,
unbounded coords=jump,
ylabel style={rotate=0, xshift=5mm},
x tick label style={font=\small,align=right, rotate = 45, anchor = east},%text width=3.0cm},
legend cell align=left,
legend style={%
at={(0.98,1.1)},
anchor=south east,
column sep=1ex,
font = \scriptsize}%%
]
\addplot[style={red, mark=*}] table[x expr = \coordindex, y index = 1] {\wastetable};
\addlegendentry{2017};
\addplot[style={blue, mark=*}] table[x expr = \coordindex, y index = 2] {\wastetable};
\addlegendentry{2018};
\end{axis}
\end{tikzpicture}
\end{document}