ScottPlot/ScottPlot

Axes: add support for multiplier and offset notation

zichen-ye opened this issue · 3 comments

Question: How do I implement 4.0's TickLabelNotation methods in ScottPlot5.0

ScottPlot Version: 5.0

plot.Plot.YAxis.TickLabelNotation(multiplier: true);

Hi @zichen-ye, I think you are referring to the little numbers in the corner of the axes indicating an offset and/or multiplier:

https://scottplot.net/cookbook/4.1/recipes/ticks_multiplier/

That feature is not currently in ScottPlot 5, but I'll use this issue to track adding it in 👍

While this feature could be added in, IMO it is much simpler just to use a custom tick formatter. As a result, you get far more control over how the ticks are formatted, such as if there is a threshold where scientific notation should be used, how it should be formatted (e.g. lowercase or capital e, whether it should be e+5 or just e5, etc), and more.

Here is a sample. In this case, I only render in scientific notation if the largest tick on the plot is above 10,000, and I use C# format strings for the notation; see docs here.

It does do some unnecessary computation of the largestTickMagnitude for every tick, which is inefficient, but this is probably only running less than 20 times per render, so it's fine.

string CustomFormatter(double position)
{
    if (position == 0)
    {
        return "0";
    }

    var largestTickMagnitude = Math.Max(Math.Abs(myPlot.Axes.Left.Range.Min), Math.Abs(myPlot.Axes.Left.Max));
    if (largestTickMagnitude <= 10_000)
    {
        // Too small to require scientific notation
        return position.ToString(CultureInfo.CurrentCulture);
    } 
    
    return $"{position:0.##e0}";;
}
WinForms_Demo_zbjsOKZYEh.mp4

In ScottPlot 4, there are a few edge cases which make this style of format difficult, such as dealing with large font sizes:
image

That being said, it is simple enough to implement your own custom Axis now (inherit from YAxisBase) and override Render and draw the text for the exponent wherever you like. You might need to make space above the Y axis somehow as it is a bit tight without a plot title - the pink below shows the bounds of the figure.

image

While this feature could be added in, IMO it is much simpler just to use a custom tick formatter.

Thanks a great point! Thanks for the example code you shared too.

@zichen-ye is this strategy sufficient to meet your needs?