3

I would like to plot a square from its vertices. For example:

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.10}
\usepgfplotslibrary{fillbetween}
\begin{document}
    \begin{center}
        \begin{tikzpicture}[scale=1]
        \begin{axis}[xmin=0,xmax=1,ymin=0,ymax=1,enlargelimits=false,xlabel=$a_1$,ylabel=$a_2$] 
            \addplot [blue!80!black,fill=blue,fill opacity=0.4]
        coordinates
        {(0, 0) (0, 0.436694) (0.436694, 0) (0.436694, 0.436694)}
        |- (axis cs:0,0) -- cycle;
        \end{axis}
        \end{tikzpicture}
    \end{center}
\end{document}

but this code plots:

enter image description here

I would like to plot the square without having to remove the point (0.436694, 0).

Regards

user1993416
  • 1,046
  • Changing the order slightly seems to work: (0, 0) (0, 0.436694) (0.436694, 0.436694) (0.436694, 0) } – Peter Grill Jun 09 '18 at 09:59
  • @PeterGrill Thanks. TikZ does not have any option so I could give the points in any order? – user1993416 Jun 09 '18 at 10:36
  • Well, there is a discussion on how to draw the convex hull of a set of coordinates. But as far as I know there is no proposal for pgfplots. –  Jun 09 '18 at 13:30
  • I am not sure if you should remove it. Others might have the same problem and maybe someone has a nice solution to it. One possible solution would be to sort the coordinates appropriately before drawing the thing. But obviously the standard sort won't do it. –  Jun 09 '18 at 13:39
  • @user1993416: If you want to use --cycle then it is best to have the points in an order that resembles a cycle. – Peter Grill Jun 09 '18 at 22:06

1 Answers1

3

In this very example (or in examples of this type) you could of course cheat. All you need to do is to add ybar interval.

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\usepgfplotslibrary{fillbetween}
\begin{document}
    \begin{center}
        \begin{tikzpicture}[scale=1]
        \begin{axis}[xmin=0,xmax=1,ymin=0,ymax=1,enlargelimits=false,xlabel=$a_1$,ylabel=$a_2$] 
            \addplot [blue!80!black,fill=blue,fill opacity=0.4,ybar interval]
         coordinates
        {(0, 0) (0, 0.436694) (0.436694, 0) (0.436694, 0.436694)}
        |- (axis cs:0,0) -- cycle;
        \end{axis}
        \end{tikzpicture}
    \end{center}
\end{document}

enter image description here