首页 > 解决方案 > 如何将背景图像放在 Rust 绘图仪库中的绘图中

问题描述

我正在尝试在飞机上绘制汽车旅行。我正在使用绘图仪库。下面是trips绘制过程的一些代码示例:

pub fn save_trips_as_a_pic<'a>(trips: &CarTrips, resolution: (u32, u32))
{
   // Some initializing stuff 
   /// <...>

    let root_area =
        BitMapBackend::new("result.png", (resolution.0, resolution.1)).into_drawing_area();

    root_area.fill(&WHITE).unwrap();

    let root_area =
        root_area.margin(10,10,10,10).titled("TITLE",
                         ("sans-serif", 20).into_font()).unwrap();

    let drawing_areas =
        root_area.split_evenly((cells.1 as usize, cells.0 as usize));

    for (index, trip) in trips.get_trips().iter().enumerate(){

        let mut chart =
            ChartBuilder::on(drawing_areas.get(index).unwrap())
            .margin(5)
            .set_all_label_area_size(50)
            .build_ranged(50.0f32..54.0f32, 50.0f32..54.0f32).unwrap();

        chart.configure_mesh().x_labels(20).y_labels(10)
            .disable_mesh()
            .x_label_formatter(&|v| format!("{:.1}", v))
            .y_label_formatter(&|v| format!("{:.1}", v))
            .draw().unwrap();

        let coors = trip.get_points();
        {
            let draw_result =
                chart.draw_series(series_from_coors(&coors, &BLACK)).unwrap();

            draw_result.label(format!("TRIP {}",index + 1)).legend(
                move |(x, y)|
                PathElement::new(vec![(x, y), (x + 20, y)], &random_color));
        }
        {
            // Here I put red dots to see exact nodes
            chart.draw_series(points_series_from_trip(&coors, &RED));
        }
        
        chart.configure_series_labels().border_style(&BLACK).draw().unwrap();
    }
}

我现在在 Rust Plotters 上得到了什么:

我现在在 Rust Plotters 上得到了什么

因此,在“result.png”图像文件中绘制后,我很难理解这些“线条”,因为我看不到地图本身。我想,这个库中有一些方法可以将地图“map.png”放在情节的背景中。如果我使用 Python,这个问题将这样解决:

# here we got a map image;
img: Image.Image = Image.open("map-image.jpg")
img.putalpha(64)
imgplot = plt.imshow(img)
# let's pretend that we got our map size in pixels and coordinates
# just in right relation to each other.
scale = 1000
x_shift = 48.0
y_shift = 50.0
coor_a = Coordinate(49.1, 50.4)
coor_b = Coordinate(48.9, 51.0)

x_axis = [coor_a.x, coor_b.x]
x_axis = [(element-x_shift) * scale for element in x_axis]
y_axis = [coor_a.y, coor_b.y]
y_axis = [(element-y_shift) * scale for element in y_axis]

plt.plot(x_axis, y_axis, marker='o')
plt.show()

Python 上的期望结果

好吧,这在 Python 上很容易,但我不知道如何在 Rust 上做类似的事情。

标签: plotrustbackground-image

解决方案


推荐阅读