# How to plot circles with given radii using Plots

**URL:** https://discourse.julialang.org/t/how-to-plot-circles-with-given-radii-using-plots/47845
**Category:** General Usage
**Tags:** plots
**Created:** [October 6, 2020, 9:54am UTC](https://discourse.julialang.org/t/how-to-plot-circles-with-given-radii-using-plots/47845 "2020-10-06T09:54:29Z")
**Posts on this page:** 13
**Page:** 1

<div class="post-metadata">

### Author: ![SteffenPL](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/steffenpl/32/206270_2.png) [@SteffenPL](https://discourse.julialang.org/u/SteffenPL)
#### Post date: [October 6, 2020, 9:54am UTC](https://discourse.julialang.org/t/how-to-plot-circles-with-given-radii-using-plots/47845/1 "2020-10-06T09:54:29Z")

</div>

I want to create movies of biological cells which are represented as circles.  
My main problem is speed. Generating the videos I need takes more than 10 minutes.

Currently, I am using this approach to plot circles

> [@Plot a circle with a given radius with Plots.jl](https://discourse.julialang.org/t/plot-a-circle-with-a-given-radius-with-plots-jl/23295):
>
> Hi, I can’t figure out how to plot circles with given radii. I would like the radius to be given as floats with plots units (not integers). I try to play with markersize but the display sizes do not correspond to xy units. Any hints? Laurent

However, if I could use the `scatter` method, it would be much faster. But the size argument in `scatter` scales the size input. So, I don’t know how to set the size in `scatter` to obtain the correct physical radii in the plots.

PS: I’m inside a Pluto notebook, therefore Makie seems to be less appropriate.

---

<div class="post-metadata">

### Author: ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)
#### Post date: [October 6, 2020, 5:53pm UTC](https://discourse.julialang.org/t/how-to-plot-circles-with-given-radii-using-plots/47845/2 "2020-10-06T17:53:15Z")

</div>

Is `markersize` what you are trying? If so, what is the problem exactly? I think you can tune the relation of the size of the markers with the plot size to get what you want:

```julia
julia> plot()

julia> for s in [10, 20, 30]
         scatter!(rand(5),rand(5),markersize=s,label="$s")
       end

julia> plot!(size=(500,500))

```

Gives:

![plot](https://global.discourse-cdn.com/julialang/original/3X/8/2/82a88728b6742f90b9ec3c021b9a30d744d6d996.png)

Edit:

A very ugly workaround is the following. Plot one figure with the desired size and a symbol with `markersize=100`, and save it svg, for example:

```julia
scatter([0.5],[0.5],markersize=100,size=(500,500),xlims=(0,1),ylims=(0,1))
savefig("plot.svg")

```

![plot1](https://global.discourse-cdn.com/julialang/original/3X/4/5/4572d52c6000eb45ede70fc27c79354693d58cc8.png)

Open the `plot.svg` file in Inkscape, and click on the x-axis, above you will see its width (in my case, 452.106).

Click on the circle, and see its width (diameter), in my case 181.

Therefore, a scatter circle of diameter 181 has diameter 181/452.106 in x-axis units, and corresponds  
to `markersize=100`. Therefore, to plot a symbol with diameter 1.0 you need `markersize=100*(452.106/181)`. In other words, set

`markerunit = 100*(452.106/181)`

Finally, use these units to set the scatter size:

```julia

scatter([0.5],[0.5],markersize=markerunit,size=(500,500),xlims=(0,1),ylims=(0,1))
scatter!([0.25],[0.25],markersize=0.5*markerunit,size=(500,500),xlims=(0,1),ylims=(0,1))

```

![plot2](https://global.discourse-cdn.com/julialang/original/3X/5/f/5ffe01eb14d5473c7c99c430aa7c29ae144a2d02.png)

Of course this only works if you maintain the overall plot appearance (size, legends, titles, etc), constant from the measure of sizes to the final plotting.

---

<div class="post-metadata">

### Author: ![mthelm85](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mthelm85/32/224164_2.png) [@mthelm85](https://discourse.julialang.org/u/mthelm85)
#### Post date: [October 6, 2020, 7:01pm UTC](https://discourse.julialang.org/t/how-to-plot-circles-with-given-radii-using-plots/47845/3 "2020-10-06T19:01:28Z")

</div>

I’m not sure what the desired outcome is but, just in case this might be helpful, you can pass a vector to the `markersize` keyword argument:

```julia
using Plots

diameters = rand(5.0:0.2:15.0, 10)

scatter(rand(10),markersize=diameters,legend=false)

```

![radii](https://global.discourse-cdn.com/julialang/original/3X/4/c/4cadec1c68ab87760148cc2268882cf176eb4b23.png)

---

<div class="post-metadata">

### Author: ![briochemc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/briochemc/32/4209_2.png) [@briochemc](https://discourse.julialang.org/u/briochemc)
#### Post date: [October 6, 2020, 8:43pm UTC](https://discourse.julialang.org/t/how-to-plot-circles-with-given-radii-using-plots/47845/4 "2020-10-06T20:43:31Z")

</div>

> [@SteffenPL](#):
>
> I want to create movies of biological cells which are represented as circles.

Have you considered using [Javis.jl](https://github.com/Wikunia/Javis.jl)?

---

<div class="post-metadata">

### Author: ![SteffenPL](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/steffenpl/32/206270_2.png) [@SteffenPL](https://discourse.julialang.org/u/SteffenPL)
#### Post date: [October 7, 2020, 9:50am UTC](https://discourse.julialang.org/t/how-to-plot-circles-with-given-radii-using-plots/47845/5 "2020-10-07T09:50:20Z")

</div>

The desired outcome is that the circles have exactly the diameter as given. The `diameters` in your example as not the diameters of the circles, but scaled in the output. (In some way which depends on the backend and the size of the plot.) Therefore, `markersize` does not do the job for me as long as I cannot prevent the automatic scaling.

---

<div class="post-metadata">

### Author: ![SteffenPL](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/steffenpl/32/206270_2.png) [@SteffenPL](https://discourse.julialang.org/u/SteffenPL)
#### Post date: [October 7, 2020, 9:52am UTC](https://discourse.julialang.org/t/how-to-plot-circles-with-given-radii-using-plots/47845/6 "2020-10-07T09:52:41Z")

</div>

Thanks for the hint. I will give it a try! Does Javis.jl have the ability to draw an x-y-axis?

---

<div class="post-metadata">

### Author: ![briochemc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/briochemc/32/4209_2.png) [@briochemc](https://discourse.julialang.org/u/briochemc)
#### Post date: [October 7, 2020, 10:18am UTC](https://discourse.julialang.org/t/how-to-plot-circles-with-given-radii-using-plots/47845/7 "2020-10-07T10:18:53Z")

</div>

I don’t think there’s a built-in way, but manually I think it should be fairly easy: you could use `draw_line` and take inspiration from the [2nd tutorial](https://github.com/Wikunia/Javis.jl/blob/598da74c941d3c80dbe768ccf97f14d523a9880e/docs/src/tutorials/tutorial_2.md) (which has some fixed lines).

If you go this route I think you should probably just start an issue directly on the repo and ask for help there, as I’m sure the contributors will happily help and could add your example (or something similar) in a gallery or a tutorial! (I’m actually thinking I might do that soon myself for something similar).

---

<div class="post-metadata">

### Author: ![joa-quim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joa-quim/32/227_2.png) [@joa-quim](https://discourse.julialang.org/u/joa-quim)
#### Post date: [October 7, 2020, 11:02am UTC](https://discourse.julialang.org/t/how-to-plot-circles-with-given-radii-using-plots/47845/8 "2020-10-07T11:02:49Z")

</div>

GMT lets you provide the symbol size as an extra column of the data. Now, because I do lots of guess work to soften the use of pure GMT in GMT.jl, one of the guesses short-circuited that ability. But we can still make it work if reading the data from a file and specifying the fig limits. The data is only a _x y diameter_ ascii file.

```julia
using GMT
# Create a data file. Third column is diameter in cm
gmtwrite("lixo.dat", [0 0 1; 0.5 0.5 2; 1 1 0.5])

# plot it
plot("lixo.dat", marker=:circ, fill=:green, limits=(-0.1,1.1,-0.1,1.1), show=true, fmt=:png)

```

 ![GMTjl_tmp](https://global.discourse-cdn.com/julialang/original/3X/9/2/92dd0f349fa2db7e64f082daca524b445fbc50cd.png)

---

<div class="post-metadata">

### Author: ![anowacki](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/anowacki/32/17375_2.png) [@anowacki](https://discourse.julialang.org/u/anowacki)
#### Post date: [October 7, 2020, 11:19am UTC](https://discourse.julialang.org/t/how-to-plot-circles-with-given-radii-using-plots/47845/9 "2020-10-07T11:19:49Z")

</div>

It’s not clear just how fast this needs to be, or whether this solution would also class as ‘too slow’, but passing a `Vector{Plots.Shape}` seems acceptably fast for me with thousands of `Shape`s.

The following example will give you a load of randomly-wiggling circles.

```julia
using Plots

function circle(x, y, r=1; n=30)
    θ = 0:360÷n:360
    Plots.Shape(r*sind.(θ) .+ x, r*cosd.(θ) .+ y)
end

function move_circles!(circles; Δ=0.01)
    for c in circles
        dx = Δ*(2rand() - 1)
        dy = Δ*(2rand() - 1)
        c.x .+= dx
        c.y .+= dy
    end
    nothing
end

nframes = 100
ncircles = 1000

circles = circle.([i*rand(ncircles) for i in (1, 1, 0.01)]...)

plot_kwargs = (aspect_ratio=:equal, fontfamily="Helvetica", legend=false, line=nothing,
    color=:black, grid=false, xlim=(0,1), ylim=(0,1))

anim = @animate for _ in 1:nframes
    move_circles!(circles)
    plot(circles; plot_kwargs...)
end
gif(anim, "anim.gif")

```

![anim](https://global.discourse-cdn.com/julialang/original/3X/9/6/969280fb8ad570340629a4f658b97db47078762f.gif)

---

<div class="post-metadata">

### Author: ![SteffenPL](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/steffenpl/32/206270_2.png) [@SteffenPL](https://discourse.julialang.org/u/SteffenPL)
#### Post date: [October 7, 2020, 12:39pm UTC](https://discourse.julialang.org/t/how-to-plot-circles-with-given-radii-using-plots/47845/10 "2020-10-07T12:39:05Z")

</div>

Wow, this looks amazing.

I was constructing the circles so far one by one, since I was not sure how to call it at once. Your solution is much faster. I will implement it later. Since my code already uses Plots.jl, I guess this is my preferred approach.

---

<div class="post-metadata">

### Author: ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)
#### Post date: [October 7, 2020, 12:40pm UTC](https://discourse.julialang.org/t/how-to-plot-circles-with-given-radii-using-plots/47845/11 "2020-10-07T12:40:24Z")

</div>

rs, I thought that that was not fast enough 🙂

---

<div class="post-metadata">

### Author: ![SteffenPL](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/steffenpl/32/206270_2.png) [@SteffenPL](https://discourse.julialang.org/u/SteffenPL)
#### Post date: [October 7, 2020, 12:41pm UTC](https://discourse.julialang.org/t/how-to-plot-circles-with-given-radii-using-plots/47845/12 "2020-10-07T12:41:53Z")

</div>

@lmiq I will try to compare the speed of this approach and @anowacki 's solution. Thanks for the idea with hacking the SVG!

---

<div class="post-metadata">

### Author: ![SteffenPL](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/steffenpl/32/206270_2.png) [@SteffenPL](https://discourse.julialang.org/u/SteffenPL)
#### Post date: [October 7, 2020, 12:46pm UTC](https://discourse.julialang.org/t/how-to-plot-circles-with-given-radii-using-plots/47845/13 "2020-10-07T12:46:12Z")

</div>

On my computer the amount of `plot()` calls influences the speed of the plots,  
i.e. calling 100 times `plot()` for 100 circles seems to be much slower than one `plot()` call with 100 circles packed into the data. So, reducing the number of plot calls helped me.  
(I cannot compare the speeds now, but I will post this evening with the timings.)
