设置轴限制
Axis Limits 可由用户设置。
WpfPlot1.Plot.Add.Signal(Generate.Sin(51));
WpfPlot1.Plot.Add.Signal(Generate.Cos(51));
WpfPlot1.Plot.Axes.SetLimits(-100, 150, -5, 5);
WpfPlot1.Refresh();
读取轴限制
使用 GetLimits() 获取当前轴限制。
WpfPlot1.Plot.Add.Signal(Generate.Sin(51));
WpfPlot1.Plot.Add.Signal(Generate.Cos(51));
AxisLimits limits = WpfPlot1.Plot.Axes.GetLimits();
double xMin = limits.Left;
double xMax = limits.Right;
double yMin = limits.Bottom;
double yMax = limits.Top;
WpfPlot1.Refresh();
用于拟合数据的 AutoScale 轴限制
可以自动调整轴限制以适应数据。可选参数允许用户定义数据边缘周围的空白量。在旧版本的 ScottPlot 中,此功能是通过名为 AxisAuto() 的方法实现的。
WpfPlot1.Plot.Add.Signal(Generate.Sin(51));
WpfPlot1.Plot.Add.Signal(Generate.Cos(51));
// set limits that do not fit the data
WpfPlot1.Plot.Axes.SetLimits(-100, 150, -5, 5);
// reset limits to fit the data
WpfPlot1.Plot.Axes.AutoScale();
WpfPlot1.Refresh();
倒轴
用户可以通过设置轴限制,将下边缘设置为比上边缘更正的值,在倒轴上显示数据。
WpfPlot1.Plot.Add.Signal(Generate.Sin());
WpfPlot1.Plot.Add.Signal(Generate.Cos());
WpfPlot1.Plot.Axes.SetLimitsY(bottom: 1.5, top: -1.5);
WpfPlot1.Refresh();
倒置自动轴
自定义自动轴缩放器的逻辑,以确保在自动缩放时,特定轴的轴限制始终反转。
WpfPlot1.Plot.Add.Signal(Generate.Sin());
WpfPlot1.Plot.Add.Signal(Generate.Cos());
WpfPlot1.Plot.Axes.AutoScaler.InvertedY = true;
WpfPlot1.Refresh();
平方轴单位
可以设置轴规则,强制垂直刻度(每像素单位数)与水平刻度匹配,因此圆始终显示为圆,而不是拉伸的椭圆。
WpfPlot1.Plot.Add.Circle(0, 0, 10);
// force pixels to have a 1:1 scale ratio
WpfPlot1.Plot.Axes.SquareUnits();
// even if you try to "stretch" the axis, it will adjust the axis limits automatically
WpfPlot1.Plot.Axes.SetLimits(-10, 10, -20, 20);
WpfPlot1.Refresh();
带字幕的轴
用户可以创建自己的完全自定义轴来替换默认轴(如演示 App 中所示)。对于可能对替代轴显示样式感兴趣的用户,可以使用一些实验性轴。
// Plot some sample data
WpfPlot1.Plot.Add.Signal(Generate.Sin());
WpfPlot1.Plot.Add.Signal(Generate.Cos());
// Instantiate a custom axis and customize it as desired
ScottPlot.AxisPanels.Experimental.LeftAxisWithSubtitle customAxisY = new()
{
LabelText = "My Custom Y Axis",
SubLabelText = "It comes with a subtitle for the axis"
};
// Remove the default Y axis and add the custom one to the plot
WpfPlot1.Plot.Axes.Remove(WpfPlot1.Plot.Axes.Left);
WpfPlot1.Plot.Axes.AddLeftAxis(customAxisY);
WpfPlot1.Refresh();
轴抗锯齿
为了提高垂直线和水平直线的清晰度,默认情况下,轴帧、刻度线和网格线的消除锯齿处于禁用状态。通过调用 AntiAlias 帮助程序方法,可以为所有这些对象启用抗锯齿。
double[] dataX = { 1, 2, 3, 4, 5 };
double[] dataY = { 1, 4, 9, 16, 25 };
WpfPlot1.Plot.Add.Scatter(dataX, dataY);
WpfPlot1.Plot.Axes.AntiAlias(true);
WpfPlot1.Refresh();
隐藏轴 和 打开/关闭框架线
演示如何隐藏轴刻度以及打开和关闭框架线。
WpfPlot1.Plot.Add.Signal(Generate.Sin());
WpfPlot1.Plot.Add.Signal(Generate.Cos());
// Hide axis label and tick
WpfPlot1.Plot.Axes.Bottom.TickLabelStyle.IsVisible = false;
WpfPlot1.Plot.Axes.Bottom.MajorTickStyle.Length = 0;
WpfPlot1.Plot.Axes.Bottom.MinorTickStyle.Length = 0;
// Hide axis edge line
WpfPlot1.Plot.Axes.Bottom.FrameLineStyle.Width = 0;
WpfPlot1.Plot.Axes.Right.FrameLineStyle.Width = 0;
WpfPlot1.Plot.Axes.Top.FrameLineStyle.Width = 0;
WpfPlot1.Refresh();
DateTime 轴快速入门
轴刻度标签可以使用时间格式显示。
// plot data using DateTime units
DateTime[] dates = Generate.ConsecutiveDays(100);
double[] ys = Generate.RandomWalk(100);
WpfPlot1.Plot.Add.Scatter(dates, ys);
// tell the plot to display dates on the bottom axis
WpfPlot1.Plot.Axes.DateTimeTicksBottom();
WpfPlot1.Refresh();
DateTime 轴值
日期时间轴是使用 Microsoft 的 DateTime.ToOADate() 和 DateTime.FromOADate() 方法在日期和数值之间进行转换来实现的。希望在 DateTime 轴上显示数据的高级用户可能更喜欢使用 double 集合,而不是 DateTimes 集合。
// create an array of DateTimes one hour apart
int numberOfHours = 24;
DateTime[] dateTimes = new DateTime[numberOfHours];
DateTime startDateTime = new(2024, 1, 1);
TimeSpan deltaTimeSpan = TimeSpan.FromHours(1);
for (int i = 0; i < numberOfHours; i++)
{
dateTimes[i] = startDateTime + i * deltaTimeSpan;
}
// create an array of doubles representing the same DateTimes one hour apart
double[] dateDoubles = new double[numberOfHours];
double startDouble = startDateTime.ToOADate(); // days since 1900
double deltaDouble = 1.0 / 24.0; // an hour is 1/24 of a day
for (int i = 0; i < numberOfHours; i++)
{
dateDoubles[i] = startDouble + i * deltaDouble;
}
// now both arrays represent the same dates
WpfPlot1.Plot.Add.Scatter(dateTimes, Generate.Sin(numberOfHours));
WpfPlot1.Plot.Add.Scatter(dateDoubles, Generate.Cos(numberOfHours));
WpfPlot1.Plot.Axes.DateTimeTicksBottom();
// add padding on the right to make room for wide tick labels
WpfPlot1.Plot.Axes.Right.MinimumSize = 50;
WpfPlot1.Refresh();
自定义日期时间标签格式
用户可以提供自己的逻辑来自定义 DateTime 刻度标签
// plot sample DateTime data
DateTime[] dates = Generate.ConsecutiveDays(100);
double[] ys = Generate.RandomWalk(100);
WpfPlot1.Plot.Add.Scatter(dates, ys);
WpfPlot1.Plot.Axes.DateTimeTicksBottom();
// add logic into the RenderStarting event to customize tick labels
WpfPlot1.Plot.RenderManager.RenderStarting += (s, e) =>
{
Tick[] ticks = WpfPlot1.Plot.Axes.Bottom.TickGenerator.Ticks;
for (int i = 0; i < ticks.Length; i++)
{
DateTime dt = DateTime.FromOADate(ticks[i].Position);
string label = $"{dt:MMM} '{dt:yy}";
ticks[i] = new Tick(ticks[i].Position, label);
}
};
WpfPlot1.Refresh();
DateTime 轴固定间隔刻度
使时钟周期以固定间隔渲染。(可选)使刻度从自定义开始日期呈现,而不是使用绘图的开始日期(例如,在每小时的整点或每月的第一天绘制刻度等)。
// 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);
WpfPlot1.Plot.Add.Scatter(dates, ys);
var dtAx = WpfPlot1.Plot.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
WpfPlot1.Plot.Grid.XAxisStyle.MajorLineStyle.Color = Colors.Black.WithOpacity();
WpfPlot1.Plot.Grid.XAxisStyle.MajorLineStyle.Width = 2;
WpfPlot1.Plot.Grid.XAxisStyle.MinorLineStyle.Color = Colors.Gray.WithOpacity(0.25);
WpfPlot1.Plot.Grid.XAxisStyle.MinorLineStyle.Width = 1;
WpfPlot1.Plot.Grid.XAxisStyle.MinorLineStyle.Pattern = LinePattern.DenselyDashed;
// Remove labels on minor ticks, otherwise there is a lot of tick label overlap
WpfPlot1.Plot.RenderManager.RenderStarting += (s, e) =>
{
Tick[] ticks = WpfPlot1.Plot.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);
}
};
WpfPlot1.Refresh();
浮动轴
通过隐藏出现在绘图边缘的默认轴并创建一个新的浮动轴并将其添加到绘图中,可以实现浮动轴或居中轴。
// create floating X and Y axes using one of the existing axes for reference
ScottPlot.Plottables.FloatingAxis floatingX = new(WpfPlot1.Plot.Axes.Bottom);
ScottPlot.Plottables.FloatingAxis floatingY = new(WpfPlot1.Plot.Axes.Left);
// hide the default axes and add the custom ones to the plot
WpfPlot1.Plot.Axes.Frameless();
WpfPlot1.Plot.HideGrid();
WpfPlot1.Plot.Add.Plottable(floatingX);
WpfPlot1.Plot.Add.Plottable(floatingY);
// add sample data last so it appears on top
WpfPlot1.Plot.Add.Signal(Generate.Sin(51));
WpfPlot1.Plot.Add.Signal(Generate.Cos(51));
WpfPlot1.Refresh();
网格线样式
网格线有许多选项,允许进行广泛的自定义。
WpfPlot1.Plot.Add.Signal(Generate.Sin(51));
WpfPlot1.Plot.Add.Signal(Generate.Cos(51));
WpfPlot1.Plot.Grid.LineColor = Colors.Blue.WithAlpha(.2);
WpfPlot1.Plot.Grid.LinePattern = LinePattern.Dotted;
WpfPlot1.Refresh();
Image Axis Label
如果轴标签字体样式无法提供所需的自定义级别,则位图图像可能会显示为轴标签。此策略允许使用任何可以将该文本呈现为位图的第三方工具来实现 RTF 格式文本。它还使用户能够将图标或图像放置在其轴标签中。
WpfPlot1.Plot.Add.Signal(Generate.Sin(51));
WpfPlot1.Plot.Add.Signal(Generate.Cos(51));
// This array holds the bytes of a bitmap. Here it's generated,
// but it could be a byte array read from a bitmap file on disk.
byte[] bytes1 = SampleImages.NoisyText("Horiz", 150, 50).GetImageBytes();
byte[] bytes2 = SampleImages.NoisyText("Vert", 150, 50).GetImageBytes();
// Create a ScottPlot.Image from the bitmap bytes
ScottPlot.Image img1 = new(bytes1);
ScottPlot.Image img2 = new(bytes2);
// Display the image for the bottom axis label
WpfPlot1.Plot.Axes.Bottom.Label.Image = img1;
WpfPlot1.Plot.Axes.Left.Label.Image = img2;
WpfPlot1.Refresh();
乘数表示法
数字刻度标签可以使用乘数表示法显示(其中刻度标签使用科学记数法显示,刻度显示在绘图的角落)。帮助程序方法可用于使用单个语句设置乘数表示法,但用户可以与此方法返回的对象交互(此处未显示)或检查该方法内部的代码,以了解如何实现增强的自定义功能。
// plot sample data with extremely large values
double[] xs = Generate.RandomSample(50, -1e10, 1e10);
double[] ys = Generate.RandomSample(50, -1e20, 1e20);
WpfPlot1.Plot.Add.Scatter(xs, ys);
// enable multiplier notation on both primary axes
WpfPlot1.Plot.Axes.SetupMultiplierNotation(WpfPlot1.Plot.Axes.Left);
WpfPlot1.Plot.Axes.SetupMultiplierNotation(WpfPlot1.Plot.Axes.Bottom);
WpfPlot1.Refresh();