# How to draw a rectangular region with Plots.jl?

**URL:** <https://discourse.julialang.org/t/how-to-draw-a-rectangular-region-with-plots-jl/1719>\
**Category:** Visualization\
**Tags:** question, plotting\
**Created:** [January 27, 2017, 4:37pm UTC](https://discourse.julialang.org/t/how-to-draw-a-rectangular-region-with-plots-jl/1719 "2017-01-27T16:37:13Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [January 27, 2017, 4:37pm UTC](https://discourse.julialang.org/t/how-to-draw-a-rectangular-region-with-plots-jl/1719/1 "2017-01-27T16:37:13Z")

</div>

For an example in PyPlot:

[http://matthiaseisen.com/pp/patterns/p0203/](http://matthiaseisen.com/pp/patterns/p0203/)

---

<div class="post-metadata">

**Author:** ![tbreloff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbreloff/32/68_2.png) [@tbreloff](https://discourse.julialang.org/u/tbreloff)\
**Post date:** [January 27, 2017, 7:02pm UTC](https://discourse.julialang.org/t/how-to-draw-a-rectangular-region-with-plots-jl/1719/2 "2017-01-27T19:02:29Z")

</div>

You could either do a scatter with custom markers ([https://juliaplots.github.io/examples/gr/#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](https://github.com/JuliaPlots/ExamplePlots.jl/blob/master/notebooks/batman.ipynb) was how I created the JuliaPlots logo.

---

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [January 27, 2017, 7:33pm UTC](https://discourse.julialang.org/t/how-to-draw-a-rectangular-region-with-plots-jl/1719/3 "2017-01-27T19:33:42Z")

</div>

From @tbreloff tutorial, the solution is:

```julia
# 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)

```

 ![](https://global.discourse-cdn.com/julialang/original/3X/7/0/709e59d3ac9770b4a35f731926339f664462ec1d.png)

---

<div class="post-metadata">

**Author:** ![LotteVictor](https://avatars.discourse-cdn.com/v4/letter/l/b19c9b/32.png) [@LotteVictor](https://discourse.julialang.org/u/LotteVictor)\
**Post date:** [November 21, 2017, 1:22pm UTC](https://discourse.julialang.org/t/how-to-draw-a-rectangular-region-with-plots-jl/1719/4 "2017-11-21T13:22:19Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![LotteVictor](https://avatars.discourse-cdn.com/v4/letter/l/b19c9b/32.png) [@LotteVictor](https://discourse.julialang.org/u/LotteVictor)\
**Post date:** [November 21, 2017, 1:56pm UTC](https://discourse.julialang.org/t/how-to-draw-a-rectangular-region-with-plots-jl/1719/5 "2017-11-21T13:56:32Z")

</div>

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 🙂

---

<div class="post-metadata">

**Author:** ![oliver](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oliver/32/19264_2.png) [@oliver](https://discourse.julialang.org/u/oliver)\
**Post date:** [November 1, 2018, 7:44pm UTC](https://discourse.julialang.org/t/how-to-draw-a-rectangular-region-with-plots-jl/1719/6 "2018-11-01T19:44:06Z")

</div>

> [@juliohm](#):
>
> plot!(rectangle(3,2,0,0), opacity=.5)

Currently gives me

```julia
ERROR: MethodError: no method matching +(::Int64, ::Array{Int64,1})
Closest candidates are:
  +(::Any, ::Any, ::Any, ::Any...) at operators.jl:502
  +(::T<:Union{Int128, Int16, Int32, Int64, Int8, UInt128, UInt16, UInt32, UInt64, UInt8}, ::T<:Union{Int128, Int16, Int32, Int64, Int8, UInt128, UInt16, UInt32, UInt64, UInt8}) where T<:Union{Int128, Int16, Int32, Int64, Int8, UInt128, UInt16, UInt32, UInt64, UInt8} at int.jl:53
  +(::Union{Int16, Int32, Int64, Int8}, ::BigInt) at gmp.jl:456
  ...
Stacktrace:
 [1] rectangle(::Int64, ::Int64, ::Int64, ::Int64) at ./REPL[17]:1
 [2] top-level scope at none:0

```

---

<div class="post-metadata">

**Author:** ![carstenbauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/carstenbauer/32/4981_2.png) [@carstenbauer](https://discourse.julialang.org/u/carstenbauer)\
**Post date:** [November 1, 2018, 8:31pm UTC](https://discourse.julialang.org/t/how-to-draw-a-rectangular-region-with-plots-jl/1719/7 "2018-11-01T20:31:28Z")

</div>

In Julia 1.0 you have to put dots in front of the plus signs in `rectangle` (be explicit about the fact that you add a scalar to every component of an array).

---

<div class="post-metadata">

**Author:** ![oliver](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oliver/32/19264_2.png) [@oliver](https://discourse.julialang.org/u/oliver)\
**Post date:** [November 1, 2018, 8:39pm UTC](https://discourse.julialang.org/t/how-to-draw-a-rectangular-region-with-plots-jl/1719/8 "2018-11-01T20:39:44Z")

</div>

Simple

```julia
# 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)

```

 ![Screenshot_20181101_173641](https://global.discourse-cdn.com/julialang/original/3X/b/7/b7215a27ae0251050e5a572f3b61e899171e8562.png)

Only difference is a setting placing the legend in top right.
