3
$\begingroup$

I want to draw ListPlots with automatic GridLines and add a custom line.

My solution is to use the Epilog option. To make sure that the automatic GridLines and my custom line have the same style, I create a style Directive and use the GridLinesStyle option. Here is an example:

defGrSty = Directive[{Thickness[0.0002], Black, Opacity[1.0]}];
defEp = {defGrSty, InfiniteLine[{{0, 5}, {2, 5}}]};
data = {{1, -12}, {2, 3}, {3, 10}, {4, 20}, {5, 42}};

ListPlot[data, PlotRange -> Full, GridLinesStyle -> defGrSty, 
GridLines -> Automatic, Epilog -> defEp]

My problem is, that the manual added line looks different. It does not look black, but gray. The cause could be, that the opacity of this line is decreased at the edges.

$\endgroup$
  • $\begingroup$ Are you working with Magnification different that 100%? Because for 100% it's ok but for e.g. 110% is looks strange. $\endgroup$ – Kuba Dec 22 '15 at 10:40
  • $\begingroup$ Which magnification do you mean? The magnification of the window? With the following modified code I have the same problems: defGrSty = Directive[{Thickness[0.0002], Black, Opacity[1.0], Magnification -> 1}]; defEp = {defGrSty, Magnification -> 1, InfiniteLine[{{0, 5}, {2, 5}}]}; data = {{1, -12}, {2, 3}, {3, 10}, {4, 20}, {5, 42}}; Show[ListPlot[data, PlotRange -> Full, GridLinesStyle -> defGrSty, GridLines -> Automatic, Epilog -> defEp], Magnification -> 1] I am using Mathematica 10.1 . $\endgroup$ – user5415068 Dec 22 '15 at 10:51
  • $\begingroup$ Yes, Notebook, what is on the bottom right corner. $\endgroup$ – Kuba Dec 22 '15 at 11:08
  • $\begingroup$ I have set it 100% and the problem remains. It seems, that the appearance of this line depends on the Mathematica version or installation. I use version 10.1.0.0 for Linux $\endgroup$ – user5415068 Dec 22 '15 at 11:22
  • $\begingroup$ Looks like a bug to me, for certain values of the Thickness, the gridlines and the line have the same apparent style and for others they do not. $\endgroup$ – Jason B. Dec 22 '15 at 11:26
1
$\begingroup$

Okay, so you run your code,

defGrSty = Directive[{Thickness[0.0002], Black, Opacity[1.0]}];
defEp = {defGrSty, InfiniteLine[{{0, 5}, {2, 5}}]};
data = {{1, -12}, {2, 3}, {3, 10}, {4, 20}, {5, 42}};

plot = ListPlot[data, PlotRange -> Full, GridLinesStyle -> defGrSty, 
  GridLines -> Automatic, Epilog -> defEp]

and it does not display the gridlines and the added line in the same style,

enter image description here

You have a couple options. If you want to use the output elsewhere, just export it to PDF, and it displays just fine

Export["temp/plot.pdf", plot]

enter image description here

Or, if you want it to display in the notebook properly, you can go through this rigmarole

grdlns = AbsoluteOptions[plot, GridLines][[1, 2, All, All, 1]];
AppendTo[grdlns[[2]], 5.];
ListPlot[data, PlotRange -> Full, GridLines -> glns, 
 GridLinesStyle -> defGrSty]

enter image description here

At the heart of the matter, it seems to do some kind of rounding for the values of Thickness that are given to GridLinesStyle. Here, for each pair the one on the left is a Line and the one on the right is a GridLine, both with the same style,

opts = {ImageSize -> {50, 100}, AspectRatio -> 2, 
   PlotRange -> {{-1, 1}, {-2, 2}}, Axes -> False};
img = Grid[{GraphicsRow /@ ((defGrSty = 
         Directive[{Thickness[#], Black, Opacity[1.0]}];
        {Graphics[{Directive[defGrSty], Line[{{0, -2}, {0, 2}}]}, 
          opts],
         Plot[Null, {x, -2, 2}, GridLines -> {{0}, None}, 
          GridLinesStyle -> defGrSty, Evaluate@opts]}) & /@ {0.001, 
       0.002, 0.01, 0.02, .03, 0.04})}]

enter image description here

But again, it goes away if you export to pdf, as LLlAMnYP pointed out.

$\endgroup$
  • $\begingroup$ Thank you very much for these two solutions and the extensive investigation of the causes. $\endgroup$ – user5415068 Dec 22 '15 at 14:26
1
$\begingroup$

You can ensure that the custom grid line has the same style by adding it to the GridLines specification. Then it will be drawn with all the other grid lines.

ListPlot[data, PlotRange -> Full, GridLinesStyle -> defGrSty,
 GridLines -> {Automatic, Append[5]@Range[-10, 40, 10]}]

enter image description here

Hope this helps.

$\endgroup$
  • $\begingroup$ Setting the horizontal GridLines manually is a possible solution. I don't want to use it. I have many plots with differing y-axis ticks. Thus I want to use the automatic generated GridLines with one manually added line, which everytime is needed. Thank you. $\endgroup$ – user5415068 Dec 22 '15 at 14:17

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.