Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Signal plot bug affecting GetLimits() when min/max render index is in use #621

Closed
LB767 opened this issue Nov 21, 2020 · 6 comments
Closed

Comments

@LB767
Copy link

LB767 commented Nov 21, 2020

When using a sampleRate different than 1 in SignalPlot & SignalPlotConst the min and maxRenderIndex properties don't seem to behave properly anymore.
I tried to play with the sampleRate value to see if I could see a pattern that made sense (like min and max being double for a sampleRate of 2 or something) but I couldn't see anything...

Just try

plt.PlotSignalConst(ys, sampleRate: 2, minRenderIndex: 100, maxRenderIndex: 200);

and at least for me this doesn't work
(Using the latest Scottplot 4.0.42 with WPF)

@LB767 LB767 added the BUG unexpected behavior label Nov 21, 2020
@swharden
Copy link
Member

Hi @LB767,

I believe minRenderIndex and maxRenderIndex define the index in the ys array to display. It is not sensitive to sample rate. I hope this helps!

@swharden swharden removed the BUG unexpected behavior label Nov 21, 2020
@LB767
Copy link
Author

LB767 commented Nov 21, 2020

Yes, that is exactly what I expected, but it doesn't work like that. As I mentionned the displayed data changes based on the samplerate value, and with weird behavior, sometimes it doesn't display any data all, as if the min and max ended up on the same index...

Edit: I tried it in one of the WPF demos, for instance https://github.com/swharden/ScottPlot/blob/a8292b19c6f4676d97c4e42697b962555613fdcc/src/demo/ScottPlot.Demo.WPF/WpfDemos/PlotInScrollViewer.xaml.cs#L33
Just put some minRenderIndex and maxRenderIndex and put a sampleRate other than 1, it seems the maximum actually works correctly (for a sampleRate of 2 you get half the max on the X axis) but not the minimum...

@swharden swharden reopened this Nov 21, 2020
@swharden
Copy link
Member

swharden commented Nov 21, 2020

Hey @LB767, I tested this by writing code that plots 1k points with a sample rate of 10 (so 1,000 points spans 0-100 on the horizontal axis). The output renders as I would expect.

Can you provide a code sample that produces output different than you'd expect? Perhaps I'm misunderstanding your concern.

Random rand = new Random(0);
double[] ys = ScottPlot.DataGen.RandomWalk(rand, 1000);

var plt = new ScottPlot.Plot();
var sig = plt.PlotSignal(ys, sampleRate: 10);

plt.Title("no limit");
plt.SaveFig("1.png");

sig.minRenderIndex = 450;
sig.maxRenderIndex = ys.Length - 1;
plt.Title("lower data removed");
plt.SaveFig("2.png");

sig.minRenderIndex = 0;
sig.maxRenderIndex = 550;
plt.Title("upper data removed");
plt.SaveFig("3.png");

sig.minRenderIndex = 450;
sig.maxRenderIndex = 550;
plt.Title("lower and upper data removed");
plt.SaveFig("4.png");

image

image

image

image

Hope this helps!
Scott

@LB767
Copy link
Author

LB767 commented Nov 21, 2020

Hey @swharden , thank you for taking the time to try this out on your side. Looks like there's more head-scratching involved than I assumed....
I copy-pasted your code, and sure enough it worked exactly as it should, so I tried stripping it down and apparently this works

Random rand = new Random(0);
double[] ys = ScottPlot.DataGen.RandomWalk(rand, 1000);

var plt = new ScottPlot.Plot();
var sig = plt.PlotSignal(ys, sampleRate: 10);
                          
plt.SaveFig("1.png");      
  
sig.minRenderIndex = 450;
sig.maxRenderIndex = 550;
plt.Title("lower and upper data removed");
plt.SaveFig("4.png");

But this doesn't, and produces a completely empty plot:

Random rand = new Random(0);
double[] ys = ScottPlot.DataGen.RandomWalk(rand, 1000);

var plt = new ScottPlot.Plot();
var sig = plt.PlotSignal(ys, sampleRate: 10);
                          
sig.minRenderIndex = 450;
sig.maxRenderIndex = 550;
plt.Title("lower and upper data removed");
plt.SaveFig("4.png");

I frankly have no idea what's going on here, it almost seems like it's more to do with the layout of the plot where something might get set when calling SaveFig() which goes into a weird state if it's called with minRenderIndex & maxRenderIndex set. No clue... I'm gonna try and play with it more in the meantime, see if I can find any lead...

Thanks again for your time

@swharden
Copy link
Member

swharden commented Nov 22, 2020

I think this a bug related to how automatic axis limits are detected in signal plots with min/max render index specified. I added some code to the end of your example and it seems that the X limits are broken (with the upper limit being a smaller number than the lower limit)

Console.WriteLine($"Axis Limits: {new ScottPlot.Config.AxisLimits2D(plt.Axis())}");
Axis Limits: x1=471.944, x2=33.056, y1=13.876, y2=23.248

A quick fix is to call AxisAuto() before setting the min/max render index. I recognize this isn't a great solution though, because middle-clicking in a user control will call AxisAuto() later and the problem will happen again.

Random rand = new Random(0);
double[] ys = ScottPlot.DataGen.RandomWalk(rand, 1000);

var plt = new ScottPlot.Plot(400, 300);
var sig = plt.PlotSignal(ys, sampleRate: 10);

plt.AxisAuto(); // set good limits before hiding data

sig.minRenderIndex = 450;
sig.maxRenderIndex = 550;
plt.Title("lower and upper data removed");

plt.SaveFig("4.png");

image

I think the problem is here:

https://github.com/swharden/ScottPlot/blob/4bf514cfe65e19d644db25f961a81f30e44caa2d/src/ScottPlot/plottables/PlottableSignalBase.cs#L197-L198

It should probably be

limits[0] = _samplePeriod * minRenderIndex + xOffset; 
limits[1] = _samplePeriod * maxRenderIndex + xOffset; 

I'll continue to explore this and issue a fix shortly...

@swharden swharden reopened this Nov 22, 2020
swharden added a commit that referenced this issue Nov 22, 2020
failing test demonstrating bug described in #621
swharden added a commit that referenced this issue Nov 22, 2020
Addresses the min/max render index problem raised in #621
swharden added a commit that referenced this issue Nov 22, 2020
@swharden
Copy link
Member

swharden commented Nov 23, 2020

I just published 4.0.44 on NuGet which should have this bug corrected!

AxisAuto() still doesn't ignore invisible plots, but the code example above should get the job done 👍
EDIT: I mixed-up this issue with #623

@swharden swharden changed the title min and maxRenderIndex broken when using sampleRate/samplePeriod in SignalPlot? Signal plot bug affecting GetLimits() when min/max render index is in use Nov 23, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants