0

I don't want to have the legend over the plot as shown in Figure. I want it inside the figure box but not over the plot. Also, the number format of y-axis, i want to have it 0.001, 0.002, etc.

enter image description here

The code of my figure is

\begin{figure}[!htb]
\begin{center}               
\begin{tikzpicture}          
\begin{axis}[xmin=0.09, ymin=0, xlabel={Froude number},ylabel={Resistance coefficient}, legend style={draw=none, legend columns=0}, width=10cm, height=7cm, legend pos=north west]

\addplot+ [mark=0,smooth] table [x=Fr, y=ct, col sep=comma] {model resistnace.csv};
\addlegendentry{$C_{T}$}

\addplot+ [mark=0,smooth] table [x=Fr, y=cf, col sep=comma] {model resistnace.csv};
\addlegendentry{$C_{F}$}

\addplot+ [mark=0,smooth] table [x=Fr, y=cr, col sep=comma] {model resistnace.csv};
\addlegendentry{$C_{R}$}


\end{axis}
\end{tikzpicture}
\caption{Resistance coefficients of ship model at full load default trim condition}
\label{resiscoef}
\end{center}
\end{figure}
2

One note: don't use the center environment inside the figure environment: it adds extra unneeded vertical space. Just one \centering placed somewhere inside the figure environment will do the job better.

Since I didn't have your data, I made some "dummy" functions that have roughly the same behavior. You can do the same for future questions: it makes it easier/faster for others to help you.

I see that you already discovered the legend pos key, which solves most of these problems. But with your plots, all the corners are not good options. Looking in the manual (Section 4.9.5), we can find that the legend pos key is really just a set of shortcuts for

legend style={at={(<x>,<y>)},anchor=<name>}

So we can use

legend style={at={(1,0.5)},anchor=east},

to place the legend, anchored at its east edge, at the right side of (x=1) and midway up (y=0.5) the axis. You can adjust those values and the anchor to place the legend anywhere in the axis viewport.

Now, about the numbering: check out the manual, Section 4.13.1, where you'll find lots of common settings for this. In this case, we need

scaled y ticks=false,
yticklabel style={/pgf/number format/fixed,/pgf/number format/precision=3},

which disables scaled ticks for the y-axis and sets fixed format with 3 digits displayed after the decimal separator.

Here's the complete code and output:

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.11}

\begin{document}
\begin{tikzpicture}          
\begin{axis}[
  xmin=0.09, 
  ymin=0, 
  xlabel={Froude number},
  ylabel={Resistance coefficient}, 
  legend style={draw=none, legend columns=0}, 
  width=10cm, 
  height=7cm, 
  legend pos=north west,
  %%%% BEGIN STYLE CHANGES/ADDITIONS %%%
  domain=0.092:0.208, % just for the example
  legend style={at={(1,0.5)},anchor=east},
  scaled y ticks=false,
  yticklabel style={/pgf/number format/fixed,/pgf/number format/precision=3},
]
  %%% DUMMY FUNCTIONS IN LIEU OF DATA %%%
  \addplot+ [mark=0,smooth] {5e-3-1e-3*x};
  \addlegendentry{$C_{T}$}
  \addplot+ [mark=0,smooth] {4.5e-3-3e-3*x};
  \addlegendentry{$C_{F}$}
  \addplot+ [mark=0,smooth] {7e-4+1e-3*x};
  \addlegendentry{$C_{R}$}
\end{axis}
\end{tikzpicture}
\end{document}

enter image description here

  • Thank you very much for your reply. Can it be like excel legend ? inside the box but not over the plot. Is it possible ? – Ameen Mohamed Bassam Jan 14 '15 at 17:09
  • 1
    @AmeenMohamedBassam I tried to explain how you could move the legend around the axis box (by changing the (1,0.5) and anchor=east parts of the code). Do you want something other than that? – Paul Gessler Jan 14 '15 at 17:13
  • Thank you @paul what about the domain. does it needs to be changed – Ameen Mohamed Bassam Jan 14 '15 at 17:21
  • I did it @paul Thank you for your help. I put ymax=0.006 then changed (1,0.5) to (1,0.9). – Ameen Mohamed Bassam Jan 14 '15 at 17:25
  • 1
    @AmeenMohamedBassam the domain part is only there for the example, to limit the data plotted. It's not needed when you're plotting your actual data files. – Paul Gessler Jan 14 '15 at 17:28

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.