How To Draw On The Map In Rust
Crate plotters [−] [src]
Expand description
Plotters - A Rust cartoon library focus on data plotting for both WASM and native applications 🦀📈🚀
Plotters is drawing library designed for rendering figures, plots, and charts, in pure rust. Plotters supports diverse types of back-ends, including bitmap, vector graph, piston window, GTK/Cairo and WebAssembly.
- A new Plotters Developer'south Guide is working in progress. The preview version is available at here.
- To try Plotters with interactive Jupyter notebook, or view here for the static HTML version.
- To view the WASM example, get to this link
- Currently we have all the internal code set up for console plotting, but a panel based backend is nevertheless non prepare. See this example for how to plotting on Console with a customized backend.
- Plotters now moved all backend code to sperate repository, check FAQ list for details
Simple Nautical chart
Plotting the Console
Jupyter Support
The Matplotlib Matshow Example [code]
The 1D Gaussian Distribution [code]
The 1D Gaussian Distribution [code]
Monthly Time Coordinate [code]
Monthly Fourth dimension Coordinate [code]
Koch Snowflake Animation [code]
Drawing bitmap on chart [code]
two-Var Gussian Distribution PDF [code]
Tabular array of Contents
- Gallery
- Quick Start
- Trying with Jupyter evcxr Kernel Interactively
- Interactive Tutorial with Jupyter Notebook
- Plotting in Rust
- Plotting on HTML5 sheet with WASM Backend
- What types of figure are supported?
- Concepts by examples
- Drawing Back-ends
- Drawing Expanse
- Elements
- Composable Elements
- Chart Context
- Misc
- Development Version
- Reducing Depending Libraries && Turning Off Backends
- Listing of Features
- FAQ List
To employ Plotters, you can simply add Plotters into your Cargo.toml
[dependencies] plotters = "^0.3.ane" And the post-obit code draws a quadratic function. src/main.rs,
apply plotters::prelude::*; fn main() - > Result <(), Box < dyn std::error::Error > > { permit root = BitMapBackend::new("plotters-doc-data/0.png", (640, 480)).into_drawing_area(); root.make full(& WHITE)?; let mut chart = ChartBuilder::on(& root) .explanation("y=x^two", ("sans-serif", 50).into_font()) .margin(5) .x_label_area_size(30) .y_label_area_size(30) .build_cartesian_2d(- 1f32..1f32, - 0.1f32..1f32)?; chart.configure_mesh().draw()?; nautical chart .draw_series(LineSeries::new( (- 50..= l).map(| x | 10 as f32 / 50.0).map(| ten | (10, x * x)), & Ruby-red, ))? .characterization("y = ten^two") .fable(|(ten, y)| PathElement::new(vec![(10, y), (x + twenty, y)], & RED)); chart .configure_series_labels() .background_style(& WHITE.mix(0.8)) .border_style(& Black) .draw()?; Ok(()) }
Plotters now supports integrate with evcxr and is able to interactively drawing plots in Jupyter Notebook. The feature evcxr should exist enabled when including Plotters to Jupyter Notebook.
The following lawmaking shows a minimal example of this.
:dep plotters = { git = "https://github.com/38/plotters", default_features = imitation, features = ["evcxr"] } extern crate plotters; use plotters::prelude::*; let figure = evcxr_figure((640, 480), |root| { root.fill(&WHITE); let mut chart = ChartBuilder::on(&root) .explanation("y=10^2", ("Arial", 50).into_font()) .margin(5) .x_label_area_size(30) .y_label_area_size(xxx) .build_cartesian_2d(-1f32..1f32, -0.1f32..1f32)?; chart.configure_mesh().draw()?; chart.draw_series(LineSeries::new( (-50..=fifty).map(|ten| x equally f32 / fifty.0).map(|ten| (x, 10 * ten)), &RED, )).unwrap() .label("y = ten^2") .fable(|(x,y)| PathElement::new(vec![(ten,y), (10 + 20,y)], &RED)); nautical chart.configure_series_labels() .background_style(&WHITE.mix(0.8)) .border_style(&BLACK) .draw()?; Ok(()) }); figure
This tutorial is now working in progress and isn't complete
Thanks to the evcxr, now we have an interactive tutorial for Plotters! To apply the interactive notebook, you must accept Jupyter and evcxr installed on your computer. Follow the pedagogy on this folio beneath to install it.
Afterwards that, you should exist able to start your Jupyter server locally and load the tutorial!
git clone https://github.com/38/plotters-medico-data cd plotteres-dr.-data jupyter notebook And select the notebook called evcxr-jupyter-integration.ipynb.
Likewise, at that place'due south a static HTML version of this notebook available at the this location
Rust is a perfect language for data visualization. Although there are many mature visualization libraries in many different languages. Only Rust is one of the best languages fits the need.
-
Easy to use Rust has a very skillful iterator organization built into the standard library. With the help of iterators, Plotting in Rust can be equally like shooting fish in a barrel every bit most of the high-level programming languages. The Rust based plotting library can be very like shooting fish in a barrel to use.
-
Fast If you need rendering a effigy with trillions of data points, Rust is a good selection. Rust'due south performance allows you to combine data processing footstep and rendering step into a single application. When plotting in high-level programming languages, e.chiliad. Javascript or Python, data points must be downward-sampled before feeding into the plotting program considering of the performance considerations. Rust is fast enough to do the data processing and visualization within a single program. You can as well integrate the figure rendering code into your awarding handling a huge amount of data and visualize it in real-time.
-
WebAssembly Support Rust is ane of few the language with the best WASM support. Plotting in Rust could be very useful for visualization on a spider web page and would have a huge performance improvement comparison to Javascript.
Plotters currently supports backend that uses the HTML5 canvas. To utilize the WASM support, you lot can merely use CanvasBackend instead of other backend and all other API remains the same!
At that place's a modest demo for Plotters + WASM under examples/wasm-demo directory of this repo. To play with the deployed version, follow this link.
Plotters is not limited to any specific type of figure. You tin can create your own types of figures easily with the Plotters API.
But Plotters provides some builtin effigy types for convenience. Currently, nosotros support line series, point series, candlestick series, and histogram. And the library is designed to be able to render multiple figure into a single image. Simply Plotter is aimed to exist a platform that is fully extendable to back up any other types of figure.
Plotters tin use different cartoon dorsum-ends, including SVG, BitMap, and fifty-fifty existent-fourth dimension rendering. For instance, a bitmap drawing backend.
employ plotters::prelude::*; fn chief() - > Result <(), Box < dyn std::fault::Error > > { let mut backend = BitMapBackend::new("plotters-doc-data/1.png", (300, 200)); backend.draw_rect((50, l), (200, 150), & Cerise, true)?; Ok(()) }
Plotters uses a concept chosen drawing expanse for layout purpose. Plotters support multiple integrating into a unmarried epitome. This is washed by creating sub-drawing-areas.
Too that, the drawing area besides allows the customized coordinate organisation, past doing so, the coordinate mapping is done by the drawing area automatically.
apply plotters::prelude::*; fn principal() - > Result <(), Box < dyn std::mistake::Mistake > > { allow root_drawing_area = BitMapBackend::new("plotters-doc-information/2.png", (300, 200)).into_drawing_area(); let child_drawing_areas = root_drawing_area.split_evenly((3, 3)); for (expanse, color) in child_drawing_areas.into_iter().null(0..) { area.fill(& Palette99::pick(color))?; } Ok(()) }
In Plotters, elements are build blocks of figures. All elements are able to draw on a drawing surface area. There are different types of built-in elements, similar lines, texts, circles, etc. Yous tin can also define your own element in the application code.
You may also combine existing elements to build a circuitous chemical element.
To learn more than about the element organisation, please read the chemical element module documentation.
employ plotters::prelude::*; fn main() - > Result <(), Box < dyn std::fault::Error > > { allow root = BitMapBackend::new("plotters-dr.-data/3.png", (300, 200)).into_drawing_area(); root.fill(& WHITE)?; root.draw(& Circle::new( (100, 100), 50, Into::< ShapeStyle > ::into(& GREEN).filled(), ))?; Ok(()) }
As well the built-in elements, elements can exist composed into a logic group we called equanimous elements. When composing new elements, the upper-left corner is given in the target coordinate, and a new pixel-based coordinate which has the upper-left corner defined as (0,0) is used for further chemical element limerick purpose.
For case, we tin can accept an chemical element which includes a dot and its coordinate.
employ plotters::prelude::*; use plotters::coord::types::RangedCoordf32; fn principal() - > Event <(), Box < dyn std::error::Fault > > { let root = BitMapBackend::new("plotters-md-information/4.png", (640, 480)).into_drawing_area(); root.fill(& RGBColor(240, 200, 200))?; let root = root.apply_coord_spec(Cartesian2d::< RangedCoordf32, RangedCoordf32 > ::new( 0f32..1f32, 0f32..1f32, (0..640, 0..480), )); let dot_and_label = | x: f32, y: f32 | { return EmptyElement::at((x, y)) + Circle::new((0, 0), 3, ShapeStyle::from(& Black).filled()) + Text::new( format!("({:.ii},{:.ii})", x, y), (ten, 0), ("sans-serif", 15.0).into_font(), ); }; root.draw(& dot_and_label(0.5, 0.half-dozen))?; root.describe(& dot_and_label(0.25, 0.33))?; root.draw(& dot_and_label(0.eight, 0.8))?; Ok(()) }
In club to describe a chart, Plotters demand a information object built on peak of the drawing area called ChartContext. The nautical chart context defines fifty-fifty higher level constructs compare to the drawing area. For example, you tin define the label areas, meshes, and put a information series onto the drawing area with the assist of the chart context object.
utilize plotters::prelude::*; fn main() - > Result <(), Box < dyn std::error::Error > > { let root = BitMapBackend::new("plotters-doc-data/five.png", (640, 480)).into_drawing_area(); root.make full(& WHITE); let root = root.margin(10, 10, ten, 10); permit mut chart = ChartBuilder::on(& root) .explanation("This is our first plot", ("sans-serif", 40).into_font()) .x_label_area_size(20) .y_label_area_size(40) .build_cartesian_2d(0f32..10f32, 0f32..10f32)?; chart .configure_mesh() .x_labels(5) .y_labels(5) .y_label_formatter(& | 10 | format!("{:.iii}", x)) .draw()?; nautical chart.draw_series(LineSeries::new( vec![(0.0, 0.0), (5.0, five.0), (viii.0, 7.0)], & Reddish, ))?; chart.draw_series(PointSeries::of_element( vec![(0.0, 0.0), (5.0, 5.0), (viii.0, vii.0)], 5, & Blood-red, & | c, due south, st | { return EmptyElement::at(c) + Circle::new((0,0),s,st.filled()) + Text::new(format!("{:?}", c), (x, 0), ("sans-serif", 10).into_font()); }, ))?; Ok(()) }
To use the latest development version, pull https://github.com/38/plotters.git. In Cargo.toml
[dependencies] plotters = { git = "https://github.com/38/plotters.git" } Plotters now supports utilize features to control the backend dependencies. Past default, BitMapBackend and SVGBackend are supported, utilize default_features = false in the dependency description in Cargo.toml and you lot tin can blood-red-pick the backend implementations.
-
svgEnable theSVGBackend -
bitmapEnable theBitMapBackend
For example, the following dependency clarification would avoid compiling with bitmap support:
[dependencies] plotters = { git = "https://github.com/38/plotters.git", default_features = simulated, features = ["svg"] } The library also allows consumers to brand apply of the Palette crate's color types past default. This behavior can also be turned off by setting default_features = false.
This is the full list of features that is defined by Plotters crate. Utilise default_features = false to disable those default enabled features, and so you should be able to cherry-choice what features yous want to include into Plotters crate. By doing so, y'all tin can minimize the number of dependencies down to only itertools and compile time is less than 6s.
The following listing is a complete list of features that can exist opt in and out.
- Tier 1 cartoon backends
| Proper name | Description | Additional Dependency | Default? |
|---|---|---|---|
| bitmap_encoder | Permit BitMapBackend save the result to bitmap files | paradigm, rusttype, font-kit | Yes |
| svg_backend | Enable SVGBackend Support | None | Yep |
| bitmap_gif | Opt-in GIF animation Rendering support for BitMapBackend, implies bitmap enabled | gif | Yes |
- Font manipulation features
| Proper noun | Description | Additional Dependency | Default? |
|---|---|---|---|
| ttf | Allows TrueType font back up | rusttype, font-kit | Yes |
- Coordinate features
| Proper noun | Description | Boosted Dependency | Default? |
|---|---|---|---|
| datetime | Eanble the date and time coordinate support | chrono | Yes |
- Chemical element, series and util functions
| Name | Description | Boosted Dependency | Default? |
|---|---|---|---|
| errorbar | The errorbar element support | None | Yes |
| candlestick | The candlestick element support | None | Yep |
| boxplot | The boxplot chemical element support | None | Yes |
| area_series | The area series back up | None | Yes |
| line_series | The line series support | None | Yes |
| histogram | The histogram serial support | None | Yep |
| point_series | The point series support | None | Yes |
- Misc
| Name | Clarification | Additional Dependency | Default? |
|---|---|---|---|
| deprecated_items | This feature allows use of deprecated items which is going to be removed in the future | None | Yes |
| debug | Enable the lawmaking used for debugging | None | No |
-
Why does the WASM example break on my car ?
The WASM example requires using
wasm32target to build. Usingcargo buildis likely to apply the default target which in about of the example is any of the x86 target. Thus you demand add--target=wasm32-unknown-unknownin the cargo parameter list to build it. -
How to draw text/circle/point/rectangle/… on the top of chart ?
As you may realized, Plotters is a cartoon library rather than a traditional data plotting library, you have the freedom to depict anything you want on the drawing area. Use
DrawingArea::depictto depict any element on the drawing surface area. -
Where can I find the backend lawmaking ?
Since Plotters 0.iii, all drawing backends are independent crate from the main Plotters crate. Utilize the following link to detect the backend code:
- Bitmap Backend
- SVG Backend
- HTML5 Canvas Backend
- GTK/Cairo Backend
| chart | The loftier-level plotting abstractions. |
| coord | Ane of the key features of Plotters is flexible coordinate arrangement abstraction and this module provides all the brainchild used for the coordinate abstarction of Plotters. |
| information | The information processing module, which implements algorithms related to visualization of data. Such as, downward-sampling, etc. |
| drawing | The drawing utils for Plotters. In Plotters, nosotros have 2 set of drawing APIs: low-level API and high-level API. |
| element | Defines the drawing elements, the high-level drawing unit in Plotters drawing system |
| prelude | The module imports the near commonly used types and modules in Plotters |
| series | This module contains predefined types of serial. The series in Plotters is actually an iterator of elements, which can be taken by |
| style | The style for shapes and text, font, color, etc. |
Source: https://docs.rs/plotters
Posted by: stewartdold1979.blogspot.com

0 Response to "How To Draw On The Map In Rust"
Post a Comment