Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

enter image description here

The above tikzpictureplot was created mainly adapting the code of Create xkcd style diagram in TeX . I located the dots and labels by trial and error. What are the better options? To create the entire plot should I use draw commands instead?

Because I noticed that xtick={},ytick={} did not remove the axes tick marks, I put two marks only in each axis, hiding them by points O, S and R. Is there another better method?

As you can see the blue curve y=f(x) is not totally smooth as it should be. I thought that might be due to inaccuracies when evaluating the square root of small numbers. Can one overcome this issue?

Here is the code I used:

\documentclass{standalone}
\usepackage{amsfonts}
\usepackage{amsmath}
\usepackage{pgfplots} 
\usepackage{tikz}

\begin{document}

\begin{tikzpicture}
\pgfplotsset{every axis/.append style={line width=0.9pt}}
\begin{axis}[
    axis x line=middle,  
    axis y line=middle,   
    ymax=2.2, ylabel=$y$, 
    xlabel=$x$,
    xtick={0,3},
    ytick={0,2},
    xticklabels={},
    yticklabels={}
    ]
  \addplot[domain=0:3, blue, thick]    {sqrt(x^3/27)-sqrt(3*x)+2}; 
  \addplot[domain=0:1.5, red, thick]  {-(x/4+7/8)*sqrt(2)+2}; 
  \addplot[domain=0:3.2, black, thin] {0};
\end{axis}

\node at (-0.25,-0.25) {$O$};
\node at (3.4,0.9) {$P(x,y)$};
\node at (-0.7,2) {$Q(0,ut)$};
\node at (-0.7,5.1) {$R(0,uT)$};
\node at (6.7,-0.3) {$S(L,0)$}; 
\node at (2.4,1.8) {$y=f(x)$};
\node at (4.8,0.4) {$s=vt$};

\fill(0,0) circle (2pt);
\fill[blue](0,5.15) circle (2pt);
\fill[blue](3.2,0.6) circle (2pt);
\fill(0,2) circle (2pt);
\fill[blue] (6.4,0) circle (2pt);
\end{tikzpicture}

\end{document}

EDIT: To add dots at the origin and Q I added to percusse's code

\addplot [only marks,mark=*] coordinates { (0,0) (0,0.76) };
\node[below left] at (axis cs:0,0) {$O$}; 

before \end{axis}. The last line should type the origin, as commented by percusse, but for some reason unknown to me it doesn't work. The initial code of mine \node at (-0.25,-0.25) {$O$}; after \end{axis} does type O.

share|improve this question
    
Sorry it's my mistake. It's being clipped. If you are not going to draw things that go out of the axis area, you can add clip=false, option to the axis. Or you can let pgfplots know that you are going to do some more stuff via \pgfplotsset{after end axis/.code={\node[below left] at (axis cs:0,0) {$O$};}} just before the \end{axis} –  percusse Nov 28 '12 at 22:37
    
Or as the last option after end axis/.code={\node[below left] at (axis cs:0,0) {$O$};} to the axis environment. –  percusse Nov 28 '12 at 22:40
    
@percusse Fixed with your suggestion. I used \pgfplotsset{after end axis/.code={\node[below left] at (axis cs:0,0) {$O$};}}. –  Américo Tavares Nov 28 '12 at 22:59
    
@percusse I would like to change the title to a more informative one. What do you suggest? –  Américo Tavares Dec 1 '12 at 14:56
    
Since there is more than one issue, I think it's quite suitable. –  percusse Dec 1 '12 at 16:14
show 1 more comment

1 Answer 1

up vote 5 down vote accepted

As you have suspected, pgfplots offers a tight integration to TikZ-like syntax. I've put a few different types of doing things so you can choose your own favorite.

First, the axis coordinate system is handier to place things and you can access to that cs by using (axis cs:xcoord,ycoord) as I did with P(x,y) node. Also just before placing an ending ; to the addplot command you can place nodes as if it is a TikZ path(it is actually but still in a nontrivial way). It's better to place the markers as an individual plot with supplied coordinates with only marks and then followed by the marker type etc. Lastly, I've removed every label and added custom tick labels via extra ticks extra tick labels keys.

\documentclass{standalone}
\usepackage{amsfonts}
\usepackage{amsmath}
\usepackage{pgfplots} 
\pgfplotsset{compat=1.7}

\begin{document}

\begin{tikzpicture}
\pgfplotsset{every axis/.append style={line width=0.9pt}}
\begin{axis}[
    axis x line=middle,
    axis y line=middle,
    ymax=2.2,xmax=3.2,
    tick style={draw=none},
    ylabel=$y$, xlabel=$x$,
    xticklabels=\empty,
    yticklabels=\empty,
    extra y ticks={0.76256,2},
    extra y tick labels={ ${Q(0,ut)}$ , ${R(0,uT)}$ },
    extra x ticks={3.0},
    extra x tick labels={ ${S(L,0)}$ },
    ]
  \addplot[domain=0:3, samples=100,blue, thick]  {sqrt(x^3/27)-sqrt(3*x)+2} 
        node[pos=0.3,above right] {$y=f(x)$} node[near end,above right] {$s=vt$}; 
  \addplot[domain=0:1.5, red, thick]  {-(x/4+7/8)*sqrt(2)+2}; 
  \addplot [only marks,mark=*,blue] coordinates { (0,2) (1.5,0.23) (3,0)};
  \node[above=2mm,blue] at (axis cs:1.5,0.23) {${P(x,y)}$};
\end{axis}

\end{tikzpicture}

\end{document}

Apologies for the 0.23, I've just guessed it visually.

EDIT: Also you can increase the sample number via samples=n key. See the relevant plot.

enter image description here

share|improve this answer
    
I forgot to write that , is the point where TikZ, pgfplots think that some option is finished and starts reading a new one. Hence you need to protect the comma in the tick labels with an additional brace pair. –  percusse Nov 28 '12 at 20:44
    
Many thanks! Your plot looks very nice! It has many advantages over mine: the coordinates are uniform, the adjustable sample number. Perhaps the reason why mine in not entirely smooth might be due to a lower number of samples by default. Your guess of 0.23 as the y-coordinate of P is completely right: The exact value is -(5/4)√2+2≈0.23223. –  Américo Tavares Nov 28 '12 at 21:02
    
@AméricoTavares Aha, it's my lucky day :) No problem at all. –  percusse Nov 28 '12 at 21:10
1  
@AméricoTavares Ouch. That's a compatibility mode that the author of pgfplots has implemented. It forces to use the new features instead of the older ones and 1.7 is the latest stable version. You can replace 1.7 by newest and it will try to be compatible with the latest version on your system. However I strongly recommend upgrading as recently many bugs are fixed so it's even better. –  percusse Nov 28 '12 at 21:15
1  
@AméricoTavares You can also use \node[below left] at (axis cs:0,0) {$O$}; before \end{axis}. –  percusse Nov 28 '12 at 22:06
show 3 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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