2

I am trying to draw my manual defined markers (several stripes) but i seem unable to crop the plot (to [-.5,.5] in both dimensions). I can't even understand the effects of pgfplots here.

My (n)MWE is

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\begin{filecontents}{pgfplots-plot-limits.dat}
X    Y    color
0    0    1
-0.5    0    0
\end{filecontents}

\begin{document}
    \begin{tikzpicture}
        \pgfdeclareplotmark{thiscube}{\draw[ultra thin, fill] (0.23438,-0.5) -- (0.26562,-0.5) -- (-0.23438,0.5) -- (-0.26562,0.5);}
        \begin{axis}[colormap/hot,
            xmin=-0.5, xmax=0.5, ymin=-0.5, ymax=0.5,
            axis equal,clip=true,xtick=\empty, ytick=\empty,hide axis
        ]
        \pgfplotsextra{\clip (axis cs:-0.5,-0.5) rectangle (axis cs:0.5,0.5);}
        \addplot+[scatter,scatter src=explicit,mark=thiscube,only marks]
            table [x=X,y=Y,meta=color] {pgfplots-plot-limits.dat};
        \draw[thick,blue] (axis cs:-0.5,-0.5) rectangle (axis cs:0.5,0.5);
        \end{axis}
    \end{tikzpicture}
\end{document}

An I start off by removing the second data item -0.5 0 1 just for simplicity. Then the marker (a small parallelotope spanning the ymin-ymax range) should be drawn at 0 0 in however the result is (the last line in the axis is just a little helper indicating the original x and y range): enter image description here

However, if I remove (comment out) the line xmin=-0.5, xmax=0.5, ymin=-0.5, ymax=0.5, the result looks fine (however the physical dimensions seem to change drastically):

enter image description here

However, i am unable to crop these. Adding the second data item (same parallelotope shifted to these; together with the axis and the second data item I obtain

enter image description here

As before i am wondering, about the following (it just gets clearer here):

  1. Why does my pgfplotsextra not clip as expected?
  2. Why are my markers shifted? The red one (which is in the this picture the 0,0 data item is not centered at (0,0) (what I would expect of the marker) but at (.5,.5)
  3. If the positioning would work right, clip marker paths=true should work - like mentioned here; how can I position them correctly?

And yes, after clipping there should only be a very small portion left of the second data item, that's expected.

Ronny
  • 6,110
  • The origin for the axis cs: is not the same as the origin for the plot (which is at the lower left corner of the axis). By defining your mark using axis coordinate you automatically add the distance from the lower left corner, which is why they are showing up at the wrong location. – John Kormylo Mar 14 '16 at 16:53
  • From examples in the manual, one can see that the marks are exempt from normal clipping. One might be able to put clipping into the mark definition itself, but the mark doesn't really know where it is relative to the axis. – John Kormylo Mar 14 '16 at 17:13
  • I wasn't aware of the marker-definition, i.e. that it has to be wrt axis cs, however - clipping them is explained in my question 3 :) where it is relative to the axis - i don't mind; i just wanted that marker to be shifted to the positions (i.e. the origin of the marker draw). – Ronny Mar 14 '16 at 17:32
  • (axis cs: ) is the default in pgfplots. Actually, one is better off using absolute coordinates, but I assumed you wanted marks which stretched from top to bottom. – John Kormylo Mar 14 '16 at 17:53
  • Yes and (up to clipping, which I can't get to work) your solution does exactely what I want. I just don't understand why ;) – Ronny Mar 14 '16 at 17:55

1 Answers1

2

One can apply clipping OUTSIDE the axis environment. This means using width, height, and scale only axis=true to match the size of the plot to the size of the clip rectangle.

(rel axis cs: ...) has the origin at the lower left corner and (1,1) at the upper right corner, which is perfect for the marker definition.

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\begin{filecontents}{pgfplots-plot-limits.dat}
X    Y    color
0    0    1
-0.5    0    0
\end{filecontents}

\def\axisdim{2cm}% adjustable size

\begin{document}
    \begin{tikzpicture}
      \clip (0,0) rectangle (\axisdim,\axisdim);
      \pgfdeclareplotmark{thiscube}{\draw[ultra thin, fill] (rel axis cs: 0.23438,-0.5) -- (rel axis cs: 0.26562,-0.5) --
        (rel axis cs:-0.23438,0.5) -- (rel axis cs: -0.26562,0.5);}
      \begin{axis}[colormap/hot,width=\axisdim,height=\axisdim,scale only axis=true,
            xmin=-0.5, xmax=0.5, ymin=-0.5, ymax=0.5,
            axis equal,xtick=\empty, ytick=\empty,hide axis
        ]
        \addplot+[scatter,scatter src=explicit,mark=thiscube,only marks]
            table [x=X,y=Y,meta=color] {pgfplots-plot-limits.dat};
        \pgfplotsextra{\draw[thick,blue] (axis cs:-0.5,-0.5) rectangle (axis cs:0.5,0.5);}
        \end{axis}
    \end{tikzpicture}
\end{document}

demo

John Kormylo
  • 79,712
  • 3
  • 50
  • 120
  • Can you elaborate on/extend your answer, what these commands are doing, and why they are necessary? (Just tried using axis cs: in my markers above, which didn't work)... – Ronny Mar 14 '16 at 17:33
  • They can all be found in the tikz manual. These are the graphics primitives used by tikz to actually draw things. Of particular note is \pgfpointdiff which computes the relative vector between two points. – John Kormylo Mar 14 '16 at 17:45
  • Still seems strange to me. Your approach makes the points use axis cs: and (up to clipping, which doesn't work now) shifts them right, while directly specifying them in axis cs: (and hence changing my MWE of course), does not. I don't understand the difference. – Ronny Mar 14 '16 at 17:50
  • I tried to get clipping to work with your approach - is there also a low level trick to do that? – Ronny Mar 15 '16 at 05:29
  • No, but there is another trick that will work (I changed my answer). – John Kormylo Mar 15 '16 at 16:33
  • Thanks for the extension, I will try it tomorrow - though having the axis double dos not look so nice in code; my size will always be -.5 to .5 so it would at least be okay for the result :) – Ronny Mar 15 '16 at 19:14
  • Discovered the [scale only axis=true] option. Also, this marker should work for any xmin, xmax, ymin and ymax. – John Kormylo Mar 16 '16 at 02:22
  • oh and the rel axis cs: looks nice. Just checking your answer on my larger examples.... – Ronny Mar 16 '16 at 07:27
  • I am having trouble with my larger examples, but I will do a follow up question on that, because the problem described here is solved. Thank you very much for your patience and ideas. – Ronny Mar 16 '16 at 08:04
  • Here's the followup http://tex.stackexchange.com/questions/299273/draw-pgfplot-markers-whose-origin-is-outside-the-xmin-xmax-ymin-ymax-range – Ronny Mar 16 '16 at 09:03