1

I have a given diagram of measured values:

diagram I wish to draw on above diagram. MWE:

\documentclass{scrartcl}
\usepackage{tikz}

\begin{document}

  \begin{tikzpicture}

    \node at (0, 0) {\resizebox{\linewidth}{!}{\includegraphics{diagram}}};
    \draw
      (0, 0) -- (2, 2);

  \end{tikzpicture}

\end{document}

The coordinates of the \node and the \draw command are each (0, 0), but in the output the drawn ((0, 0) -- (2, 2)) line begins somewhere in the area:

result What do I wrong and why it's happens? What is to do for equivalent coordinates on both plain, to make intuitive drawing on the diagram. All possible solutions are welcome, also a complete redraw of the diagram too.

Thank you for your help and effort in advance!

  • 3
    The node (in which your image is contained) is centered at (0,0), which lines up with the bottom-left end of the line. To have the origin in the bottom left, try \node[anchor=south west,inner sep=0], per the top answer to this question: tex.stackexchange.com/questions/9559/… – rbrignall Sep 20 '18 at 14:11
  • 2
    You can always install your local coordinate system with \begin{scope}[shift={(pic node.south west)},x=\linewidth/3500,y=...] but the problem here is that the labels are also part of the graphics so that the trick won't work without manual adjustment that gets rid of the space occupied by the labels. – marmot Sep 20 '18 at 14:39
  • 2
    BTW, \includegraphics[width=\linewidth]{...} would make \resizebox redundant. – John Kormylo Sep 20 '18 at 20:20
  • 1
    The tikzgraphicx package (eigenheimstrasse.de/~ben/tikzgraphicx) could be helpful to determine suitable coordinated for your additions – user36296 Sep 20 '18 at 20:43
  • 1
    @Su-47 Because this is the width of your picture. This is a first step towards achieving a local coordinate system in which the x and y coordinates coincide with those of your background picture. Please see the answers of this question for how to annotate picture with TikZ. – marmot Sep 20 '18 at 20:55
4

One simpleminded proposal is to draw the full thing with TikZ/pgfplots. I reproduced your plot using my crystal ball. The advantage is that this will install automatically the appropriate axis coordinate system for you, such that you can add whatever lines to your plot (as long as you are inside the axis).

\documentclass{scrartcl}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\begin{document}
\begin{tikzpicture}
\begin{axis}[font=\sffamily,
grid=major,
xmin=0,
xmax=3500,
ymin=0,
ymax=0.2,
grid=both,
major grid style={draw=gray!60},
minor grid style={densely dotted},
minor x tick num=4,
minor y tick num=1,
xtick={0,500,...,3500},
ytick={0,0.02,...,0.2},
yticklabel style={/pgf/number format/fixed},
width=\linewidth,
ylabel={Messgr\"osse $y$},
xlabel={Zeit $t$ [$s$]},
title={Sprungantwort der Strecke}
]
\addplot[domain=100:3500,samples=201,smooth] {0.2*(1-exp(-(x-100)/606))};
\draw[red] (0,0) -- (1000,0.04);
\end{axis}
\end{tikzpicture}
\end{document}

enter image description here

  • Hello @marmot! Thank you for your answer! I have no data, only the picture of this diagram. – Su-47 Sep 20 '18 at 20:40
  • @Su-47 No problem, I used my crystal ball to find out the function you are plotting. ;-) – marmot Sep 20 '18 at 23:37
  • 1
    @Su-47 You can use engauge-digitizer to digitize the graph. I have found it very useful. – mas Sep 21 '18 at 1:53
  • 1
    @Su-47 In addition, our friends from Mathematica have this post. I actually used neither of them but just made a quick guess. Correspondingly this is not a perfect reproduction. On the other hand, this is a rather simple plot, and I personally would prefer to have some vector graphics for that if I'd have to use it in my own papers. – marmot Sep 21 '18 at 3:31
  • Hello @marmot! You are funny guys (crystal ball)! – Su-47 Sep 21 '18 at 11:12
7

I often use TikZ to draw paths above another picture, say a photograph. For this I use

\node[anchor=south west,inner sep=0] (image) at (0,0) {\includegraphics{somepicture.jpg}};
\begin{scope}[x={(image.south east)},y={(image.north west)}]
% draw your things here
\end{scope}

It creates a node called 'image' and with the scope command, the image's lower left corner gets coordinates (0,0) while the upper right corner is (1,1). So you can use coordinates relative to the image to place your nodes etc.

If you need to find coordinates of certain points you want to use in your picture, you can use

\draw[help lines, very thin, step=0.02] (0,0) grid (1,1);
\draw[help lines,thin,xstep=.1,ystep=.1] (0,0) grid (1,1);
\foreach \x in {0,1,...,9} { \node [anchor=north] at (\x/10,0) {0.\x}; }
\foreach \y in {0,1,...,9} { \node [anchor=east] at (0,\y/10) {0.\y}; }

this will draw a grid with coordinate labels on top of your image, which allows you to figure out the proper coordinates. When you are done, just remove the grid.

Here is an example with the grid:

\begin{tikzpicture}
  \node[anchor=south west,inner sep=0] (image) at (0,0) {\includegraphics[width=5cm]{qOr3L.png}};
  \begin{scope}[x={(image.south east)},y={(image.north west)}]
  \draw[help lines, very thin, step=0.02] (0,0) grid (1,1);
  \draw[help lines,thin,xstep=.1,ystep=.1] (0,0) grid (1,1);
  \foreach \x in {0,1,...,9} { \node [anchor=north] at (\x/10,0) {0.\x}; }
  \foreach \y in {0,1,...,9} { \node [anchor=east] at (0,\y/10) {0.\y}; }
  \draw[<-] (0.45,0.4) -- (1.1,1.1) node[above] {marmot's crystal ball};
  \end{scope}
\end{tikzpicture}

Produces:

crystal ball

I use these commands to put labels on different things in photographs.

  • 2
    Nice answer (+1). If you add shift={(image.south west)} you can make it even more general, such that the image does not have to be placed with its south west anchor in (0,0). See also my answer here which turns it into a simple style. – Max Sep 21 '18 at 12:31
  • Thanks. The shift option makes indeed sense if one does not want to put the image at (0,0). On the other hand, if the image is used in a standalone document just for this purpose, I think it makes no difference between shift or positioning the image at (0,0). – T. Pluess Sep 21 '18 at 16:24
  • True, but if you, for example, wanted to include two pictures, it would make little sense to place both at (0,0). – Max Sep 21 '18 at 16:32
1

A manual hack, together with use of anchor is as follows:

\documentclass{scrartcl}
\usepackage{tikz}

\begin{document}

  \begin{tikzpicture}[xscale=0.03884,yscale=53.44]
    \node[anchor=south west,inner sep=0] at (-29.53,-0.0156) {\resizebox{\linewidth}{!}{\includegraphics{diagram}}};
    % A few red lines and dots to show it lining up:
    \draw[red] (0, 0.1) -- (20, 0.1);
    \draw[red] (0,0) -- (0,0.1);
    \node at (0,0) {\color{red}$\cdot$};
    \node at (10,0.2) {\color{red}$\cdot$};
    \node at (100,0.1) {\color{red}$\cdot$};
  \end{tikzpicture}

\end{document}

Note that I divided the x coordinates by 10 to avoid LaTeX getting unhappy with large dimensions, so (100,0.1) really corresponds to the point (1000,0.1) on the plot. Additionally, if you put this in a document with (e.g.) different page dimensions, you'd probably need to adjust xscale, yscale, and the coordinates of the node all over again...

  • Hello @rbrignall! Thank you for your answer! How did you calculated the scale parameters [xscale = .03884, yscale = 53.44] and the node coodinates (-29.53, -.0156)? – Su-47 Sep 21 '18 at 11:10
  • 1
    A lot of trial and error... using the red dots to gauge how close I was... – rbrignall Sep 21 '18 at 11:57

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.