# Gridlines overrule PlotRange

edit at the end

I encountered an interesting bug (?) when plotting where if a Gridline is outside the boundaries set by PlotRange, then the plot appears distorted and automatically resizes so that the full span of the image includes the 'invisible' gridline.

Here is a quick example where I plot two frequencies at 180 and 160 Hz, specify the PlotRange to only look at the desired range, but if I introduce a gridline at 300 Hz, which lies outside the visible range, it distorts.

nSamples = 2^14;
sRate = 2^14;
time = Array[# &, nSamples, {0, nSamples/sRate}];
x1 = Sin[2 \[Pi] 180 time];                                           \
(* generate the first frequency *)
x2 = Sin[2 \[Pi] 160 time];                                           \
(* generate the second frequency *)
x = (x1 + x2)/2;
fft = (Abs@
Fourier[x,
FourierParameters -> {-1,
1}])^2;            (* generate power spectrum*)
fSpace = Table[(n - 1.) sRate/Length[fft], {n,
Length[fft]}]; (* create the frequency space *)
ListLinePlot[{fSpace, fft}\[Transpose],
PlotRange -> {{150, 210}, Full},
GridLines -> {{300}, None}] (* plot *) For reference, this is how it looks like if the gridline is within the boundary (gridline at 180 rather than 300) Does anyone know how can this be circumvented? I must note that in both cases the ImageSize is fixed.

EDIT: I'm using v.13.0 on Windows and the following options:

SetOptions[{ListPlot},
Frame -> {True, True, False, False}, ImageSize -> 350,
PlotStyle ->
Evaluate[{AbsolutePointSize, cols[[#]]} & /@
Range[Length@cols]], PlotRange -> Full,
FrameStyle -> Directive[Black, 14, AbsoluteThickness]];


with "cols" being a list with RBG colours.

• I cannot reproduce the behavior in your first plot (v. 12.3.1 on Win10-64). Particularly, I notice that your plot uses non-standard colors even though no style directives have bee included in your code, so I wonder if you have set some global preferences that are impacting other behaviors in unexpected ways. What version are you using? What OS? Apr 18, 2022 at 16:28
• Hi! I've added more info as an edit. I don't think they should interfere, but maybe. will test them out later.
– alex
Apr 19, 2022 at 11:25

Where you simply set ImageSize->Automatic->size