1

I have a two dimensional Data set which is created by an outside program (matlab).

I want to plot this using pgfplots.

I am only interested in two data points on the x-axis. These should get ticks. The problem is: The position of these data points depends on the specification of my data generating matlab code and is thus endogenous. How can I automize the the picture drawing?

Here is my idea so far: When generating the data I also write down the two values in two separate files b_value.dat and c_value.dat each containing only that value.

The naive approach was to use the following properties

\begin{axis}[%
    ytick=\empty,
    xtick={\input{b_value.dat},\input{c_value.dat}},
    xticklabels={$b$,$c$}
]

I understand however from this question that \input does not work.

It also seems not to work inside \pgfplotsset.

Is there any way to automize this?

Nasty workaround: I write the entire \pgfplotsset-part in matlab and use input for that. I don't like that approach as to my understanding matlab should not write dirty latex. I would prefer to only write the values b and c as data points from the matlab rountine. Is this possible?

(I can include an MWE if demanded, but IMHO it is not necessary and only confusing by the amount of files)

johaschn
  • 333

1 Answers1

2

You already had the right idea to put the coordinates into a separate file, but instead of "\inputting" it in xtick directly, you should plot it as dummy first plot and use xtick=data. This option plots the x coordinates of the first given \addplot command and thus gives exactly the result you are searching for.

Please note that full coordinates have to be given and the corresponding y values influence the axis limits. So best it is to use the same y coordinate as in your "real" data (or set the axis limits explicitly).

For more details please have a look at the comments in the code.

    % put the two coordinates for which you want to show the `xtick's here
    % (Optimally provide the "full" coordinate, i.e. including the corresponding
    %  y value, so the axis limits aren't influenced by these dummy data like
    %  shown in this example)
    \begin{filecontents*}{xtick.txt}
        x   y
        0.5 0
        1.5 0
    \end{filecontents*}
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
    \begin{axis}[
        xtick=data,
    ]
        % use the first plot to draw the ticks only, so make it invisible
        % and "reset" the `cycle list index`
        \addplot [draw=none] table {xtick.txt};
            \pgfplotsset{cycle list shift=-1}

        % now you add your normal data
        \addplot coordinates {(1,1)};
    \end{axis}
\end{tikzpicture}
\end{document}

image showing the result of above code

Stefan Pinnow
  • 29,535