pub struct Plot {
pub width: i64,
pub height: i64,
pub frame: PlotFrame,
/* private fields */
}
Expand description
Struct used to plot data and generate images.
A plot represents a single drawing area. The region of the image that data
is plotted to is refered to as the canvas
, which by default, is inset
within the full image. The size of the canvas can be changed with
with_drawing_bounds
.
A plot has two notions of coordinates: “logical” coordinates which are real-
valued coordinates that are plotted and “physical” coordinates which are the
integer locations of actual pixels. The range of logical coordinates
displayed can be changed with with_plotting_range
.
Currently data can only be plotted with the low level primitives
Circle
and Line
. This makes
plotting somewhat cumbersome, but it also makes this system flexible.
§Examples
Basic sine wave without axis labels
let mut p = Plot::new(300, 200)
.with_canvas_color(GREY)
.with_bg_color(BLUE)
.with_plotting_range(0.0..(2.0 * 3.14), -1.1..1.1)
.with_drawing_bounds(0.05..0.95, 0.1..0.95);
let x_values: Vec<f64> = (0..1000).map(|i| (i as f64) / 1000.0 * 2.0 * 3.14).collect();
let points: Vec<Point> = x_values.into_iter().map(|x| {
(x, x.sin()).into()
}).collect();
let circle = Circle::new(RED, 1);
p.draw_circles(&circle, &points);
p.save("sine-wave.bmp").unwrap();
Fields§
§width: i64
The width of the output image.
height: i64
The width of the output image.
frame: PlotFrame
Internal only: TODO: Make private
Implementations§
Source§impl Plot
impl Plot
Sourcepub fn new(width: u32, height: u32) -> Plot
pub fn new(width: u32, height: u32) -> Plot
Creates a new plot with the given image dimensions.
Sourcepub fn draw_line<P>(&mut self, point1: P, point2: P, color: Color)
pub fn draw_line<P>(&mut self, point1: P, point2: P, color: Color)
Draws a line segment with the given logical coordinate endpoints.
Sourcepub fn draw_pixel_line(&mut self, p1: (f64, f64), p2: (f64, f64), color: Color)
pub fn draw_pixel_line(&mut self, p1: (f64, f64), p2: (f64, f64), color: Color)
Draws a line segment with the given physical pixel endpoints.
Sourcepub fn draw_orientations(
&mut self,
orientation: &Orientation,
positions: &[Point],
angles: &[f64],
)
pub fn draw_orientations( &mut self, orientation: &Orientation, positions: &[Point], angles: &[f64], )
Plots data as line segments with the given logical positions and angles.
Sourcepub fn draw_circles(&mut self, circle: &Circle, positions: &[Point])
pub fn draw_circles(&mut self, circle: &Circle, positions: &[Point])
Plots data as circles with the given logical positions.
Sourcepub fn save(&self, filename: &str) -> Result<(), ImageError>
pub fn save(&self, filename: &str) -> Result<(), ImageError>
Saves contents of plot as an image.
Sourcepub fn put_pixel_safe(&mut self, x: i64, y: i64, c: Color)
pub fn put_pixel_safe(&mut self, x: i64, y: i64, c: Color)
Sets a pixel value, respecting bounds of plotting frame.
Sourcepub fn with_drawing_bounds<R: Into<Range<f64>>>(
self,
x_bounds: R,
y_bounds: R,
) -> Self
pub fn with_drawing_bounds<R: Into<Range<f64>>>( self, x_bounds: R, y_bounds: R, ) -> Self
Changes the fraction of the plot image that the plotting canvas takes up.
This accepts ranges of fractions between 0.0 and 1.0 indicating the portion of the plot that the plottable area takes up. All plotted data is displayed in this area. A bordering frame is drawn around the plotting canvas.
Sourcepub fn with_plotting_range<R: Into<Range<f64>>>(
self,
x_range: R,
y_range: R,
) -> Self
pub fn with_plotting_range<R: Into<Range<f64>>>( self, x_range: R, y_range: R, ) -> Self
Changes the logical coordinate range displayed in the plot.
For example, (0.0, 2.0 * PI), (-1.0, 1.0)
would correspond to displaying
a single period of a standard sine function.
Sourcepub fn with_bg_color(self, color: Color) -> Self
pub fn with_bg_color(self, color: Color) -> Self
Changes the background color of the plot image (behind the canvas).
Sourcepub fn with_canvas_color(self, color: Color) -> Self
pub fn with_canvas_color(self, color: Color) -> Self
Changes the background color of the plotting canvas.
Sourcepub fn with_frame_color(self, color: Option<Color>) -> Self
pub fn with_frame_color(self, color: Option<Color>) -> Self
Changes the color of the border around the plotting canvas.