Axes and Axis Limits

contenido

ScottPlot 5 has improved performance, new features, and better cross-platform support, but its API is not identical to ScottPlot 4. See the What's New in ScottPlot 5.0 page for additional information.

This page has not yet been updated to reflect ScottPlot 5.0 (the latest version of ScottPlot). Information about ScottPlot 5 features may be present in the ScottPlot 5.0 Cookbook or demonstrated (with source code) in the ScottPlot 5.0 Demo application.

ScottPlot displays data in a rectangular view of a 2D coordinate system. The edges of this view are referred to as Axis Limits, and this page discusses the various ways axis limits can be adjusted and customized.

  • An axis displays information on one edge of a plot
  • The axis label is the title displayed on an axis
  • Tick marks are small lines drawn on the figure outside the data area
  • Tick labels can be displayed for each tick mark
  • Grid lines are lines at each tick mark drawn on the data area
  • Axis lines are single lines drawn on the edge of the axis. Axis lines of the four primary axes form a rectangle that outlines the data area.
  • Axes have major ticks and minor ticks. The default behavior is that both display axis lines and tick marks, but only major ticks display tick labels and grid lines.

  • Plots have 4 axes by default (2 primary, 2 secondary)
  • Primary axes on the bottom (Plot.XAxis) and left (Plot.YAxis) are fully visible by default
  • Secondary axes on the top (Plot.XAxis2) and right (Plot.YAxis2) only display axis lines by default
  • The Title of a plot is just the axis label of the top axis (Plot.XAxis2)
  • Additional axes can be added with Plot.AddAxis()

The plot area can be defined manually by calling SetAxisLimits() and passing in the edges of the rectangle you wish to view.

// show a plot from -10 to +10 on X axis and -20 to +20 on Y axis
myPlot.SetAxisLimits(-10, 10, -20, 20);

The AxisAuto() command will evaluate every plotted object to determine the limits of the data it contains, then adjust the plot as needed to ensure all data is visible. Optional arguments allow the user to adjust how much extra padding to include around the periphery of the data.

// resize the plot view to accommodate the data
myPlot.AxisAuto();

It may be helpful to reference the current axis limits so they can be saved, used for calculations in another part of an application, or applied to another plot. The GetAxisLimits() function returns an object which contains properties describing the plot’s axis limits when the function was called.

// read current limits
var limits = myPlot.GetAxisLimits();
// properties hold axis view information
Console.WriteLine($"X goes from {limits.XMin} to {limits.XMax}");
// apply those limits to a different plot
myOtherPlot.SetAxisLimits(limits);

Sometimes you want the user to be able to zoom in and out, but not farther than a range you control. The boundaries of the axis limits can be set using these functions:

// disable zooming out farther than 0 to +50 horizontally or -1 to +1 vertically
formsPlot1.Plot.XAxis.SetBoundary(0, 50);
formsPlot1.Plot.YAxis.SetBoundary(-1, 1);
// disable zooming in closer than 10 to 20 horizontally or 0.1 to 0.2 vertically
formsPlot1.Plot.XAxis.SetInnerBoundary(10, 20);
formsPlot1.Plot.YAxis.SetInnerBoundary(0.1, 0.2);

This behavior can be controlled at the user control level. See the User Control Configuration Object page for more details.

// disable left-click-drag pan
formsPlot1.Configuration.Pan = false;
// disable right-click-drag zoom
formsPlot1.Configuration.Zoom = false;
// disable scroll wheel zoom
formsPlot1.Configuration.ScrollWheelZoom = false;
// disable middle-click-drag zoom window
formsPlot1.Configuration.MiddleClickDragZoom = false;

It is possible to have multiple X or Y axes using ScottPlot, but the functions discussed above all work on the primary X and Y axis by default. All the advice above applies to multi-axis plots, but each of the functions discussed has optional “axis index” arguments which must be defined. For example:

plt.SetAxisLimits(yMin: -1, yMax: 1, yAxisIndex: 0); // primary Y axis
plt.SetAxisLimits(yMin: -5, yMax: 5, yAxisIndex: 1); // secondary Y axis

See the ScottPlot Cookbook and Multi-Axis FAQ page for additional example.

Resumir
ScottPlot 5 introduces enhanced performance, new features, and improved cross-platform support, although its API differs from ScottPlot 4. The article outlines the functionalities related to axis limits in ScottPlot, which is used to display data in a 2D coordinate system. Key components include axes, axis labels, tick marks, and grid lines. By default, plots have four axes, with options to add more. Users can manually set axis limits using the `SetAxisLimits()` function or automatically adjust them with `AxisAuto()`, which evaluates plotted data to ensure visibility. The current axis limits can be retrieved using `GetAxisLimits()`, allowing for easy reference and application to other plots. Additionally, boundaries can be set to control zooming and panning behavior, ensuring users do not exceed specified ranges. The article also mentions that while multiple axes can be used, the discussed functions primarily target the primary axes unless specified otherwise. For further details, users are encouraged to refer to the ScottPlot 5.0 Cookbook and Demo applications.