Skip to main content

scattergeo/
scattergeo.rs

1use plotlars::{Mode, Plot, Rgb, ScatterGeo, Shape, Text};
2use polars::prelude::*;
3
4fn main() {
5    let cities = LazyCsvReader::new(PlRefPath::new("data/us_cities_regions.csv"))
6        .finish()
7        .unwrap()
8        .select([col("city"), col("lat"), col("lon")])
9        .limit(5)
10        .collect()
11        .unwrap();
12
13    ScatterGeo::builder()
14        .data(&cities)
15        .lat("lat")
16        .lon("lon")
17        .text("city")
18        .plot_title(Text::from("US Major Cities").font("Arial").size(20))
19        .build()
20        .plot();
21
22    let cities_with_regions = LazyCsvReader::new(PlRefPath::new("data/us_cities_regions.csv"))
23        .finish()
24        .unwrap()
25        .collect()
26        .unwrap();
27
28    ScatterGeo::builder()
29        .data(&cities_with_regions)
30        .lat("lat")
31        .lon("lon")
32        .mode(Mode::Markers)
33        .text("city")
34        .group("region")
35        .size(20)
36        .colors(vec![
37            Rgb(255, 0, 0),
38            Rgb(0, 255, 0),
39            Rgb(0, 0, 255),
40            Rgb(255, 165, 0),
41        ])
42        .shapes(vec![
43            Shape::Circle,
44            Shape::Square,
45            Shape::Diamond,
46            Shape::Cross,
47        ])
48        .plot_title(
49            Text::from("US Cities by Region")
50                .font("Arial")
51                .size(24)
52                .x(0.5),
53        )
54        .legend_title(Text::from("Region").size(14))
55        .build()
56        .plot();
57
58    // Example 3: ScatterGeo with lines connecting cities (flight paths)
59    let flight_path = LazyCsvReader::new(PlRefPath::new("data/flight_path.csv"))
60        .finish()
61        .unwrap()
62        .collect()
63        .unwrap();
64
65    ScatterGeo::builder()
66        .data(&flight_path)
67        .lat("lat")
68        .lon("lon")
69        .mode(Mode::LinesMarkers)
70        .text("city")
71        .size(15)
72        .color(Rgb(0, 123, 255))
73        .line_width(2.0)
74        .line_color(Rgb(255, 123, 0))
75        .opacity(0.8)
76        .plot_title(Text::from("Flight Path: NY to LA").font("Arial").size(20))
77        .build()
78        .plot();
79
80    // Example 4: World cities with custom styling
81    let world_cities = LazyCsvReader::new(PlRefPath::new("data/world_cities.csv"))
82        .finish()
83        .unwrap()
84        .collect()
85        .unwrap();
86
87    ScatterGeo::builder()
88        .data(&world_cities)
89        .lat("lat")
90        .lon("lon")
91        .mode(Mode::Markers)
92        .text("city")
93        .group("continent")
94        .size(25)
95        .opacity(0.7)
96        .colors(vec![
97            Rgb(255, 0, 0),
98            Rgb(0, 255, 0),
99            Rgb(0, 0, 255),
100            Rgb(255, 255, 0),
101            Rgb(255, 0, 255),
102            Rgb(0, 255, 255),
103        ])
104        .plot_title(
105            Text::from("Major World Cities by Continent")
106                .font("Arial")
107                .size(24),
108        )
109        .legend_title(Text::from("Continent").size(16))
110        .build()
111        .plot();
112}