Skip to content

Improve support for logarithmic axes#1393

Merged
swharden merged 8 commits intomasterfrom
minor-tick-log
Oct 21, 2021
Merged

Improve support for logarithmic axes#1393
swharden merged 8 commits intomasterfrom
minor-tick-log

Conversation

@swharden
Copy link
Member

@swharden swharden commented Oct 21, 2021

This PR attempts to address issues raised in #1386 related to log-scale tick placement.

  • minor tick positions aren't the expected ones

  • major tick positions look bad if tick labels aren't powers of 10

image

double[] xs = { 1, 2, 3, 4, 5 };
double[] ys = { 10, 2_000, 50_000, 1_000_000, 1_500_000 };

var plt = new ScottPlot.Plot();

// Plot the log of the Ys
double[] logYs = ys.Select(y => Math.Log10(y)).ToArray();
var scatter = plt.AddScatter(xs, logYs);

// label each point with a red line
plt.Palette = ScottPlot.Palette.ColorblindFriendly;
for (int i = 0; i < scatter.PointCount; i++)
{
    double x = scatter.Xs[i];
    double y = scatter.Ys[i];
    double actualY = Math.Pow(10, y);
    var color = plt.Palette.GetColor(i);
    plt.AddHorizontalLine(y, color, 1, ScottPlot.LineStyle.Dot);
    plt.AddVerticalLine(x, color, 1, ScottPlot.LineStyle.Dot);
    plt.AddPoint(x, y, color, 10, ScottPlot.MarkerShape.openCircle);
    var txt = plt.AddText(actualY.ToString("N0"), x, y, 12, System.Drawing.Color.Black);
}

// Use a custom tick formatter to label tick marks as the antilog of their position
Func<double, string> tickLabeler = (y) => Math.Pow(10, y).ToString("N0");
plt.YAxis.TickLabelFormat(tickLabeler);

// Use log-spaced tick marks and grid lines to make it more convincing
plt.YAxis.MinorGrid(enable: true);
plt.YAxis.MinorLogScale(true);

// Set the axis limits manually to ensure edges terminate at a nice locations in log space
plt.SetAxisLimits(.5, 5.5, 0, Math.Log10(10_000_000));

Related: #29, #41, #207, #412

Resolves #1386

@swharden swharden changed the title Tests: demonstrate log scale issues Improve support for logarithmic axes Oct 21, 2021
fixes one of the primary issues raised in #1386
Create an object to represent a tick (tick mark, tick label, grid line, etc.) which can be used when refactoring the tick system
Create a mechanism to get tick information out of the tick collection
@swharden
Copy link
Member Author

swharden commented Oct 21, 2021

This is working a lot better now

image

integer rounding? It still looks bad when tick marks are placed at 0.5 and 0.25 increments though... Perhaps a fix is a flag to "only use integer major ticks"?

image

EDIT: Yeah, that fixed it:

image

@swharden swharden marked this pull request as ready for review October 21, 2021 03:24
@swharden swharden merged commit 041a1a4 into master Oct 21, 2021
@swharden swharden deleted the minor-tick-log branch October 21, 2021 03:24
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

Successfully merging this pull request may close these issues.

Ticks: improve positions of log-scaled minor ticks

1 participant