pub struct Plot { /* private fields */ }
Expand description
Plot is a container for structs that implement the Trace
trait. Optionally a Layout
can
also be specified. Its function is to serialize Trace
s and the Layout
in html format and
display and/or persist the resulting plot.
§Examples
extern crate plotly;
use plotly::common::Mode;
use plotly::{Plot, Scatter};
fn line_and_scatter_plot() {
let trace1 = Scatter::new(vec![1, 2, 3, 4], vec![10, 15, 13, 17])
.name("trace1")
.mode(Mode::Markers);
let trace2 = Scatter::new(vec![2, 3, 4, 5], vec![16, 5, 11, 9])
.name("trace2")
.mode(Mode::Lines);
let trace3 = Scatter::new(vec![1, 2, 3, 4], vec![12, 9, 15, 12]).name("trace3");
let mut plot = Plot::new();
plot.add_trace(trace1);
plot.add_trace(trace2);
plot.add_trace(trace3);
plot.show();
}
fn main() -> std::io::Result<()> {
line_and_scatter_plot();
Ok(())
}
Implementations§
Source§impl Plot
impl Plot
Sourcepub fn use_local_plotly(&mut self)
pub fn use_local_plotly(&mut self)
This option results in the plotly.js library being written directly in the html output. The benefit is that the plot will load faster in the browser and the downside is that the resulting html will be much larger.
Sourcepub fn add_traces(&mut self, traces: Vec<Box<dyn Trace>>)
pub fn add_traces(&mut self, traces: Vec<Box<dyn Trace>>)
Add multiple Trace
s to the Plot
.
Sourcepub fn set_layout(&mut self, layout: Layout)
pub fn set_layout(&mut self, layout: Layout)
Set the Layout
to be used by Plot
.
Sourcepub fn show(&self)
pub fn show(&self)
Renders the contents of the Plot
and displays them in the system default browser.
This will serialize the Trace
s and Layout
in an html page which is saved in the temp
directory. For example on Linux it will generate a file plotly_<22 random characters>.html
in the /tmp directory.
Sourcepub fn show_png(&self, width: usize, height: usize)
pub fn show_png(&self, width: usize, height: usize)
Renders the contents of the Plot
, creates a png raster and displays it in the system default browser.
To save the resulting png right-click on the resulting image and select Save As...
.
Sourcepub fn show_jpeg(&self, width: usize, height: usize)
pub fn show_jpeg(&self, width: usize, height: usize)
Renders the contents of the Plot
, creates a jpeg raster and displays it in the system default browser.
To save the resulting png right-click on the resulting image and select Save As...
.
Sourcepub fn to_html<P: AsRef<Path>>(&self, filename: P)
pub fn to_html<P: AsRef<Path>>(&self, filename: P)
Renders the contents of the Plot
and displays it in the system default browser.
In contrast to Plot::show()
this will save the resulting html in a user specified location
instead of the system temp directory.
In contrast to Plot::write_html
, this will save the resulting html to a file located at a
user specified location, instead of a writing it to anything that implements std::io::Write
.
Sourcepub fn write_html<W: Write>(&self, buffer: &mut W)
pub fn write_html<W: Write>(&self, buffer: &mut W)
Renders the contents of the Plot
to HTML and outputs them to a writable buffer.
In contrast to Plot::to_html
, this will save the resulting html to a byte buffer using the
std::io::Write
trait, instead of to a user specified file.
Sourcepub fn to_inline_html<T: Into<Option<&'static str>>>(
&self,
plot_div_id: T,
) -> String
pub fn to_inline_html<T: Into<Option<&'static str>>>( &self, plot_div_id: T, ) -> String
Renders the contents of the Plot
and returns it as a String, for embedding in
web-pages or Jupyter notebooks. A div
is generated with the supplied id followed by the
script that generates the plot. The assumption is that plotly.js is available within the
html page that this element is embedded. If that assumption is violated then the plot will
not be displayed.
If plot_div_id
is None
the plot div id will be randomly generated, otherwise the user
supplied div id is used.
Sourcepub fn notebook_display(&self)
pub fn notebook_display(&self)
Display plot in Jupyter Notebook.
Sourcepub fn lab_display(&self)
pub fn lab_display(&self)
Display plot in Jupyter Lab.
Sourcepub fn evcxr_display(&self)
pub fn evcxr_display(&self)
Displays the plot in Jupyter Lab; if running a Jupyter Notebook then use the
notebook_display()
method instead.