Skip to content

Log10 returns NaN which breaks GetLimits() #376

@bclehmann

Description

@bclehmann

Describe the bug
If you attempt to use a ScottPlot.Tools.Log10 on a dataset which contains 0, it will appear to work properly. However, Math.Log10(0) returns double.NegativeInfinity, which does not play nicely with Plottable.GetLimits(). It causes the axis to return limits of [double.NaN, double.NaN]. This does not cause a crash, but simply for the plot to not draw.

To Reproduce
Use ScottPlot.Tools.Log10 on a dataset which contains 0.

I modified the Log Axis demo to look like this:

public class LogAxis : PlotDemo, IPlotDemo
        {
            public string name { get; } = "Log Axis";
            public string description { get; } = "";

            public void Render(Plot plt)
            {
                // generate some interesting log-distributed data
                int pointCount = 200;
                double[] dataXs = new double[pointCount];
                double[] dataYs = new double[pointCount];
                Random rand = new Random(0);
                for (int i = 0; i < pointCount; i++)
                {
                    double x = 10.0 * i / pointCount;
                    dataXs[i] = x;
                    dataYs[i] = Math.Pow(2, x) + rand.NextDouble() * i;
                }

                dataYs[0] = 0;

                // this tool can convert linear data to log data
                double[] dataYsLog = ScottPlot.Tools.Log10(dataYs);

                foreach(double curr in dataYsLog) {
                    Debug.WriteLine(curr);
                }
                plt.PlotScatter(dataXs, dataYsLog, lineWidth: 0);

                // call this to move minor ticks to simulate a log scale
                plt.Ticks(logScaleY: true);

                plt.Title("Data (Log Scale)");
                plt.YLabel("Vertical Units (10^x)");
                plt.XLabel("Horizontal Units");
            }
        }

Screenshots
If applicable, add screenshots to help explain your problem
Output for given code:

image

Debug output: https://pastebin.com/9vYd9Hdm

Metadata

Metadata

Assignees

No one assigned

    Labels

    BUGunexpected behavior

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions