Skip to content

Instantly share code, notes, and snippets.

@ChengzhiZhao
Last active February 16, 2022 07:36
Show Gist options
  • Save ChengzhiZhao/653879f34ae2f66691526a00c4d35c7c to your computer and use it in GitHub Desktop.
Save ChengzhiZhao/653879f34ae2f66691526a00c4d35c7c to your computer and use it in GitHub Desktop.
Rust Plotters Data Visulization
use plotters::prelude::*;
use std::error::Error;
fn read_csv() -> Result<(Vec<String>, Vec<f64>,Vec<f64>), Box<dyn Error>> {
// Build the CSV reader and iterate over each record.
// Data Source is from https://www.kaggle.com/johnharshith/hollywood-theatrical-market-synopsis-1995-to-2021/version/2?select=TopGenres.csv
let mut rdr = csv::Reader::from_path("~/Downloads/TopGenres.csv")?;
let mut genres:Vec<String> = Vec::new();
let mut movies:Vec<f64> = Vec::new();
let mut market_share:Vec<f64> = Vec::new();
for result in rdr.records() {
// The iterator yields Result<StringRecord, Error>, so we check the
// error here..
let record = result?;
match record.get(1) {
Some(i) => genres.push(i.to_string()),
_ => ()
}
match record.get(2) {
Some(i) => {
movies.push(i.replace(",","").to_string().parse::<f64>().unwrap()/100.0 as f64)
},
_ => ()
}
match record.get(5) {
Some(i) => market_share.push(i.replace("%","").to_string().parse::<f64>().unwrap()),
_ => ()
}
}
return Ok((genres, movies, market_share));
}
fn main() {
let movie_genres = match read_csv(){
Ok(t) => t,
_ => (Vec::new(), Vec::new(),Vec::new())
};
let genres = movie_genres.0;
let movies = movie_genres.1;
let market_share = movie_genres.2;
let movies_market_share: Vec<(f64, f64)>= movies.iter().cloned().zip(market_share.iter().cloned()).collect();
let genres_market_share: Vec<(String, f64)>= genres.iter().cloned().zip(market_share.iter().cloned()).collect();
let root_area = BitMapBackend::new("/Users/chengzhizhao/Downloads/test.png", (600, 400)).into_drawing_area();
root_area.fill(&WHITE).unwrap();
let mut ctx = ChartBuilder::on(&root_area)
.set_label_area_size(LabelAreaPosition::Left, 40.0)
.set_label_area_size(LabelAreaPosition::Bottom, 40.0)
.set_label_area_size(LabelAreaPosition::Right, 40.0)
.set_label_area_size(LabelAreaPosition::Top, 40.0)
.caption("Movies Market Share", ("sans-serif", 40.0))
.build_cartesian_2d(0.0..70.0, 0.0..40.0)
.unwrap();
ctx.configure_mesh().draw().unwrap();
// Draw Scatter Plot
ctx.draw_series(
movies_market_share.iter().map(|point| Circle::new(*point, 4.0_f64, &RED)),
).unwrap();
// Draw Area Plot
// ctx.draw_series(
// AreaSeries::new((0..).zip(market_share.iter().map(|x| *x)), 0.0,&RED.mix(0.2)).border_style(&RED)
// ).unwrap();
// Draw Bar Plot
// ctx.draw_series((0..).zip(market_share.iter()).map(|(x, y)| {
// let x0 = SegmentValue::Exact(x);
// let x1 = SegmentValue::Exact(x + 1);
// let y0 = y.round() as i32;
// let mut bar = Rectangle::new([(x0, 0), (x1, y0)], BLUE.filled());
// bar.set_margin(0, 0, 5, 5);
// bar
// })).unwrap();
// Draw Line Chart
// ctx.draw_series(
// LineSeries::new((0..).zip(market_share.iter()).map(|(idx, y)| {(idx, *y)}),&BLUE)
// ).unwrap();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment