1

I am trying to plot numerical values stored in a file called test1.dat

\begin{tikzpicture}
\begin{axis}[
title = ,
xlabel = {X},
ylabel= {Y},
scale only axis,
]
\addplot+ [blue] table {test1.dat};
\addlegendentry{Values}
\end{axis}
\end{tikzpicture}

I am trying to print to have a plot similar the image below in terms the used grid, I don't know how to make the legend outside of the plot and finally the exact values should printed on the plot like (7.073, etc.).

enter image description here

Thank you for your help.

Bests,

2
  • nodes near coords is designed to add text near plot points, and by default it adds the y-value above the points. I added that option to the axis, which turns on the feature for all the plots. For the individual plots I specified how those nodes should be positioned (in the example, 10pt above/below the point itself), by using e.g. nodes near coords align={above=10pt}.

  • ymajorgrids turns on the horizontal grid lines only. Add this to the axis options.

  • The position of the legend is set with legend style, see code below, which has some comments.

  • Perhaps you also want axis lines*=left.

(I don't of course have your data file, so I plotted a pair of linear functions.)

enter image description here

\documentclass[border=5mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
title = ,
xlabel = {X},
ylabel= {Y},
scale only axis,
ymajorgrids, %  turn on horizontal grid lines
nodes near coords, % <-- adds y-values next to points
legend columns=-1, % legend entries distributed horizontally instead of vertically
legend style={
  at={(0.5,1)}, % coordinates are relative to axis box, (0,0) in bottom left, (1,1) in top right
  above, % place it above that coord
  draw=none % remove frame of legend
  },
samples=10 % just for example
]
\addplot+ [nodes near coords align={above=10pt}]  {x};
\addplot+ [nodes near coords align={below=10pt}]  {x-1};
\addlegendentry{Values}
\addlegendentry{Stuff}
\end{axis}
\end{tikzpicture}
\end{document}
|improve this answer|||||

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.