pub struct Plot { /* private fields */ }Expand description
A 2D plot, e.g. a graph of a function.
Plot supports multiple lines and points.
use egui_plot::{Line, Plot, PlotPoints};
let sin: PlotPoints = (0..1000).map(|i| {
let x = i as f64 * 0.01;
[x, x.sin()]
}).collect();
let line = Line::new(sin);
Plot::new("my_plot").view_aspect(2.0).show(ui, |plot_ui| plot_ui.line(line));Implementations§
Source§impl Plot
impl Plot
Sourcepub fn id(self, id: Id) -> Plot
pub fn id(self, id: Id) -> Plot
Set an explicit (global) id for the plot.
This will override the id set by Self::new.
This is the same Id that can be used for PlotMemory::load.
Sourcepub fn data_aspect(self, data_aspect: f32) -> Plot
pub fn data_aspect(self, data_aspect: f32) -> Plot
width / height ratio of the data.
For instance, it can be useful to set this to 1.0 for when the two axes show the same
unit.
By default the plot window’s aspect ratio is used.
Sourcepub fn view_aspect(self, view_aspect: f32) -> Plot
pub fn view_aspect(self, view_aspect: f32) -> Plot
width / height ratio of the plot region. By default no fixed aspect ratio is set (and width/height will fill the ui it is in).
Sourcepub fn width(self, width: f32) -> Plot
pub fn width(self, width: f32) -> Plot
Width of plot. By default a plot will fill the ui it is in.
If you set Self::view_aspect, the width can be calculated from the height.
Sourcepub fn height(self, height: f32) -> Plot
pub fn height(self, height: f32) -> Plot
Height of plot. By default a plot will fill the ui it is in.
If you set Self::view_aspect, the height can be calculated from the width.
Sourcepub fn show_x(self, show_x: bool) -> Plot
pub fn show_x(self, show_x: bool) -> Plot
Show the x-value (e.g. when hovering). Default: true.
Sourcepub fn show_y(self, show_y: bool) -> Plot
pub fn show_y(self, show_y: bool) -> Plot
Show the y-value (e.g. when hovering). Default: true.
Sourcepub fn center_x_axis(self, on: bool) -> Plot
pub fn center_x_axis(self, on: bool) -> Plot
Always keep the X-axis centered. Default: false.
Sourcepub fn center_y_axis(self, on: bool) -> Plot
pub fn center_y_axis(self, on: bool) -> Plot
Always keep the Y-axis centered. Default: false.
Sourcepub fn allow_zoom<T>(self, on: T) -> Plot
pub fn allow_zoom<T>(self, on: T) -> Plot
Whether to allow zooming in the plot. Default: true.
Note: Allowing zoom in one axis but not the other may lead to unexpected results if used in combination with data_aspect.
Sourcepub fn allow_scroll(self, on: bool) -> Plot
pub fn allow_scroll(self, on: bool) -> Plot
Whether to allow scrolling in the plot. Default: true.
Sourcepub fn allow_double_click_reset(self, on: bool) -> Plot
pub fn allow_double_click_reset(self, on: bool) -> Plot
Whether to allow double clicking to reset the view.
Default: true.
Sourcepub fn set_margin_fraction(self, margin_fraction: Vec2) -> Plot
pub fn set_margin_fraction(self, margin_fraction: Vec2) -> Plot
Set the side margin as a fraction of the plot size. Only used for auto bounds.
For instance, a value of 0.1 will add 10% space on both sides.
Sourcepub fn allow_boxed_zoom(self, on: bool) -> Plot
pub fn allow_boxed_zoom(self, on: bool) -> Plot
Whether to allow zooming in the plot by dragging out a box with the secondary mouse button.
Default: true.
Config the button pointer to use for boxed zooming. Default: Secondary
Sourcepub fn allow_drag<T>(self, on: T) -> Plot
pub fn allow_drag<T>(self, on: T) -> Plot
Whether to allow dragging in the plot to move the bounds. Default: true.
Sourcepub fn label_formatter(
self,
label_formatter: impl Fn(&str, &PlotPoint) -> String + 'static,
) -> Plot
pub fn label_formatter( self, label_formatter: impl Fn(&str, &PlotPoint) -> String + 'static, ) -> Plot
Provide a function to customize the on-hover label for the x and y axis
use egui_plot::{Line, Plot, PlotPoints};
let sin: PlotPoints = (0..1000).map(|i| {
let x = i as f64 * 0.01;
[x, x.sin()]
}).collect();
let line = Line::new(sin);
Plot::new("my_plot").view_aspect(2.0)
.label_formatter(|name, value| {
if !name.is_empty() {
format!("{}: {:.*}%", name, 1, value.y)
} else {
"".to_owned()
}
})
.show(ui, |plot_ui| plot_ui.line(line));Sourcepub fn coordinates_formatter(
self,
position: Corner,
formatter: CoordinatesFormatter,
) -> Plot
pub fn coordinates_formatter( self, position: Corner, formatter: CoordinatesFormatter, ) -> Plot
Show the pointer coordinates in the plot.
Sourcepub fn x_grid_spacer(
self,
spacer: impl Fn(GridInput) -> Vec<GridMark> + 'static,
) -> Plot
pub fn x_grid_spacer( self, spacer: impl Fn(GridInput) -> Vec<GridMark> + 'static, ) -> Plot
Configure how the grid in the background is spaced apart along the X axis.
Default is a log-10 grid, i.e. every plot unit is divided into 10 other units.
The function has this signature:
fn step_sizes(input: GridInput) -> Vec<GridMark>;This function should return all marks along the visible range of the X axis.
step_size also determines how thick/faint each line is drawn.
For example, if x = 80..=230 is visible and you want big marks at steps of
100 and small ones at 25, you can return:
vec![
// 100s
GridMark { value: 100.0, step_size: 100.0 },
GridMark { value: 200.0, step_size: 100.0 },
// 25s
GridMark { value: 125.0, step_size: 25.0 },
GridMark { value: 150.0, step_size: 25.0 },
GridMark { value: 175.0, step_size: 25.0 },
GridMark { value: 225.0, step_size: 25.0 },
];There are helpers for common cases, see log_grid_spacer and uniform_grid_spacer.
Sourcepub fn y_grid_spacer(
self,
spacer: impl Fn(GridInput) -> Vec<GridMark> + 'static,
) -> Plot
pub fn y_grid_spacer( self, spacer: impl Fn(GridInput) -> Vec<GridMark> + 'static, ) -> Plot
Default is a log-10 grid, i.e. every plot unit is divided into 10 other units.
See Self::x_grid_spacer for explanation.
Sourcepub fn grid_spacing(self, grid_spacing: impl Into<Rangef>) -> Plot
pub fn grid_spacing(self, grid_spacing: impl Into<Rangef>) -> Plot
Set when the grid starts showing.
When grid lines are closer than the given minimum, they will be hidden. When they get further apart they will fade in, until the reaches the given maximum, at which point they are fully opaque.
Sourcepub fn clamp_grid(self, clamp_grid: bool) -> Plot
pub fn clamp_grid(self, clamp_grid: bool) -> Plot
Clamp the grid to only be visible at the range of data where we have values.
Default: false.
Sourcepub fn include_x(self, x: impl Into<f64>) -> Plot
pub fn include_x(self, x: impl Into<f64>) -> Plot
Expand bounds to include the given x value.
For instance, to always show the y axis, call plot.include_x(0.0).
Sourcepub fn include_y(self, y: impl Into<f64>) -> Plot
pub fn include_y(self, y: impl Into<f64>) -> Plot
Expand bounds to include the given y value.
For instance, to always show the x axis, call plot.include_y(0.0).
Sourcepub fn auto_bounds(self, auto_bounds: Vec2b) -> Plot
pub fn auto_bounds(self, auto_bounds: Vec2b) -> Plot
Set whether the bounds should be automatically set based on data by default.
This is enabled by default.
Sourcepub fn auto_bounds_x(self) -> Plot
👎Deprecated: Use auto_bounds instead
pub fn auto_bounds_x(self) -> Plot
auto_bounds insteadExpand bounds to fit all items across the x axis, including values given by include_x.
Sourcepub fn auto_bounds_y(self) -> Plot
👎Deprecated: Use auto_bounds instead
pub fn auto_bounds_y(self) -> Plot
auto_bounds insteadExpand bounds to fit all items across the y axis, including values given by include_y.
Sourcepub fn show_background(self, show: bool) -> Plot
pub fn show_background(self, show: bool) -> Plot
Whether or not to show the background Rect.
Can be useful to disable if the plot is overlaid over existing content.
Default: true.
Sourcepub fn show_axes(self, show: impl Into<Vec2b>) -> Plot
pub fn show_axes(self, show: impl Into<Vec2b>) -> Plot
Show axis labels and grid tick values on the side of the plot.
Default: true.
Sourcepub fn show_grid(self, show: impl Into<Vec2b>) -> Plot
pub fn show_grid(self, show: impl Into<Vec2b>) -> Plot
Show a grid overlay on the plot.
Default: true.
Sourcepub fn link_axis(
self,
group_id: impl Into<Id>,
link_x: bool,
link_y: bool,
) -> Plot
pub fn link_axis( self, group_id: impl Into<Id>, link_x: bool, link_y: bool, ) -> Plot
Add this plot to an axis link group so that this plot will share the bounds with other plots in the same group. A plot cannot belong to more than one axis group.
Sourcepub fn link_cursor(
self,
group_id: impl Into<Id>,
link_x: bool,
link_y: bool,
) -> Plot
pub fn link_cursor( self, group_id: impl Into<Id>, link_x: bool, link_y: bool, ) -> Plot
Add this plot to a cursor link group so that this plot will share the cursor position with other plots in the same group. A plot cannot belong to more than one cursor group.
Sourcepub fn sharp_grid_lines(self, enabled: bool) -> Plot
pub fn sharp_grid_lines(self, enabled: bool) -> Plot
Round grid positions to full pixels to avoid aliasing. Improves plot appearance but might have an undesired effect when shifting the plot bounds. Enabled by default.
Sourcepub fn x_axis_label(self, label: impl Into<WidgetText>) -> Plot
pub fn x_axis_label(self, label: impl Into<WidgetText>) -> Plot
Set the x axis label of the main X-axis.
Default: no label.
Sourcepub fn y_axis_label(self, label: impl Into<WidgetText>) -> Plot
pub fn y_axis_label(self, label: impl Into<WidgetText>) -> Plot
Set the y axis label of the main Y-axis.
Default: no label.
Sourcepub fn x_axis_position(self, placement: VPlacement) -> Plot
pub fn x_axis_position(self, placement: VPlacement) -> Plot
Set the position of the main X-axis.
Sourcepub fn y_axis_position(self, placement: HPlacement) -> Plot
pub fn y_axis_position(self, placement: HPlacement) -> Plot
Set the position of the main Y-axis.
Sourcepub fn x_axis_formatter(
self,
fmt: impl Fn(GridMark, usize, &RangeInclusive<f64>) -> String + 'static,
) -> Plot
pub fn x_axis_formatter( self, fmt: impl Fn(GridMark, usize, &RangeInclusive<f64>) -> String + 'static, ) -> Plot
Specify custom formatter for ticks on the main X-axis.
Arguments of fmt:
- the grid mark to format
- maximum requested number of characters per tick label.
- currently shown range on this axis.
Sourcepub fn y_axis_formatter(
self,
fmt: impl Fn(GridMark, usize, &RangeInclusive<f64>) -> String + 'static,
) -> Plot
pub fn y_axis_formatter( self, fmt: impl Fn(GridMark, usize, &RangeInclusive<f64>) -> String + 'static, ) -> Plot
Specify custom formatter for ticks on the main Y-axis.
Arguments of fmt:
- the grid mark to format
- maximum requested number of characters per tick label.
- currently shown range on this axis.
Sourcepub fn y_axis_width(self, digits: usize) -> Plot
pub fn y_axis_width(self, digits: usize) -> Plot
Set the main Y-axis-width by number of digits
The default is 5 digits.
Todo: This is experimental. Changing the font size might break this.
Sourcepub fn custom_x_axes(self, hints: Vec<AxisHints>) -> Plot
pub fn custom_x_axes(self, hints: Vec<AxisHints>) -> Plot
Set custom configuration for X-axis
More than one axis may be specified. The first specified axis is considered the main axis.
Sourcepub fn custom_y_axes(self, hints: Vec<AxisHints>) -> Plot
pub fn custom_y_axes(self, hints: Vec<AxisHints>) -> Plot
Set custom configuration for left Y-axis
More than one axis may be specified. The first specified axis is considered the main axis.
Auto Trait Implementations§
impl Freeze for Plot
impl !RefUnwindSafe for Plot
impl !Send for Plot
impl !Sync for Plot
impl Unpin for Plot
impl !UnwindSafe for Plot
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<S> FromSample<S> for S
impl<S> FromSample<S> for S
fn from_sample_(s: S) -> S
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more