9

How another graphics can be inserted into Asymptote or pgfplots? The following is a MWE in pgfplots that creates three figures in the document. The main figure which I want to insert graphics in is a pgfplot graphics and the other two are a vector or a raster graphics.

\documentclass{article}
\usepackage{pgfplots}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{amsfonts}
\usepackage{graphicx}

\pgfplotsset{compat=newest}

\begin{document}
Figure 1 is the main figure that will be used for the insertion of a vector or a raster graphics.
\begin{figure}
    \begin{tikzpicture}
        \begin{axis}[
            colormap={bw}{gray(0cm)=(0); gray(1cm)=(1)}]
        \addplot+[scatter,only marks,
             domain=0:8,samples=100]
            {exp(x)};
        \end{axis}
    \end{tikzpicture}
    \caption{This is the main figure.}
\end{figure}

Figure 2 is a graphics which I want to be inside figure 1, which is a vector graphics. I want to place it in the upper left corner position.
\begin{figure}
    \begin{tikzpicture}
        \begin{axis}[colormap/bluered]
        \addplot+[scatter,
             scatter src=x,samples=50]
            {sin(deg(x))};
        \end{axis}
    \end{tikzpicture}
    \caption{This figure needs to be inside figure 1.}
\end{figure}

Figure 3 is another graphics which I want to be placed inside figure 1 in the upper left corner.
\begin{figure}
    \includegraphics[width=\linewidth]{piv.jpg}
    \caption{This figure needs to be inside figure 1}.
    \end{figure}

\end{document}

This is figure 1, which is a pgfplots graphics. This is figure 1

This is figure 2, which is a pgfplots graphics. This is figure 2

This is figure 3, which is a jpg image.

This is figure 3

This is combined figure 1 and figure 2. Figure 2 is placed inside figure 1 in the upper left corner. I would like this figure to be a vector graphics This is combined figure 1 and figure 2

This is combined figure 1 and figure 3. Figure 3 is placed inside figure 1 in the upper left corner. I would like figure 1 to be still a vector graphics. This is combined figure 1 and figure 3

I would be thankful if someone could help me how I can insert other vector graphics as well as raster graphics in Asymptote and pgfplots.

4
  • Both Yes but insert where in the axis? – percusse Mar 23 '14 at 4:27
  • @percusse: thanks for your comment. Both in and outside the axis. – AFPP Mar 23 '14 at 8:51
  • 1
    @Ahm: At the moment, your question is really to vague to be answered in a meaningful way. Could you please edit it to include more information on what exactly you're trying to achieve, ideally including a mockup of what you want the final result to look like? – Jake Mar 23 '14 at 10:58
  • @Jake: I made some modifications in the question for better clarification and illustrated a MWE. – AFPP Mar 23 '14 at 19:54
4

For the pgfplots within pgfplots you could combine the two plots in the code, as seen in How the plot in \groupplot could be moved horizontally and vertically?

For the image, add the following inside the axis environment of figure 1:

\node [above right] at (rel axis cs:0.2,0.4) {\includegraphics[width=2.5cm]{piv}};

The exact coordinates, which specify the location of lower left corner of the image, and the width should probably be modified. rel axis cs is a a coordinate system that has (0,0) in the lower left corner of the axis, and (1,1) in the upper right.

Note that if you have for example the pgfplots plot as a vectorised PDF, then including it this way will not rasterise it, so you can use this method for both.

enter image description here

\documentclass{article}
\usepackage{pgfplots}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{amsfonts}
\usepackage{graphicx}

\pgfplotsset{compat=newest}

\begin{document}
\begin{figure}
\centering
    \begin{tikzpicture}
        \begin{axis}[
            colormap={bw}{gray(0cm)=(0); gray(1cm)=(1)}]
        \addplot+[scatter,only marks,
             domain=0:8,samples=100]
            {exp(x)};

            \coordinate (otheraxis) at (rel axis cs:0.2,0.4);
        \end{axis}
        \begin{axis}[colormap/bluered,at={(otheraxis)},width=5cm]
        \addplot+[scatter,
             scatter src=x,samples=50]
            {sin(deg(x))};
        \end{axis}
    \end{tikzpicture}
    \caption{This is the main figure.}
\end{figure}

\begin{figure}
\centering
    \begin{tikzpicture}
        \begin{axis}[
            colormap={bw}{gray(0cm)=(0); gray(1cm)=(1)}]
        \addplot+[scatter,only marks,
             domain=0:8,samples=100]
            {exp(x)};

            \node [above right] at (rel axis cs:0.1,0.25) {\includegraphics[width=3cm]{piv}};
        \end{axis}
        \end{tikzpicture}

    \caption{This is the main figure.}
\end{figure}

\end{document}
7

As for Asymptote, you can use the label command to include external EPS graphics, as explained in the manual (version 2.24, p. 19):

The function string graphic(string name, string options="") returns a string that can be used to include an encapsulated PostScript (EPS) file. Here, name is the name of the file to include and options is a string containing a comma-separated list of optional bounding box (bb=llx lly urx ury), width (width=value), height (height=value), rotation (angle=value), scaling (scale=factor), clipping (clip=bool), and draft mode (draft=bool) parameters. The layer() function can be used to force future objects to be drawn on top of the included image:

label(graphic("file.eps","width=1cm"),(0,0),NE);
layer();
7
  • Thanks. It seems it is not possible to put a raster image into Asymptote. Would it be possible to put the image into the EPS file and then use the label function to insert it in Asymptote? – AFPP Mar 24 '14 at 17:23
  • 1
    What do you mean by "putting the image into the EPS file"? What I would do is simply to convert this image to the EPS format and then include it via Asymptote in your picture. – Franck Pastor Mar 25 '14 at 18:19
  • what would you use to convert the image to an EPS format? – AFPP Mar 26 '14 at 2:37
  • 2
    Converting to eps format is unnecessary if you use settings.tex = pdflatex;. – Charles Staats Mar 28 '14 at 23:39
  • 2
    @CharlesStaats, it is settings.tex = "pdflatex"; – alfC Oct 17 '14 at 22:00
3

Asymptote supports importing the external image directly, you can see its sample code in its GitHub source repo orthocenter.asy, the png file is located here piicon.png for a reference.

import geometry;
import math;

size(7cm,0);

if(!settings.xasy && settings.outformat != "svg") settings.tex="pdflatex";

real theta=degrees(asin(0.5/sqrt(7)));

pair B=(0,sqrt(7));
pair A=B+2sqrt(3)*dir(270-theta);
pair C=A+sqrt(21);
pair O=0;

pair Ap=extension(A,O,B,C);
pair Bp=extension(B,O,C,A);
pair Cp=extension(C,O,A,B);

perpendicular(Ap,NE,Ap--O,blue);
perpendicular(Bp,NE,Bp--C,blue);
perpendicular(Cp,NE,Cp--O,blue);

draw(A--B--C--cycle);

draw("1",A--O,-0.25*I*dir(A--O));
draw(O--Ap);
draw("$\sqrt{7}$",B--O,LeftSide);
draw(O--Bp);
draw("4",C--O);
draw(O--Cp);

dot("$O$",O,dir(B--Bp,Cp--C),red);
dot("$A$",A,dir(C--A,B--A),red);
dot("$B$",B,NW,red);
dot("$C$",C,dir(A--C,B--C),red);
dot("$A'$",Ap,dir(A--Ap),red);
dot("$B'$",Bp,dir(B--Bp),red);
dot("$C'$",Cp,dir(C--Cp),red);

label(graphic("piicon.png","width=2.5cm, bb=0 0 147 144"),Ap,5ENE);

Note the last statement of the code. Here is the result screen shot of the pdf. screen shot of the generated pdf

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.