Here's a very bizarre inconsistency I've just struggled with and I'm wondering why it exists or if I'm missing something.
I have some noisy data and I wish to make a framed plot of the data but allow the data to extend outside the vertical limits of the frame (for stylistic reasons). Like so:
xs = Range[0, 0.5, 0.005];
data = Transpose[{xs, Sin[Pi xs]^2 + 0.05 RandomReal[{-1, 1}, Length[xs]]}];
opts = Sequence[PlotRange -> {{0, 0.5}, {0, 1}}, Frame -> True,
PlotRangeClipping -> False, ImagePadding -> 20];
ListLinePlot[{}, opts,
Prolog -> First@ListLinePlot[data, PlotRange -> All]
]
However, my dataset contains points outside my desired x limits, thus my data is more accurately given by:
xs = Range[-0.1, 0.6, 0.005];
data = Transpose[{xs, Sin[Pi xs]^2 + 0.05 RandomReal[{-1, 1}, Length[xs]]}];
Which when plotted with exactly the same code gives:
which obviously extends in both the x and y directions when I only want the extension in the y.
My solution is to change the value of PlotRange -> All
in the 'Prolog' 'ListLinePlot'. However, this only works in the y-direction, observe:
Grid[{{
ListLinePlot[{}, opts,
Prolog -> First@ListLinePlot[data, PlotRange -> {All, {0, 1}}]
],
ListLinePlot[{}, opts,
Prolog -> First@ListLinePlot[data, PlotRange -> {{0, 1}, All}]
],
ListLinePlot[{}, opts,
Prolog -> First@ListLinePlot[data, PlotRange -> {{0, 1}, {0, 1}}]
]
}}]
As you can see the data never obeys the PlotRange in the x direction! Looking into the content of the First@ListLinePlot[data, ...]
we can see that the graphics items describing the data do get clipped in the y-direction:
You can see there are several instances near the beginning where the Line
's y-coordinate has been clipped to 0.
and many at the end where it is 1.
.
However if we try to restrict the graphics in the x-direction:
We see no such clipping occurs, leading to the problems described above.
Why is this happening? Is there an elegant workaround? My current method to circumvent this problem is to create and Interpolation
of may data and then use Plot
as opposed to ListLinePlot
in the Prolog
but this seems like overkill for what should be a simple fix?
I note that merely taking a subset of my data won't work for my real data as the x-values do not lie nicely on my coordinates, ie I might not have a value at 0 so I would be left with a gap either side.
Many thanks.
Prolog
etc) was one of the simplest ways to achieve that.ListLinePlot[data, opts]
doesn't produce the first plot which is what I want. $\endgroup$PlotRangeClipping
does not allow to specify separate clipping specifications for the axes and in additionListLinePlot
handles this option for them inconsistently. :( $\endgroup$RegionFunction
is a documented option ofListLinePlot
(see under the "Details" field). But this option is not recognized and does not work (version 10.3.1)... :( $\endgroup$Prolog
, not the problem of OP. $\endgroup$