5

I construct plots by coordinates from table. For some coordinates, the y value is not given which causes the related xtick label disappears. But I want these xtick labels for such coordinates being shown on my plots. How to do it? Here is my MWE:

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.6}
\usepackage{filecontents} 

\begin{document}
\begin{filecontents}{test.dat} 
time    id  a
59:58   1   1
59:59   2   4
00:00   3   nan
00:01   4   16
00:02   5   25
\end{filecontents}

\begin{tikzpicture}
\begin{axis}[
title={example},
xlabel=time,
ylabel={$data$},
xticklabel style={rotate=90},
xtick=data,
xticklabels from table={test.dat}{time} 
]
\addplot [blue,mark=*] table[x=id,y=a] {test.dat};
\end{axis}
\end{tikzpicture}

\end{document}

enter image description here

4
  • Since the x axis is time, you can try using pgfplots library dateplot and then directly plot time vs data.
    – nidhin
    Dec 15, 2018 at 9:08
  • Thank you @nidhin. Yet, the x-axis has to be "id" in my case.
    – lyl
    Dec 15, 2018 at 10:19
  • Is it okay if the 00:00 is not marked but other ticks appear at the correct place?
    – nidhin
    Dec 15, 2018 at 11:24
  • I'm afraid not. 00:00 is necessary in my plots.
    – lyl
    Dec 15, 2018 at 12:44

1 Answer 1

1

For this case you could use xtick={1,...,5} instead of xtick=data.

enter image description here

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.6}
\usepackage{filecontents} 

\begin{document}
\begin{filecontents}{test.dat} 
time    id  a
59:58   1   1
59:59   2   4
00:00   3   nan
00:01   4   16
00:02   5   25
\end{filecontents}

\begin{tikzpicture}
\begin{axis}[
title={example},
xlabel=time,
ylabel={$data$},
xticklabel style={rotate=90},
xtick={1,...,5},
xticklabels from table={test.dat}{time} 
]
\addplot [blue,mark=*] table[x=id,y=a] {test.dat};
\end{axis}
\end{tikzpicture}

\end{document}
4
  • Many thanks @Torbjφm. In my example, there are only 5 lines of data. In fact, I have to plot nearly ten thousands lines of data, and some of them have "nan" data. And I want a xtick label at an interval every 900 lines.So how to use "xtick=" to solve my problem? Thank you.
    – lyl
    Dec 16, 2018 at 4:18
  • @lyl Well, with 10000 points you definitely don't want xtick=data anyways. Perhapsxtick={1,901,...,10000}. Dec 16, 2018 at 8:21
  • @lyl Sorry, I forgot about the xticklabels from table. that makes it a bit more tricky, but I think I have another answer describing a possible method. Give me a couple of minutes, I'll see if I can find it. Dec 16, 2018 at 8:57
  • @lyl See tex.stackexchange.com/questions/424357/… Dec 16, 2018 at 8:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.