1
$\begingroup$

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 *)

enter image description here

For reference, this is how it looks like if the gridline is within the boundary (gridline at 180 rather than 300)

enter image description here

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[10], cols[[#]]} & /@ 
     Range[Length@cols]], PlotRange -> Full, 
  FrameStyle -> Directive[Black, 14, AbsoluteThickness[2]]];

with "cols" being a list with RBG colours.

$\endgroup$
2
  • 1
    $\begingroup$ 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? $\endgroup$
    – MarcoB
    Apr 18, 2022 at 16:28
  • $\begingroup$ Hi! I've added more info as an edit. I don't think they should interfere, but maybe. will test them out later. $\endgroup$
    – alex
    Apr 19, 2022 at 11:25

1 Answer 1

1
$\begingroup$

It seems that this fantastic little trick does the trick:

Where you simply set ImageSize->Automatic->size

$\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.