有没有简单的方法可以替换 axis 环境中的刻度?我想替换由 定义的刻度data
(即xtick = data
)。
下面是一个例子添加多了一个勾。因为我在添加,所以我没有替换,我想知道如何替换。
\documentclass[11pt, a4paper]{article}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\begin{filecontents}{data.csv}
date,pct
44755,0.01925374
44756,0.01925374
44757,0.01925374
44768,0.018989264
44769,0.018989264
\end{filecontents}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
ymin = -0.07, ymax =0.11,
%
xtick = data,
xticklabel style={ rotate=45},
extra x ticks={44757, 44765},
extra x tick labels={special1, special2},
extra x tick style={tick label style={red}},
scaled ticks=false,
]
\addplot[
blue, const plot,
] table [x=date, y=pct, col sep = comma]{data.csv};
\end{axis}
\end{tikzpicture}
\end{document}
谢谢
答案1
以下代码可让您根据以下情况指定要替换的刻度:X-value 为\replaceTheseTicks
。在此示例中,带有X-value 44757 和 44768 分别被替换为“Alice”和“Bob”。但请注意:此代码仅适用于整数。
\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\begin{filecontents}{data.csv}
date, pct
44755,0.01925374
44756,0.01925374
44757,0.01925374
44768,0.018989264
44769,0.018989264
\end{filecontents}
% Comma-separated list of ticks you want to replace
\def\replaceTheseTicks{44757,44768}
\def\replaceTick{
\pgfmathtruncatemacro{\result}{\tick}
\def\replaceHere{}
\foreach\xValue in \replaceTheseTicks{
\ifnum\xValue=\result
\gdef\replaceHere{T}
\fi
}
\if T\replaceHere
{}
\else
\axisdefaultticklabel
\fi
}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
ymin = -0.07,
ymax = 0.11,
xtick = data,
xticklabel = {\replaceTick},
xticklabel style={rotate = 90},
extra x ticks=\replaceTheseTicks,
extra x tick labels={Alice, Bob},
extra x tick style={tick label style={red}},
scaled ticks=false,
]
\addplot[blue, const plot] table [x=date, y=pct, col sep = comma]{data.csv};
\end{axis}
\end{tikzpicture}
\end{document}