How to preserve scaled circular shapes when combining graphics?

4

The following code

g1 = Graphics[{Thickness[0.01], Line[{{0, 0}, {10, 10}}], Disk[{5, 3}, Scaled[0.1]]}, Frame -> True]

produces the following output

enter image description here

I then create another graphic with a non-square aspect ratio, and then combine the two graphics together:

g2 = Graphics[{Blue, Thickness[0.01], Arrow[{{0, 0}, {20, 10}}]}];
Show[g1, g2]

In this case, I obtain the following output with a squashed disk:

enter image description here

This can, of course, be fixed by specifying an aspect ratio of 1:

Show[g1, g2, AspectRatio -> 1]

enter image description here

But this gives rise to various other problems when combining multiple graphics and then forcing the combined graphic to have an aspect ratio of 1 (in particular when the coordinate height and width of the combined graphics are very different).

So, I'm wondering if there is a way to preserve scaled circular shapes when combing graphics without needing to adjust the aspect ratio. Thanks!

AnInquiringMind

Posted 2018-06-26T18:17:06.173

Reputation: 723

Answers

5

In the case of Disk you can use Offset radius specification, but it will make the radius independent from the scale of the plot:

g1 = Graphics[{Thickness[0.01], Line[{{0, 0}, {10, 10}}], 
    Disk[{5, 3}, Offset[20]]}, Frame -> True];
g2 = Graphics[{Blue, Thickness[0.01], Arrow[{{0, 0}, {20, 10}}]}];
Show[g1, g2]

output

Inset offers more flexibility:

g1 = Graphics[{Thickness[0.01], Line[{{0, 0}, {10, 10}}], 
    Inset[Graphics[{Disk[{0, 0}, 1]}], {5, 3}, {0, 0}, 2.5]}, 
   Frame -> True];
g2 = Graphics[{Blue, Thickness[0.01], Arrow[{{0, 0}, {20, 10}}]}];
Show[g1, g2]

output

(Note that you can specify the size of Inset also in Scaled and Offset forms.)

When you need exactly filled disks, you can use Point instead of Disk to get what you want (this also is beneficial from the performance viewpoint):

g1 = Graphics[{Thickness[0.01], Line[{{0, 0}, {10, 10}}], 
    PointSize[0.1], Point[{5, 3}]}, Frame -> True];
g2 = Graphics[{Blue, Thickness[0.01], Arrow[{{0, 0}, {20, 10}}]}];
Show[g1, g2]

output

Alexey Popkov

Posted 2018-06-26T18:17:06.173

Reputation: 50 220

Thanks for the response. What would you suggest doing if one of the various graphics to be combined included many disks (say, hundreds)? Would a single inset work for all of them, or would I need to have a different inset for each of them? – AnInquiringMind – 2018-06-26T18:46:32.117

@AnInquiringMind I would suggest to use Translate with Inset as the argument (if all disks should be the same). If the disks should be different, then probably you need separate Inset for each of them. But you could also try to experiment with a single Inset containing a lot of disks if the performance is critical for you. – Alexey Popkov – 2018-06-26T18:49:46.857

@AnInquiringMind I've updated the answer. – Alexey Popkov – 2018-06-26T18:57:08.983

Thanks for the update. I had actually shifted away from using Point a while ago, in part because of the greater flexibility allowed by Disk, but this might get me to move back. Will explore further. – AnInquiringMind – 2018-06-26T19:08:24.783