3

I'm trying to put together a pgfplot inside a pre-existing tikz picture. But somehow the axis I create with pfgplot has different units and I'm not sure how to make the two things fit.

The code is:

\documentclass[11pt]{amsart}
\usepackage{geometry}                % See geometry.pdf to learn the layout options. There are lots.
\geometry{letterpaper}                   % ... or a4paper or a5paper or ... 
%\geometry{landscape}                % Activate for for rotated page geometry
%\usepackage[parfill]{parskip}    % Activate to begin paragraphs with an empty line rather than an indent
\usepackage{graphicx}
\usepackage{amssymb}
\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{epstopdf}
\DeclareGraphicsRule{.tif}{png}{.png}{`convert #1 `dirname #1`/`basename #1 .tif`.png}

\title{Brief Article}
\author{The Author}
%\date{}                                           % Activate to display a given date or no date


\begin{document}
\maketitle

\begin{tikzpicture}[scale=1]
\draw [line width=0.05cm] (0,1) to [out=80,in=180] (1,1) to [out=0,in=180] (2.5,1.5) to [out=0,in=-100] (4,1);
\draw [line width=0.05cm] (0,3) to [out=25,in=150] (1.5,3.5) to [out=-30,in=120] (3,3) to [out=-50,in=-155] (4,3);
\draw [thin, ->] (-0.1,0) -- (4.5,0);
\draw [thin, ->] (0, -0.1) -- (0, 4.5);
\draw [thin] (4, -0.1) -- (4, 0.1);
\draw [line width=0.05cm, red] (1, 1) to [out=0, in=180] (1.5, 1.5);
\draw [line width=0.05cm, red] (2.5, 2.5) to [out=0, in=180] (3, 3);
\begin{axis}[samples=3000]
\addplot[very thick,red ]plot (\x, {\x*sin(1/abs(\x))});
\end{axis}
\end{tikzpicture}

\end{document}  

I would like somehow for the graph of the function x sin(1/|x|) (properly scaled and shifted) to connect the between the two pre-existing red lines smoothly.

Any help would be greatly appreciated.

** EDIT **

Here is some improved code:

\begin{tikzpicture}
\path [fill=lightgray] (0,1) to [out=80,in=180] (1,1) to [out=0,in=180] (2.5,1.5) to [out=0,in=-100] (4,1) to (4,0) to (0,0);
\draw [line width=0.05cm] (0,1) to [out=80,in=180] (1,1) to [out=0,in=180] (2.5,1.5) to [out=0,in=-100] (4,1);
\path [fill=lightgray] (0,3) to [out=25,in=150] (1.5,3.5) to [out=-30,in=120] (3,3) to [out=-50,in=-155] (4,3) to (4,4.3) to (0,4.3); \draw [line width=0.05cm] (0,3) to [out=25,in=150] (1.5,3.5) to [out=-30,in=120] (3,3) to [out=-50,in=-155] (4,3); \draw [thin, ->] (-0.1,0) -- (4.5,0);
\draw [thin, ->] (0, -0.1) -- (0, 4.5);
\draw [thin] (4, -0.1) -- (4, 0.1);
\node [below] at (4, -0.1) {$2\pi$};
\node [below] at (0, -0.1) {$0$};
\draw [dashed] (-0.1, 2) -- (4,2);
\draw [line width=0.05cm, red] (1, 1) to [out=0, in=180] (1.5,1.5); \draw [line width=0.05cm, red] (2.5,2.5) to [out=0, in=180] (3, 3); \begin{axis}[samples=9000,scale only axis,axis lines=none] \addplot[very thick,red ]plot (\x, {0.1*(\x + 3)*sin((1/abs((\x + 3)))r)}); \end{axis}
and the result:enter image description here

  • 1
    The usual request: Please help us to help you and add a minimal working example (MWE) that illustrates your problem. It will be much easier for us to reproduce your situation and find out what the issue is when we see compilable code, starting with \documentclass{...} and ending with \end{document}. – user31729 Apr 6 '15 at 20:33
  • @ChristianHupfer, hi, sorry, I hope I fixed it as you instructed. – PPR Apr 6 '15 at 20:38
  • Possible duplicate: Positioning pgfplots axis in tikzpicture – erik Apr 6 '15 at 21:09
1

If I understood your question correctly, the following should produce the curve you want, but doesn't address the question in the title about placing pgfplots axes within a tikzpicture.

\documentclass[11pt]{amsart}
\usepackage{tikz}
\usepackage{pgfplots}
\title{Brief Article}
\author{The Author}
\begin{document}
\maketitle

\begin{tikzpicture}[scale=1]
\draw [line width=0.05cm] (0,1) to [out=80,in=180] (1,1) to [out=0,in=180] (2.5,1.5) to [out=0,in=-100] (4,1);
\draw [line width=0.05cm] (0,3) to [out=25,in=150] (1.5,3.5) to [out=-30,in=120] (3,3) to [out=-50,in=-155] (4,3);
\draw [thin, ->] (-0.1,0) -- (4.5,0);
\draw [thin, ->] (0, -0.1) -- (0, 4.5);
\draw [thin] (4, -0.1) -- (4, 0.1);
\draw [line width=0.05cm, red] (1, 1) to [out=0, in=180] (1.5,1.5);
\draw[line width=0.05cm, red,rounded corners=2pt] (1.5,1.5) -| (2,2) |- (2.5,2.5);
\draw [line width=0.05cm, red] (2.5,2.5) to [out=0, in=180] (3, 3);
\end{tikzpicture}
\end{document}

enter image description here

Edit:

TikZ \draw commands can be within an axis environment, and the coordinates (x,y) will correspond to the current coordinate system. With this in mind, one approach is to essentially make a plot with two ordinates, then draw the shaded areas on one axis and the function on another. Ideally the x-axes should be the same and the y-axes can have different scales, but in the code below I've just manually lined things up rather than change all the coordinates of the shaded regions.

\documentclass[11pt]{amsart}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=1.12}

\title{Brief Article}
\author{The Author}
\begin{document}
\maketitle

\begin{tikzpicture}
\begin{axis}[axis lines=none,
xmin=-1,
xmax=5,
ymin=-1,
ymax=5]
\draw [fill=lightgray] (0,1) to [out=80,in=180] (1,1) to [out=0,in=180] (2.5,1.5) to [out=0,in=-100] (4,1) to (4,0) to (0,0);
\draw [line width=0.05cm] (0,1) to [out=80,in=180] (1,1) to [out=0,in=180] (2.5,1.5) to [out=0,in=-100] (4,1);
\path [fill=lightgray] (0,3) to [out=25,in=150] (1.5,3.5) to [out=-30,in=120] (3,3) to [out=-50,in=-155] (4,3) to (4,4.3) to (0,4.3);     \draw [line width=0.05cm] (0,3) to [out=25,in=150] (1.5,3.5) to [out=-30,in=120] (3,3) to [out=-50,in=-155] (4,3);                   \draw [thin, ->] (-0.1,0) -- (4.5,0);
\draw [thin, ->] (0, -0.1) -- (0, 4.5);
\draw [thin] (4, -0.1) -- (4, 0.1);
\node [below] at (4, -0.1) {$2\pi$};
\node [below] at (0, -0.1) {$0$};
\draw [dashed] (-0.1, 2) -- (4,2);
\draw [line width=0.05cm, red] (1, 1) to [out=0, in=180] (1.5,1.5);
\draw [line width=0.05cm, red] (2.5,2.5) to [out=0, in=180] (3, 3);
\end{axis}
\begin{axis}[axis lines=none,
xmin=-6,
xmax=6,
ymin=-5.1,
ymax=5.1
]
    \addplot[very thick,red,samples=1000,restrict x to domain=-1.1:1.1]
        plot (\x, {(\x)*sin((1/abs((\x)))r)});
\end{axis}
\end{tikzpicture}
\end{document}

enter image description here

This is not at all elegant, I'll admit, but hopefully the approach proves helpful.

  • Dear @erik, that is sort of what I wanted, but you drew a step function. I need the oscillations Here is what I mean: i.imgur.com/FBU0jp6.png and the Mathematica command: Plot[x Sin[1/Abs[x]], {x, -1, 1}, PlotRange -> {-1, 1}] – PPR Apr 6 '15 at 22:16
  • 1
    Ah, I see. The code you provided produced what looked basically like a step function (see Christian Hupfer's answer), so I just put one in manually. I'll see if I can come up with something else. – erik Apr 6 '15 at 23:37
  • 1
    I've hacked together something that might be helpful. Please see my edited answer. – erik Apr 7 '15 at 0:44

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.