Write a plot in rust
Download ▾
From a Chatbot Arena battleThe same person sent this prompt to two anonymous models at once. Compare it with gpt-3.5-turbo-0613’s answer.
AssistantHere is a simple plot example in Rust:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
use plotters::prelude::*;
fn main() -> Result<(), Box<dyn std::error::Error>>{
let root = BitMapBackend::new("plot.png", (640, 480)).into_drawing_area();
root.fill(&WHITE)?;
let mut chart = ChartBuilder::on(&root)
.caption("Simple Plot", ("sans-serif", 50).into_font())
.margin(5)
.x_label_area_size(30)
.y_label_area_size(30)
.build_cartesian_2d(0..10, 0..5)?;
chart.configure_mesh().draw()?;
chart
.draw_series(LineSeries::new(
vec![(0, 1), (2, 3), (3, 2), (7, 4)],
&RED,
))?
.label("Line")
.legend(|(x, y)| PathElement::new(vec![(x, y), (x + 20, y)], &RED));
chart
.configure_series_labels()
.background_style(&WHITE.mix(0.8))
.border_style(&BLACK)
.draw()?;
Ok(())
}
This creates a simple line plot by:
- Creating a new image to draw on
- Setting the chart title and labels
- Drawing the x and y axis grid lines
- Drawing a red line series with some data points
- Adding a legend for the line
- Configuring styling for the data labels
The plotters crate provides a nice API for building plots in Rust. This makes it straightforward to create basic charts and graphs.