DateTime Axis Fixed Interval Ticks
Make ticks render at fixed intervals. Optionally make the ticks render from a custom start date, rather than using the start date of the plot (e.g. to draw ticks on the hour every hour, or on the first of every month, etc).
ScottPlot.Plot myPlot = new();
// Plot 24 hours sample DateTime data (1 point every minute)
DateTime[] dates = Generate.ConsecutiveMinutes(24 * 60, new DateTime(2000, 1, 1, 2, 12, 0));
double[] ys = Generate.RandomWalk(24 * 60);
myPlot.Add.Scatter(dates, ys);
var dtAx = myPlot.Axes.DateTimeTicksBottom();
// Create fixed-intervals ticks, major ticks every 6 hours, minor ticks every hour
dtAx.TickGenerator = new ScottPlot.TickGenerators.DateTimeFixedInterval(
new ScottPlot.TickGenerators.TimeUnits.Hour(), 6,
new ScottPlot.TickGenerators.TimeUnits.Hour(), 1,
// Here we provide a delegate to override when the ticks start. In this case, we want the majors to be
// 00:00, 06:00, 12:00, etc. and the minors to be on the hour, every hour, so we start at midnight.
// If you do not provide this delegate, the ticks will start at whatever the Min on the x-axis is.
// The major ticks might end up as 1:30am, 7:30am, etc, and the tick positions will be fixed on the plot
// when it is panned around.
dt => new DateTime(dt.Year, dt.Month, dt.Day));
// Customise gridlines to make the ticks easier to see
myPlot.Grid.XAxisStyle.MajorLineStyle.Color = Colors.Black.WithOpacity();
myPlot.Grid.XAxisStyle.MajorLineStyle.Width = 2;
myPlot.Grid.XAxisStyle.MinorLineStyle.Color = Colors.Gray.WithOpacity(0.25);
myPlot.Grid.XAxisStyle.MinorLineStyle.Width = 1;
myPlot.Grid.XAxisStyle.MinorLineStyle.Pattern = LinePattern.DenselyDashed;
// Remove labels on minor ticks, otherwise there is a lot of tick label overlap
myPlot.RenderManager.RenderStarting += (s, e) =>
{
Tick[] ticks = myPlot.Axes.Bottom.TickGenerator.Ticks;
for (int i = 0; i < ticks.Length; i++)
{
if (ticks[i].IsMajor)
{
continue;
}
ticks[i] = new Tick(ticks[i].Position, "", ticks[i].IsMajor);
}
};
myPlot.SavePng("demo.png", 400, 300);
This recipe is one of many in the Axis and Ticks category
