2

I would like to draw the blend of two diagrams. Here is my code for the origin:

\documentclass{article}

\usepackage{pgfplots}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usepackage{tkz-euclide}
%\pgfplotsset{compat=newest} %<------ Here
\pgfplotsset{compat=1.11} %<------ Or use this one

\begin{document}

\begin{center}
\begin{tikzpicture}[scale=1.5][x=1cm,y=1cm]
\begin{axis}[xtick={-5,-4,-3,-2,-1,0,1,2,3,4,5}, ytick={-5,-4,-3,-2,-1,0,1,2,3,4,5},
xmin=-5, xmax=5, ymin=-5, ymax=5, grid=major,
xlabel={Galactic longitude $\ell$ in degree},
ylabel={Galactic latitude $b$ in degree},
axis lines = left,
axis line style={->}]

 % rectangle
 \draw[blue] (0,0) circle[radius=1];
 \draw[blue] (0,0) circle[radius=2];
 \draw[blue] (0,0) circle[radius=3];
 \draw[blue] (0,0) circle[radius=4];
 \draw[blue] (0,0) circle[radius=5];
\end{axis}
\end{tikzpicture}
\end{center}
\end{document}

I am trying to draw concentric circles but ended up with ovals. I would like to know how this can be fixed?

Here is the second issue, I would like to put a rectangular patch following the image:

Rectangular Patch.

I am struggling with these two: turning ovals into circles and placing a rectangular patch on it.

0

1 Answer 1

3

You can use the following:

\documentclass[tikz, border=2mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}

\begin{document}

\begin{tikzpicture}[scale=1.5]
  \begin{axis}[
    xmin=-5, xmax=5, ymin=-5, ymax=5, grid=major,
    xtick={-5,...,5}, ytick={-5,...,5},
    unit vector ratio = 1 1,
    xlabel={Galactic longitude $\ell$ in degree},
    ylabel={Galactic latitude $b$ in degree},
    axis lines = left,
    axis line style={->},
    clip=false]

   \pgfplotsinvokeforeach {1,...,5} {
     \draw[blue] (0,0) circle[radius=#1];
   }
   \draw[green, thick, fill=white] (-5,-0.5) rectangle (5,0.5);
  \end{axis}
\end{tikzpicture}

\end{document}

screenshot

Notes:

  • don't pass several optional arguments to tikzpicture (\begin{tikzpicture}[scale=1.5][x=1cm,y=1cm] is incorrect, better use something like \begin{tikzpicture}[scale=1.5, x=1cm,y=1cm]);

  • pgfplots compatibility level 1.11 is a bit old, I bumped it to 1.16. \pgfplotsset{compat=newest} may be fine when you are actively working on a plot and are going to fix all problems, but then better hardcode a version to ensure that your plot doesn't silently produce a different result in the future when you recompile it.
    In essence, saying \pgfplotsset{compat=1.16} means: “I have verified that this plot works fine with pgfplots compatibility level 1.16, I want to have the same behavior every time I recompile. Only when I'm ready to fix potential problems introduced by changes in pgfplots will I switch to a newer compatibility level (when more recent versions of pgfplots come out—1.16 is the latest at this point in time)”.

  • I used a \pgfplotsinvokeforeach loop to draw the circles;

  • I drew the rectangle using the implicit axis cs coordinate system (implicit since compatibility level 1.11);

  • I drew it after the circles to make sure its filling covers the circles and grid;

  • I used option unit vector ratio = 1 1 to make sure the units on each axis are represented with the same length (this is what makes the circles actual circles as opposed to ellipses).

  • clip=false ensures that the left and right sides of the green rectangle are not trimmed (half of the line width would be trimmed otherwise—try it).

Another coordinate system could be used to draw the rectangle patch, this is rel axis cs (see Coordinate system rel axis cs in the pgfplots manual). In this case, the coordinates wouldn't correspond to your data values; instead, the [0,1] range would be mapped to the ranges covered by each of the plot axes (x and y here, but this also works with 3D plots). In other words, the green rectangle could also be drawn this way:

\draw[green, thick, fill=white]
  (rel axis cs:0,0.45) rectangle (rel axis cs:1,0.55);
9
  • Thank You. This is exactly what I am looking for. Thank you very much.
    – Mass
    Commented Feb 3, 2020 at 9:08
  • 1
    Glad to hear that. :-)
    – frougon
    Commented Feb 3, 2020 at 9:09
  • @ frougon. How can I develop drawing skills in Latex? In particular using Tikz and pgfplot.
    – Mass
    Commented Feb 3, 2020 at 9:11
  • 1
    Read this forum, the manuals, and practice. If you do all these for some months and have enough LaTeX background to understand the answers, you should make good progress. I suggest back-and-forth between forum posts and manuals: you find something interesting that you don't understand well -> read corresponding manual part and try it for yourself.
    – frougon
    Commented Feb 3, 2020 at 9:13
  • 1
    @AMS I added another way to draw the rectangle, using the rel axis cs coordinate system of pgfplots. TikZ and pgfplots are complex beasts. Don't be impatient, take your time to learn little by little. Keep written notes (possibly on computer) when you find something interesting.
    – frougon
    Commented Feb 3, 2020 at 9:37

You must log in to answer this question.

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