ProductPromotion
Logo

Rust

made by https://0x3d.site

GitHub - plotly/plotly.rs: Plotly for Rust
Plotly for Rust. Contribute to plotly/plotly.rs development by creating an account on GitHub.
Visit Site

GitHub - plotly/plotly.rs: Plotly for Rust

GitHub - plotly/plotly.rs: Plotly for Rust

Table of Contents

Introduction

A plotting library for Rust powered by Plotly.js.

Documentation and numerous interactive examples are available in the Plotly.rs Book, the examples/ directory and docs.rs.

For changes since the last version, please consult the changelog.

Basic Usage

Add this to your Cargo.toml:

[dependencies]
plotly = "0.10.0"

Exporting an Interactive Plot

Any figure can be saved as an HTML file using the Plot.write_html() method. These HTML files can be opened in any web browser to access the fully interactive figure.

use plotly::{Plot, Scatter};

let mut plot = Plot::new();
let trace = Scatter::new(vec![0, 1, 2], vec![2, 1, 0]);
plot.add_trace(trace);

plot.write_html("out.html");

By default, the Plotly JavaScript library will be included via CDN, which results in a smaller filesize, but slightly slower first load as the JavaScript library has to be downloaded first. To instead embed the JavaScript library (several megabytes in size) directly into the HTML file, the library must be compiled with the feature flag plotly_embed_js. Once enabled, by default the JavaScript library is directly embedded in the generated HTML file. It is still possible to use the CDN version, by using the use_cdn_plotly method.

// <-- Create a `Plot` -->

plot.use_cdn_plotly();
plot.write_html("out.html");

If you only want to view the plot in the browser quickly, use the Plot.show() method.

// <-- Create a `Plot` -->

plot.show(); // The default web browser will open, displaying an interactive plot

Exporting a Static Image

To save a plot as a static image, the kaleido feature is required:

# Cargo.toml

[dependencies]
plotly = { version = "0.10.0", features = ["kaleido"] }

With this feature enabled, plots can be saved as any of png, jpeg, webp, svg, pdf and eps. Note that the plot will be a static image, i.e. they will be non-interactive.

The Kaleido binary is downloaded for your system's architecture at compile time from the official Kaleido release page. This library currently supports x86_64 on Linux and Windows, and both x86_64 and aarch64 on macOS.

Exporting a simple plot looks like this:

use plotly::{ImageFormat, Plot};

let mut plot = Plot::new();
let trace = Scatter::new(vec![0, 1, 2], vec![2, 1, 0]);
plot.add_trace(trace);

plot.write_image("out.png", ImageFormat::PNG, 800, 600, 1.0);

Usage Within a Wasm Environment

Using Plotly.rs in a Wasm-based frontend framework is possible by enabling the wasm feature:

# Cargo.toml

[dependencies]
plotly = { version = "0.10.0", features = ["wasm"] }

First, make sure that you have the Plotly JavaScript library in your base HTML template:

 <!-- index.html -->

<!doctype html>
<html lang="en">
    <head>
        <!-- snip -->
        <script src="https://cdn.plot.ly/plotly-2.14.0.min.js"></script>
    </head>
    <!-- snip -->
</html>

A simple Plot component would look as follows, using Yew as an example frontend framework:

use plotly::{Plot, Scatter};
use yew::prelude::*;


#[function_component(PlotComponent)]
pub fn plot_component() -> Html {
    let p = yew_hooks::use_async::<_, _, ()>({
        let id = "plot-div";
        let mut plot = Plot::new();
        let trace = Scatter::new(vec![0, 1, 2], vec![2, 1, 0]);
        plot.add_trace(trace);

        async move {
            plotly::bindings::new_plot(id, &plot).await;
            Ok(())
        }
    });


        use_effect_with_deps(move |_| {
            p.run();
            || ()
        }, (),
    );


    html! {
        <div id="plot-div"></div>
    }
}

More detailed standalone examples can be found in the examples/ directory.

Crate Feature Flags

The following feature flags are available:

kaleido

Adds plot save functionality to the following formats: png, jpeg, webp, svg, pdf and eps.

plotly_image

Adds trait implementations so that image::RgbImage and image::RgbaImage can be used more directly with the plotly::Image trace.

plotly_ndarray

Adds support for creating plots directly using ndarray types.

plotly_embed_js

By default, the CDN version of plotly.js is used in the library and in the generated HTML files. This feature can be used to opt in for embedding plotly.min.js in the generated HTML files. The benefit is that the plot will load faster in the browser.

However, there are two downsides of using this feature flag, one is that the resulting html will be much larger, as a copy of the plotly.min.js library is embedded in each HTML file. The second, more relevant, is that a copy of the plotly.min.js library needs to be compiled in the plotly-rs library itself which increases the size by approx 3.5 Mb.

When the feature is enabled, users can still opt in for the CDN version by using the method use_cdn_plotly.

Note that when using Plot::to_inline_html(), it is assumed that the plotly.js library is already in scope within the HTML file, so enabling this feature flag will have no effect.

wasm

Enables compilation for the wasm32-unknown-unknown target and provides access to a bindings module containing wrappers around functions exported by the plotly.js library.

Contributing

  • If you've spotted a bug or would like to see a new feature, please submit an issue on the issue tracker.

  • Pull requests are welcome, see the contributing guide for more information.

License

Plotly.rs is distributed under the terms of the MIT license.

See LICENSE-MIT, and COPYRIGHT for details.

More Resources
to explore the angular.

mail [email protected] to add your project or resources here 🔥.

Related Articles
to learn about angular.

FAQ's
to learn more about Angular JS.

mail [email protected] to add more queries here 🔍.

More Sites
to check out once you're finished browsing here.

0x3d
https://www.0x3d.site/
0x3d is designed for aggregating information.
NodeJS
https://nodejs.0x3d.site/
NodeJS Online Directory
Cross Platform
https://cross-platform.0x3d.site/
Cross Platform Online Directory
Open Source
https://open-source.0x3d.site/
Open Source Online Directory
Analytics
https://analytics.0x3d.site/
Analytics Online Directory
JavaScript
https://javascript.0x3d.site/
JavaScript Online Directory
GoLang
https://golang.0x3d.site/
GoLang Online Directory
Python
https://python.0x3d.site/
Python Online Directory
Swift
https://swift.0x3d.site/
Swift Online Directory
Rust
https://rust.0x3d.site/
Rust Online Directory
Scala
https://scala.0x3d.site/
Scala Online Directory
Ruby
https://ruby.0x3d.site/
Ruby Online Directory
Clojure
https://clojure.0x3d.site/
Clojure Online Directory
Elixir
https://elixir.0x3d.site/
Elixir Online Directory
Elm
https://elm.0x3d.site/
Elm Online Directory
Lua
https://lua.0x3d.site/
Lua Online Directory
C Programming
https://c-programming.0x3d.site/
C Programming Online Directory
C++ Programming
https://cpp-programming.0x3d.site/
C++ Programming Online Directory
R Programming
https://r-programming.0x3d.site/
R Programming Online Directory
Perl
https://perl.0x3d.site/
Perl Online Directory
Java
https://java.0x3d.site/
Java Online Directory
Kotlin
https://kotlin.0x3d.site/
Kotlin Online Directory
PHP
https://php.0x3d.site/
PHP Online Directory
React JS
https://react.0x3d.site/
React JS Online Directory
Angular
https://angular.0x3d.site/
Angular JS Online Directory