How to draw a rectangular region with Plots.jl?

question
plotting

#1

For an example in PyPlot:


#2

You could either do a scatter with custom markers (https://juliaplots.github.io/examples/gr/#custom-markers) or more likely you want to use the :shape seriestype, where the x/y inputs are the vertices of the polygon you wish to draw. You can draw multiple polygons in the same series by separating them with NaN. It may not run out of the box, but this tutorial was how I created the JuliaPlots logo.


#3

From @tbreloff tutorial, the solution is:

# define a function that returns a Plots.Shape
rectangle(w, h, x, y) = Shape(x + [0,w,w,0], y + [0,0,h,h])

plot(0:5,0:5)
plot!(rectangle(3,2,0,0), opacity=.5)


#4

I tried figuring out how Shape() works in general, but I couldn’t really figure out the rules for myself. I also searched the docs, but so far I didn’t find anything, that explained in a very general way, how to pass arguments to Shape() in a way that makes it do what I want. I could of course reproduce the rectangle, but what if I want to plot a trapezoid or anything? I did have a look at the neat batman-example, but it’s so much and so complicated, I couldn’t find the general rule how to get the x and y argument, that has to be passed to Shape(). I also found the Shape(vertices) -syntax and thought I could plot something, if I give e.g. four coordinates, but i didn’t find the right way to do so.
Can anybody here explain how I would use Shape(x,y) in a general way? Which type of vector do I need to pass? Why does the rectangle(w,h,x,y) function above work like it does?
I’m kind of desperate right now, because I really did try to figure it out on my own… Any help is appreciated!
Thanks!


#5

Just after writing here, I finally found out, what was what in general. In case anybody aver wonders this as well, I’ll add a small explanation:

Shape([x-value-corner1, x-value-corner2, x-value-corner3, x-value-corner4, …], [y-value-corner1, y-value-corner2, y-value-corner3, y-value-corner4, …])

So, to get a trapezoid, the code would be something like this:

plot(Shape([25, 425, 425, 25], [-1, -5.24, 5.24, 1]))

ultimatley giving the four corners of the trapezoid the coordinates

  • x= 25, y= -1
  • x= 425, y= -5.24,
  • x=425, y= 5.24,
  • x=25, y=5.24

I hope,this helps anybody stumbling across the same problem :slight_smile: