3
$\begingroup$

How can we insert a circle with an arbitrary radius, thickness and color in an arbitrary position in a plot which is created either by a list or a function (ListPlot or Plot)? enter image description here

$\endgroup$
  • 5
    $\begingroup$ Look up Prolog and Epilog. Something like: ListLinePlot[{data1, data2}, Epilog -> {Orange, Thick, Circle[center, radius], Circle[otherCenter, otherRadius]}]. $\endgroup$ – MarcoB May 4 '18 at 16:23
11
$\begingroup$

You can also use Offset to control the size (in pixels). With @eyorble's example:

pts = {x,Cos[x]} /. Solve[{Cos[x] == Sin[x],0<x<2 \[Pi]}];
Plot[
    {Cos[x], Sin[x]},
    {x, 0, 2 \[Pi]},
    Epilog->{Red, Circle[pts[[1]], Offset[10]], Blue, Circle[pts[[2]],Offset[10]]}
]

enter image description here

$\endgroup$
9
$\begingroup$

Inset is another way to deal with the aspect ratio:

pts = {x, Cos[x]} /. Solve[{Cos[x] == Sin[x], 0 < x < 2 \[Pi]}];
plot = Plot[{Cos[x], Sin[x]}, {x, 0, 2 \[Pi]}, Epilog -> {
    Inset[Graphics@{Red, Circle[{0, 0}, 1]}, pts[[1]], Center, Scaled[.25]],
    Inset[Graphics@{Blue,Circle[{0, 0}, 1]}, pts[[2]], Center, Scaled[.25]]}]

enter image description here

$\endgroup$
7
$\begingroup$

Using the method MarcoB suggested in the comments, recreating the above plot is reasonably simple. Find the intersection points and plot circles around them:

pts = {x, Cos[x]} /. Solve[{Cos[x] == Sin[x], 0 < x < 2 π}];
plot = Plot[{Cos[x], Sin[x]}, {x, 0, 2 \[Pi]}, 
    Epilog -> {Red, Circle[pts[[1]], 0.2], Blue, Circle[pts[[2]], 0.3]}]

Demonstration plot

You'll notice the circles appear vertically elongated. If you wish to have precise circles on your output, you'll need to change either the AspectRatio of the plot, or plot the circles elongated to account for the AspectRatio. The former is pretty simple, but the latter is a little bit trickier so I have implemented it below:

plot = Plot[{Cos[x], Sin[x]}, {x, 0, 2 π}, 
    Epilog -> {Red, Circle[pts[[1]], {1, ImageAspectRatio[plot]} 0.2], 
    Blue, Circle[pts[[2]], {1, ImageAspectRatio[plot]} 0.5]}]

Demonstration plot with corrected circle aspect ratios.

Note however that this plot depends on the previous output of plot, so you'll need to plot it without the adjusted circles at least once to get it started. There are ways to avoid that issue, but they mostly revolve around determining the image aspect ratio in some other way prior.

$\endgroup$
5
$\begingroup$

Far from as elegant as using code like other answers but if you are just trying to highlight the intersection, drawing tools is pretty quick (right click)

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.