1

I'm using pgfplots to plot an implicit equation, but I'm running around using gnuplot by parametrizing the curve instead. The down side is that my parametrization necessarily comes in curve segments. There are certain places in the plot where the parametrized segments should meet up, but don't quite, and leave a little white space. Is there an option I could pass to an \addplot, or some other tikz tool, so that the last sampled point form the previous \addplot is used as a 0th sample point for the next \addplot?

Here is a MWE:

\documentclass{article}
\usepackage{pgfplots}

\begin{document}

\begin{tikzpicture}
\begin{axis}[variable=v,samples=100]
\addplot[domain=0:2.3499859792] ({v^2/(2+sqrt(4-v^2*cos(v^2*180/3.1415926)))},{2+sqrt(4-v^2*cos(v^2*180/3.1415926))});
%There is a white gap between these two curves that I'd like to automatically bridge
\addplot[domain=2.2795725971:2.3499859793,<-] ({v^2/(2-sqrt(4-v^2*cos(v^2*180/3.1415926)))},{2-sqrt(4-v^2*cos(v^2*180/3.1415926))});
\end{axis}
\end{tikzpicture}

\end{document}
5
  • You can put domain=2.3499859792:2.3499859793 in the second \addplot Commented May 6, 2015 at 6:10
  • @jpayansomet This is just an minimal working example. I am looking for a universal way to bridge these curve segments, without refining the domains further. There are too many segments in my actual project to investigate exploring all of the domains like this. Commented May 6, 2015 at 6:13
  • Then I don't know the answer. Commented May 6, 2015 at 6:16
  • Are you OK with using shorten key? Anyway, I am posting an answer. Please drop comments.
    – user11232
    Commented May 6, 2015 at 8:19
  • 1
    Use a piecewise function : tex.stackexchange.com/questions/132476/…
    – percusse
    Commented May 6, 2015 at 8:31

3 Answers 3

3

Here is an option with shorten key. The amount by which to shorten need to be carefully chosen so as to make the curve look continuous. Here you need not change the domains.

\documentclass{article}
\usepackage{pgfplots}

\begin{document}

\begin{tikzpicture}
\begin{axis}[variable=v,samples=100]
\addplot[samples=100,domain=0:2.3499859792,shorten >=-0.29ex] ({v^2/(2+sqrt(4-v^2*cos(v^2*180/3.1415926)))},{2+sqrt(4-v^2*cos(v^2*180/3.1415926))});
%There is a white gap between these two curves that I'd like to automatically bridge
\addplot[samples=100,domain=2.2795725971:2.3499859793,<-,shorten >=-0.29ex] ({v^2/(2-sqrt(4-v^2*cos(v^2*180/3.1415926)))},{2-sqrt(4-v^2*cos(v^2*180/3.1415926))});
\end{axis}
\end{tikzpicture}

\end{document}

enter image description here

1
  • Thanks for this answer. I decided not to go this route because I didn't want to take care about how much to (anti)-shorten with each such connection. Commented May 7, 2015 at 0:06
1

I ended up declaring functions for the paramterization, dialing back the domain a bit to help ensure I'm not bumping up against machine rounding error, and then just plotting the endpoints separately using the named functions, with a simplie connecting line.

\documentclass{article}
\usepackage{pgfplots}

\begin{document}

\begin{tikzpicture}[
  declare function={
    c(\x)= cos(\x^2*180/3.14159265359);
    sm(\x)=sqrt(4-\x^2*c(\x));    
    m(\x)= 2+sm(\x);
    n(\x)= 2-sm(\x);
    f(\x)= \x^2/m(\x);
    g(\x)= m(\x)/c(\x);
  }
]
\begin{axis}[variable=v,samples=100]
    \addplot[domain=0:2.3499,-,samples=200] ({f(v)},{m(v)});
    \addplot[mark=none] coordinates {({f(2.3499)},{m(2.3499)}) ({g(2.3499)},{n(2.3499)})};
    \addplot[domain=2.2795725971:2.3499,<-] ({g(v)},{n(v)});
\end{axis}
\end{tikzpicture}

\end{document}
0

An alternative to defining a piece-wise function is to define your function such that you can use it both for plotting and computing individual values, as given in the answer to this question: Consistently specify a Function and use it for computation and plotting

Here's a MWE, where I've defined 2 functions A and B, plotted them over separate domains, and then calculated coordinates to add 2 straight lines. Note that you can use the functions directly with braces (method 1) or set macros to store coordinate values (method 2).

\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}%

\def\myFuncA(#1){(#1)^3}%
\def\myFuncB(#1){20*exp(-1*#1)}%

\begin{document}
\begin{tikzpicture}
\begin{axis}
  \addplot[domain=0:2] {\myFuncA(x)};
  \addplot[-] coordinates { (2,{\myFuncA(2)}) (2,{\myFuncB(2)}) };% coordinates method 1
  \addplot[domain=2:4] {\myFuncB(x)};
  \pgfmathsetmacro{\Xa}{4};% coordinates method 2
  \pgfmathsetmacro{\Ya}{ \myFuncB(\Xa) };
  \pgfmathsetmacro{\Xb}{5};
  \pgfmathsetmacro{\Yb}{ 3*\Xb-10 };
  \addplot[-] coordinates { (\Xa,\Ya) (\Xb,\Yb) };
\end{axis}
\end{tikzpicture}
\end{document}

Plot showing that lines connect

You must log in to answer this question.

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