Plotly for Rust

Aug 18, 2021 | Data Science

Plotly for Rust – A plotting library powered by Plotly.js. This library allows you to create dynamic and visually appealing plots in Rust. Let’s dive into how you can get started with it!

Table of Contents

Introduction

Plotly for Rust is a straightforward plotting library that makes it easy to visualize data with minimal effort. Documentation and numerous interactive examples can be found in the Plotly.rs Book, the examples directory, and docs.rs.

Basic Usage

To begin using Plotly.rs, you will need to add it to your Cargo.toml file:

[dependencies]
plotly = "0.10.0"

Exporting an Interactive Plot

You can save any figure as an HTML file using the Plot.write_html() method. This HTML file can be opened in any web browser for a fully interactive experience. Think of this method as a postman, delivering a beautiful plot right to your viewer’s doorstep.

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");

Exporting a Static Image

To save a plot as a static image, you need the kaleido feature:

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

With this feature, you can save plots as formats like PNG or JPEG. It’s like capturing a snapshot of your work!

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

To enable Plotly.rs in a WebAssembly-based environment, you initiate it with:

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

Then include the Plotly JavaScript library in your base HTML. This allows you to create plots in frontend frameworks like Yew. Imagine it as setting a stage for your plot character to perform!

html



    


    

Crate Feature Flags

Plotly for Rust comes with several feature flags:

  • kaleido: Allows saving plots in various static formats.
  • plotly_image: Provides direct integration with image types.
  • plotly_ndarray: Supports ndarray types for plotting.
  • plotly_embed_js: Embeds Plotly.js directly, enhancing load speed at the expense of file size.
  • wasm: Enables compilation for Wasm targets.

Contributing

If you encounter any bugs or have feature suggestions, kindly submit an issue on the issue tracker. Pull requests are always welcome!

License

Plotly.rs is distributed under the terms of the MIT license. For more details, see the LICENSE-MIT and COPYRIGHT.

Troubleshooting

If you encounter issues while using Plotly for Rust, consider the following troubleshooting tips:

  • Ensure you have the correct version of Rust installed.
  • Verify your internet connection, especially if you’re using the CDN for JavaScript.
  • Check for any missing feature flags in your Cargo.toml that are necessary for your intended functionality.
  • Review the documentation for updates or common issues.

For more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.

At fxis.ai, we believe that such advancements are crucial for the future of AI, as they enable more comprehensive and effective solutions. Our team is continually exploring new methodologies to push the envelope in artificial intelligence, ensuring that our clients benefit from the latest technological innovations.

Stay Informed with the Newest F(x) Insights and Blogs

Tech News and Blog Highlights, Straight to Your Inbox