mkrphys / ipython-tikzmagic

IPython magics for generating figures with TikZ
BSD 3-Clause "New" or "Revised" License
160 stars 44 forks source link

draw coordinates wrong when using pgfplots #30

Closed parthi2929 closed 5 years ago

parthi2929 commented 5 years ago

Hi I am trying to draw a plot like below. This is working fine in actual tex file

2018-10-29_19h53_36

But when I try to use the same in ipython tikzmagic, this is what I get 2018-10-29_19h54_13

MWE for tex file:

\documentclass{article}
\usepackage{tikz}
\usepackage{pgfplots}
% \usetikzlibrary{math, decorations.pathreplacing,angles,quotes,bending}
\pgfplotsset{compat=1.15}
\pgfplotstableread{
X Y label
2.2 14 $(x_1,y_1)$
2.7 23 $(x_2,y_2)$
2.4 12 $(x_3,y_3)$
1.5 19 $(x_4,y_4)$
}\datatable

\begin{document}

\section{table using raw data}

% https://tex.stackexchange.com/questions/11251/trend-line-or-line-of-best-fit-in-pgfplots
\begin{tikzpicture}[scale=1.5]

    \begin{axis}[
        % legend pos=outer north east,
        xmin=1,xmax=4,ymin=5,ymax=30,
        ytick=\empty,xtick=\empty,
        xlabel={x},ylabel={y},
        clip=false, axis on top,grid = major,axis lines = middle,
        every node near coord/.append style={anchor=south west},
        ]
        \addplot [nodes near coords, only marks, blue, mark = *, point meta = explicit symbolic, font=\fontsize{5pt}{5pt}\selectfont] table[meta = label] {\datatable};
        \coordinate (XYA) at (2.2, 14);
        \coordinate (XYB) at (2.7, 23);
        \coordinate (XYC) at (2.4, 12);
        \coordinate (XYD) at (1.5, 19);
        \draw [ultra thin, draw=green, fill=green, opacity=0.2]  (XYA) rectangle (XYB);
        \draw [ultra thin, draw=red, fill=red, opacity=0.2]  (XYC) rectangle (XYD);
    \end{axis}

\end{tikzpicture}

\end{document}

MWE for tikzmagic which goes in a ipython cell:

%%tikz -p pgfplots
    \pgfplotstableread{
    X Y label
    2.2 14 $(x_1,y_1)$
    2.7 23 $(x_2,y_2)$
    2.4 12 $(x_3,y_3)$
    1.5 19 $(x_4,y_4)$
    }\datatable
\begin{axis}[
        xmin=1,xmax=4,ymin=5,ymax=30,   
        ytick=\empty,xtick=\empty,
        xlabel={x},ylabel={y},
        clip=false, axis on top,grid = major,axis lines = middle,
        every node near coord/.append style={anchor=south west}
        ]
        \addplot [nodes near coords, only marks, blue, mark = *, point meta = explicit symbolic] table[meta = label] {\datatable};
        \coordinate (XYA) at (2.2, 14);
        \coordinate (XYB) at (2.7, 23);
        \coordinate (XYC) at (2.4, 12);
        \coordinate (XYD) at (1.5, 19);
        \draw (0,0) rectangle (10,10);
        \draw [draw=green, fill=green]  (2.2,14) rectangle (2.7,23);
        \draw [ultra thin, draw=red, fill=red, opacity=0.5]  (XYC) rectangle (XYD);
\end{axis}

I initially had tried to push the datatable to preamble, but in that case, I get error as tikzmagic not able to reference that datatable so included in same cell now.

Kindly help with why the draw coordinates mess up? I even tried passing hard coded coordinate value but same issue as see in MWE.

parthi2929 commented 5 years ago

solved. this was a preamble compatability issue. Adding below line solved it.

preamble = '''
    \pgfplotsset{compat=1.15}
'''