3

I'm looking to plot a simple plot with data I've taken from MATLAB in a .dat file format. The file has two columns in the order shown below. I would like column 1 to be x and column 2 to be y.

I've been trying to use tikzpicture for ages but to no avail. Any help is much appreciated thank you.

I dont know if this is an issue but the columns are huge and have lots of entries. Is that an issue in latex?

1.3888889e-03   2.0000478e+01
2.7777778e-03   2.0001440e+01
4.1666667e-03   2.0002877e+01
5.5555556e-03   2.0004785e+01
6.9444444e-03   2.0007160e+01
8.3333333e-03   2.0009999e+01
9.7222222e-03   2.0013296e+01

MATLAB Plot

The picture is what I am attempting to recreate. Here's the code I've simply got to plot the marker points, can anyone point me in the direction of creating a line opposed to all the points. Also adding in more y axis points i.e. every 100oC and grid lines if possible. Thanks

\begin{figure}[H]
\begin{tikzpicture}
\begin{axis}
[legend style = { at = {(0.6,0.75)}},only marks,
xlabel = Time\,/\,hr,xmax = 8,xmin = 0,
ylabel = Temperature\,/\,$^{0}$C,ymax = 600,ymin = 0]
\addplot [thin] table {data.dat};
\end{axis}
\end{tikzpicture}
\caption{Temperature Change in FB}
\end{figure}
3
  • 3
    Please, share the code of what you have tried already.
    – m0nhawk
    Apr 22, 2014 at 17:34
  • also remove the empty lines between entries. is that your data file structure?
    – percusse
    Apr 22, 2014 at 18:01
  • 1
    I would recommend 2 approaches. The best one for me is using Gnuplot (can take your data exported from matlab) and export in latex. The other could be using this: mathworks.com/matlabcentral/fileexchange/22022-matlab2tikz
    – Nico
    Apr 22, 2014 at 18:31

1 Answer 1

3

Whether the number of data points is a problem or not depends on what exactly you mean by 'huge'. But if you've successfully plotted with points already, then it obviously isn't a problem. With regard to the rest:

The only marks option will give you just the points. If you remove that, you will get a line. The default is actually to plot both points and line, but the thin option that you've added to the \addplot overrides this, giving you just the line. You can also say this explicitly with the no marks option.

You can specify the values for y-ticks with ytick={<numbers>}. To get regularly spaced ticks, you use the triple dot notation, i.e. ytick={0,100,...,600}. Similar for xtick.

To get grid lines for the specified ticks, just add grid to the axis options.

In the code below I also added an example of use of the units library for adding units to axis. I also added an additional data value just to get a visible line for the axis limits you had given.

enter image description here

\documentclass[tikz,border=2mm]{standalone}
\usepackage{pgfplots}
\usepgfplotslibrary{units}

\usepackage{filecontents}
% the filecontents environment writes its content to the specified file
\begin{filecontents*}{data.dat}
1.3888889e-03   2.0000478e+01
2.7777778e-03   2.0001440e+01
4.1666667e-03   2.0002877e+01
5.5555556e-03   2.0004785e+01
6.9444444e-03   2.0007160e+01
8.3333333e-03   2.0009999e+01
9.7222222e-03   2.0013296e+01
8               550
\end{filecontents*}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
  legend style = { at = {(0.6,0.75)}},
  unit markings=slash space,
  x unit={hr},
  y unit={^\circ C},
  xlabel=Time,
  ylabel=Temperature,
  xmin=0,xmax=8,
  ymin=0,ymax=600,
  ytick={0,100,...,600},
  xtick={0,1,...,8},
  grid,
  grid style={dotted}]

\addplot [thin] table {data.dat};

\end{axis}
\end{tikzpicture}
\end{document}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .