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

Axes: add support for multiplier and offset notation #3827

Closed
zichen-ye opened this issue May 14, 2024 · 3 comments
Closed

Axes: add support for multiplier and offset notation #3827

zichen-ye opened this issue May 14, 2024 · 3 comments
Labels
Help Wanted Scott won't do this soon, but PRs from the community are welcome!

Comments

@zichen-ye
Copy link

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

ScottPlot Version: 5.0

plot.Plot.YAxis.TickLabelNotation(multiplier: true);
@swharden
Copy link
Member

swharden commented May 14, 2024

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 👍

@swharden swharden reopened this May 14, 2024
@swharden swharden changed the title How do I implement 4.0's TickLabelNotation methods in ScottPlot5.0 Axes: add support for multiplier and offset notation May 14, 2024
@swharden swharden added the Help Wanted Scott won't do this soon, but PRs from the community are welcome! label May 14, 2024
@EricEzaM
Copy link
Contributor

EricEzaM commented May 16, 2024

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

@swharden
Copy link
Member

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?

@swharden swharden closed this as not planned Won't fix, can't repro, duplicate, stale May 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Help Wanted Scott won't do this soon, but PRs from the community are welcome!
Projects
None yet
Development

No branches or pull requests

3 participants