先进的Axis功能
 

高级网格定制

网格线可以使用各种配置方法进行广泛定制。

var plt = new ScottPlot.Plot(600, 400);

// plot sample data
plt.AddSignal(DataGen.Sin(51));
plt.AddSignal(DataGen.Cos(51));

// advanced grid customizations are available by accessing Axes directly
plt.XAxis.MajorGrid(color: Color.FromArgb(100, Color.Black));
plt.XAxis.MinorGrid(enable: true, color: Color.FromArgb(20, Color.Black));
plt.YAxis.MajorGrid(lineWidth: 2, lineStyle: LineStyle.Dash, color: Color.Magenta);

plt.SaveFig("asis_gridAdvanced.png");
 

数字格式字符串

可以使用自定义格式字符串将记号标签转换为文本。

var plt = new ScottPlot.Plot(600, 400);

// plot sample data
plt.AddSignal(DataGen.Sin(51));
plt.AddSignal(DataGen.Cos(51));

// See https://tinyurl.com/y86clj9k to learn about numeric format strings
plt.XAxis.TickLabelFormat("E2", dateTimeFormat: false);
plt.YAxis.TickLabelFormat("P1", dateTimeFormat: false);

plt.SaveFig("ticks_numericFormatString.png");
 

手动勾号标签

勾选位置和标签可以手动定义。

var plt = new ScottPlot.Plot(600, 400);

// plot sample data
plt.AddSignal(DataGen.Sin(51));
plt.AddSignal(DataGen.Cos(51));

// manually define X axis tick positions and labels
double[] xPositions = { 7, 21, 37, 46 };
string[] xLabels = { "VII", "XXI", "XXXVII", "XLVI" };
plt.XAxis.ManualTickPositions(xPositions, xLabels);

// manually define Y axis tick positions and labels
double[] yPositions = { -1, 0, .5, 1 };
string[] yLabels = { "bottom", "center", "half", "top" };
plt.YAxis.ManualTickPositions(yPositions, yLabels);

plt.SaveFig("ticks_defined.png");
 

非线性刻度间距

在常规笛卡尔空间上绘制数据,然后手动控制轴标签,以显示点之间的非线性间距。

var plt = new ScottPlot.Plot(600, 400);

// these are our nonlinear data values we wish to plot
double[] amplitudes = { 23.9, 24.2, 24.3, 24.5, 25.3, 26.3, 27.6, 31.4, 33.7, 36,
38.4, 42, 43.5, 46.1, 48.8, 51.5, 53.2, 55, 56.9, 58.7, 60.6 };
double[] frequencies = { 50, 63, 80, 100, 125, 160, 200, 250, 315, 400, 500, 630,
 800, 1000, 1250, 1600, 2000, 2500, 3150, 4000, 5000 };

// ignore the "real" X values and plot data at consecutive X values (0, 1, 2, 3...)
double[] positions = DataGen.Consecutive(frequencies.Length);
plt.AddScatter(positions, amplitudes);

// then define tick labels based on "real" X values, rotate them, and give them extra space
string[] labels = frequencies.Select(x => x.ToString()).ToArray();
plt.XAxis.ManualTickPositions(positions, labels);
plt.XAxis.TickLabelStyle(rotation: 45);
plt.XAxis.SetSizeLimit(min: 50); // extra space for rotated ticks

// apply axis labels, trigging a layout reset
plt.Title("Vibrational Coupling");
plt.YLabel("Amplitude (dB)");
plt.XLabel("Frequency (Hz)");

plt.SaveFig("ticks_nonLinearX.png");
 

下降的蜱虫

ScottPlot将始终显示X值从左向右递增的数据。要模拟反转轴(数字从左向右递减),请在负空间中绘制数据,然后反转记号标签的符号。

var plt = new ScottPlot.Plot(600, 400);

// plot the positive data in the negative space
double[] values = DataGen.Sin(50);
var sig = plt.AddSignal(values);
sig.OffsetX = -50;

// then invert the sign of the axis tick labels
plt.XAxis.TickLabelNotation(invertSign: true);
plt.YAxis.TickLabelNotation(invertSign: true);

plt.SaveFig("ticks_descending.png");
 

定义的刻度间距

勾号之间的间距可以通过设置栅格间距手动定义。

var plt = new ScottPlot.Plot(600, 400);

// plot the positive data in the negative space
double[] values = DataGen.Sin(50);
var sig = plt.AddSignal(values);
sig.OffsetX = -50;

// then invert the sign of the axis tick labels
plt.XAxis.ManualTickSpacing(2);
plt.YAxis.ManualTickSpacing(.1);

plt.SaveFig("ticks_definedSpacing.png");
 

滴答标签文化

对于不同的文化,大量的数字和日期的格式是不同的。Hungarian使用空格分隔大数字,使用句点分隔日期中的字段。

var plt = new ScottPlot.Plot(600, 400);

// generate some data
double[] price = DataGen.RandomWalk(null, 60 * 8, 10000);
DateTime start = new DateTime(2019, 08, 25, 8, 30, 00);
double pointsPerDay = 24 * 60;

// create the plot
var sig = plt.AddSignal(price, pointsPerDay);
sig.OffsetX = start.ToOADate();

// set the localization
var culture = System.Globalization.CultureInfo.CreateSpecificCulture("hu"); // Hungarian
plt.SetCulture(culture);

// further decorate the plot
plt.XAxis.DateTimeFormat(true);
plt.YAxis.Label("Price");
plt.XAxis.Label("Date and Time");
plt.XAxis2.Label("Hungarian Formatted DateTime Tick Labels");

plt.SaveFig("ticks_culture.png");
 

自定义标记文化

SetCulture()作为参数,允许用户手动定义格式化字符串,这些字符串将全局用于更改数字和日期的格式化方式。

var plt = new ScottPlot.Plot(600, 400);

// generate 10 days of data
int pointCount = 10;
double[] values = DataGen.RandomWalk(null, pointCount);
double[] days = new double[pointCount];
DateTime day1 = new DateTime(1985, 09, 24);
for (int i = 0; i < days.Length; i++)
    days[i] = day1.AddDays(1).AddDays(i).ToOADate();

// plot the data with custom tick format (https://tinyurl.com/ycwh45af)
plt.AddScatter(days, values);
plt.XAxis.TickLabelFormat("M\\/dd", dateTimeFormat: true);

plt.SaveFig("ticks_cultureCustom.png");
 

乘法器表示法

当绘制大数据值时,乘数表示法使刻度标签保持较小。这种风格也被称为工程符号或科学符号。

var plt = new ScottPlot.Plot(600, 400);

plt.AddLine(-1e5, -1e10, 1e5, 1e10);

plt.XAxis.TickLabelNotation(multiplier: true);
plt.YAxis.TickLabelNotation(multiplier: true);

plt.SaveFig("ticks_multiplier.png");
 

偏移符号

当绘制紧密相连的大数据值时,偏移符号使记号标签保持较小。

var plt = new ScottPlot.Plot(600, 400);

plt.AddLine(1e5 + 111, 1e10 + 111, 1e5 + 222, 1e10 + 222);

plt.XAxis.TickLabelNotation(offset: true);
plt.YAxis.TickLabelNotation(offset: true);

plt.SaveFig("ticks_offset.png");
 

定义的日期时间间隔

此示例演示如何对日期时间轴使用固定的间隔距离

var plt = new ScottPlot.Plot(600, 400);

// create a series of dates
int pointCount = 20;
double[] dates = new double[pointCount];
var firstDay = new DateTime(2020, 1, 22);
for (int i = 0; i < pointCount; i++)
    dates[i] = firstDay.AddDays(i).ToOADate();

// simulate data for each date
double[] values = new double[pointCount];
Random rand = new Random(0);
for (int i = 1; i < pointCount; i++)
    values[i] = values[i - 1] + rand.NextDouble();

plt.AddScatter(dates, values);
plt.XAxis.DateTimeFormat(true);

// define tick spacing as 1 day (every day will be shown)
plt.XAxis.ManualTickSpacing(1, ScottPlot.Ticks.DateTimeUnit.Day);
plt.XAxis.TickLabelStyle(rotation: 45);

// add some extra space for rotated ticks
plt.XAxis.SetSizeLimit(min: 50);

plt.SaveFig("ticks_definedDateTimeSpace.png");
 

对数刻度

ScottPlot设计用于在线性X轴和Y轴上显示二维数据,但您可以在打印之前记录变换数据,并自定义刻度和网格,以显示对数刻度的外观。

var plt = new ScottPlot.Plot(600, 400);

// These are the dat we will plot with a linear X scale but log Y scale
double[] xs = { 1, 2, 3, 4, 5 };
double[] ys = { 10, 2_000, 50_000, 1_000_000, 1_500_000 };

// Plot the Log10 of all the Y values
double[] logYs = ys.Select(y => Math.Log10(y)).ToArray();
var scatter = plt.AddScatter(xs, logYs, lineWidth: 2, markerSize: 10);

// Use a custom formatter to control the label for each tick mark
static string logTickLabels(double y) => Math.Pow(10, y).ToString("N0");
plt.YAxis.TickLabelFormat(logTickLabels);

// Use log-spaced minor tick marks and grid lines to make it more convincing
plt.YAxis.MinorLogScale(true);
plt.YAxis.MajorGrid(true, Color.FromArgb(80, Color.Black));
plt.YAxis.MinorGrid(true, Color.FromArgb(20, Color.Black));
plt.XAxis.MajorGrid(true, Color.FromArgb(80, Color.Black));

// Set the axis limits manually to ensure edges terminate at desirable locations
plt.SetAxisLimits(0, 6, 0, Math.Log10(10_000_000));

plt.SaveFig("asis_log.png");
 

对数刻度蜱虫密度

可以自定义主刻度之间的小刻度数。

var plt = new ScottPlot.Plot(600, 400);

double[] ys = ScottPlot.DataGen.Range(100, 10_000, 100, true);
double[] xs = ScottPlot.DataGen.Consecutive(ys.Length);
double[] logYs = ys.Select(y => Math.Log10(y)).ToArray();

var scatter = plt.AddScatter(xs, logYs);

static string logTickLabels(double y) => Math.Pow(10, y).ToString("N0");
plt.YAxis.TickLabelFormat(logTickLabels);

// set the number of minor ticks per major tick here
plt.YAxis.MinorLogScale(true, minorTickCount: 20);

// darken grid line colors
plt.YAxis.MinorGrid(true);
plt.YAxis.MinorGrid(true, Color.FromArgb(20, Color.Black));
plt.YAxis.MajorGrid(true, Color.FromArgb(80, Color.Black));
plt.XAxis.MajorGrid(true, Color.FromArgb(80, Color.Black));

plt.SaveFig("asis_logTickDensity.png");
 

标尺模式

标尺模式是显示轴刻度的另一种方式。它绘制长记号并偏移记号标签,以显示标尺的外观。

var plt = new ScottPlot.Plot(600, 400);

plt.AddSignal(DataGen.Sin(51));
plt.AddSignal(DataGen.Cos(51));

plt.XAxis.RulerMode(true);
plt.YAxis.RulerMode(true);

plt.SaveFig("asis_ruler.png");
 

极坐标

辅助函数将半径和θ数组转换为笛卡尔坐标,适用于使用传统打印类型打印。

var plt = new ScottPlot.Plot(600, 400);

// create data with polar coordinates
int count = 400;
double step = 0.01;

double[] rs = new double[count];
double[] thetas = new double[count];

for (int i = 0; i < rs.Length; i++)
{
    rs[i] = 1 + i * step;
    thetas[i] = i * 2 * Math.PI * step;
}

// convert polar data to Cartesian data
(double[] xs, double[] ys) = ScottPlot.Tools.ConvertPolarCoordinates(rs, thetas);

// plot the Cartesian data
plt.AddScatter(xs, ys);

// decorate the plot
plt.Title("Scatter Plot of Polar Data");
plt.AxisScaleLock(true);

plt.SaveFig("asis_polar.png");
 

图像作为轴标签

图像可以用作轴标签,以允许像LaTeX轴标签这样的东西。

var plt = new ScottPlot.Plot(600, 400);

// create an interesting plot
double[] xs = DataGen.Range(-5, 5, .5);
double[] ys = DataGen.Range(-5, 5, .5);
Vector2[,] vectors = new Vector2[xs.Length, ys.Length];
for (int i = 0; i < xs.Length; i++)
    for (int j = 0; j < ys.Length; j++)
        vectors[i, j] = new Vector2(ys[j], -15 * Math.Sin(xs[i]));
plt.AddVectorField(vectors, xs, ys, colormap: Drawing.Colormap.Turbo);

// use images as axis labels
plt.XAxis.ImageLabel(new Bitmap("Images/theta.png"));
plt.YAxis.ImageLabel(new Bitmap("Images/d_theta_dt.png"));

plt.SaveFig("asis_image.png");
 

透明图像轴标签

PNG中的透明度受到尊重,但JPEG文件不支持透明度。

var plt = new ScottPlot.Plot(600, 400);

plt.Style(Style.Light2);
plt.AddSignal(DataGen.Sin(51));
plt.AddSignal(DataGen.Cos(51));

// vertical axis label uses a transparent PNG
plt.YAxis.ImageLabel(new Bitmap("Images/d_theta_dt.png"));

// horizontal axis label uses a non-transparent JPEG
plt.XAxis.ImageLabel(new Bitmap("Images/theta.jpg"));

plt.SaveFig("asis_imageTransparent.png");
 

蜱虫密度

轴刻度密度可由用户调整。密度越大,显示的滴答声就越多。将该值设置得太高将导致勾号标签重叠。

var plt = new ScottPlot.Plot(600, 400);

plt.AddSignal(DataGen.Sin(51));
plt.AddSignal(DataGen.Cos(51));

plt.XAxis.Label("Lower Density Ticks");
plt.XAxis.TickDensity(0.2);

plt.YAxis.Label("Higher Density Ticks");
plt.YAxis.TickDensity(3);

plt.SaveFig("asis_tickDensity.png");
 

最小刻度间距

可以定义最小刻度间距,以便放大时不会产生超出定义限制的更多网格线、刻度和刻度标签。

var plt = new ScottPlot.Plot(600, 400);

plt.AddSignal(DataGen.Sin(51));
plt.AddSignal(DataGen.Cos(51));

plt.YAxis.MinimumTickSpacing(1);
plt.XAxis.MinimumTickSpacing(25);

plt.SaveFig("asis_minimumTickSpacing.png");
 

自定义记号格式化程序

对于记号标签格式的最终控制,您可以创建一个自定义格式化程序函数,并使用该函数将位置转换为标签。这允许使用逻辑来设置勾号标签的格式。

var plt = new ScottPlot.Plot(600, 400);

plt.AddSignal(ScottPlot.DataGen.Sin(51));
plt.AddSignal(ScottPlot.DataGen.Cos(51));

// create a custom formatter as a static class
static string customTickFormatter(double position)
{
    if (position == 0)
        return "zero";
    else if (position > 0)
        return $"+{position:F2}";
    else
        return $"({Math.Abs(position):F2})";
}

// use the custom formatter for horizontal and vertical tick labels
plt.XAxis.TickLabelFormat(customTickFormatter);
plt.YAxis.TickLabelFormat(customTickFormatter);

plt.SaveFig("asis_custom_tick_formatter.png");
 

反转记号标记方向

记号可以向外(默认)或反转,以显示为相对于绘图区域边缘的向内线。

var plt = new ScottPlot.Plot(600, 400);

plt.AddSignal(DataGen.Sin(51));
plt.AddSignal(DataGen.Cos(51));

plt.XAxis.TickMarkDirection(outward: false);
plt.YAxis.TickMarkDirection(outward: false);

plt.SaveFig("ticks_invert_tick_mark_direction.png");