2
$\begingroup$

I am trying to plot a graph similar to

enter image description here

I have tried using ContourPlot with GridLines as

ContourPlot[x^2 + y^2 == 400, {x, -40, 40}, {y, -40, 40}, 
 LabelStyle -> Directive[Black, FontSize -> 18], 
 GridLines -> Automatic]

However this only plots a circle with a transparent interior. But I want the inside to be dark just like in the figure. Also GridLines only shows the dark lines around -40,-20,0,20,24 without the grey lines inbetween. Is there a better way to do it?

$\endgroup$
8
  • 1
    $\begingroup$ ContourPlot should only plot a circle, which is the solution of your equation. Try RegionPlot[x^2 + y^2 < 400, {x, -40, 40}, {y, -40, 40}] instead. Another approach would be using Disk[] and Line. $\endgroup$
    – anderstood
    Oct 21, 2016 at 18:17
  • $\begingroup$ @anderstood, But how to use Disk[] with gridlines? $\endgroup$
    – MrDi
    Oct 21, 2016 at 18:25
  • $\begingroup$ @MrDi You could something like this: Graphics[{Gray, Disk[{0, 0}, 20]}, Frame -> {{Automatic, None}, {Automatic, None}}, PlotRange -> {{-50, 50}, {-50, 50}}, GridLines -> {Range[-50, 50, 2], Range[-50, 50, 2]}]. $\endgroup$
    – user31159
    Oct 21, 2016 at 18:27
  • $\begingroup$ @xavier, thank you this is really helpful but can the lines around -40,-20,0,20,40 be dark than the others. $\endgroup$
    – MrDi
    Oct 21, 2016 at 18:29
  • 1
    $\begingroup$ I am going to post an answer with this additional update. $\endgroup$
    – user31159
    Oct 21, 2016 at 18:42

2 Answers 2

6
$\begingroup$

You can alternatively use Graphics with the primitive Disk.

Definitions.

disk = {Darker[Gray, 0.5], Disk[{0, 0}, 20]};
frame = {{Automatic, None}, {Automatic, None}};
plotrange = {{-50, 50}, {-50, 50}};

gridlines = ConstantArray[
   Join[
        (* all lines *)
        {#, Directive[LightGray, Thin]} & /@ Range[-50, 50, 2], 
        (* main lines *)
        {#, Directive[Lighter[Black, 0.5]]} & /@ Range[-40, 40, 20]], 
   2
];

labelstyle = Directive[Black, FontSize -> 18];

Graphics.

Graphics[disk, 
         Frame -> frame, PlotRange -> plotrange, GridLines -> gridlines, 
         LabelStyle -> labelstyle
]

enter image description here

Comments.

The symbol gridlines could be instead defined with a DownValues, as shown by anderstood in his answer. This would read here:

Clear[gridlines];
gridlines[min_, max_] := Table[
     If[Divisible[i, 20], 
         (* main lines *)
         {i, Directive[Lighter[Black, 0.5]]}, 
         (* other lines *)
         {i, Directive[LightGray, Thin]}
     ], 
     {i, Ceiling[min], Floor[max], 2}
];

Update

To get outward ticks, as in the graphics of the question, you can define:

myticks[min_, max_] := Table[
    If[Divisible[i, 20],
         (* ticks for main lines *)
         {i, i, {0, 0.005}},
         (* ticks for other lines *)
         {i, "", {0, 0.005}}
    ],
    {i, Ceiling[min], Floor[max], 2}
];

frameticks = {{myticks[##] &, None}, {myticks[##] &, None}};

which gives

Graphics[disk, 
     Frame -> frame, FrameTicks -> frameticks, 
     PlotRange -> plotrange, GridLines -> gridlines, 
     LabelStyle -> labelstyle
]

enter image description here

$\endgroup$
3
  • $\begingroup$ What about ticks? yours are inward... should be outward? $\endgroup$
    – BlacKow
    Oct 21, 2016 at 19:08
  • $\begingroup$ @BlacKow Good point! I will make an update to this answer later today. $\endgroup$
    – user31159
    Oct 21, 2016 at 19:16
  • $\begingroup$ @BlacKow Please see update. $\endgroup$
    – user31159
    Oct 21, 2016 at 19:41
3
$\begingroup$

Using your approach:

grids[min_, max_] := 
 Table[If[Mod[i, 20] == 0, {i, Black}, {i, Gray}], {i, Ceiling[min], 
   Floor[max], 1}]
RegionPlot[x^2 + y^2 < 400, {x, -40, 40}, {y, -40, 40}, GridLines -> grids]

enter image description here

Slight variation: increase grid spacing, change blue to gray, increase ticks label size and reduce disk diameter with:

grids[min_, max_] := 
     Table[If[Mod[i, 20] == 0, {i, Black}, {i, Gray}], {i, -40, 40, 2}]
RegionPlot[x^2 + y^2 < 300, {x, -40, 40}, {y, -40, 40}, 
     GridLines -> grids, PlotStyle -> Directive[Lighter[Black, 0.4]], 
     BoundaryStyle -> Gray, LabelStyle -> Directive[Black, FontSize -> 18]]

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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