矢量可视化使用绘图器“绘制器中的界限:: ChartContext ::< a,db,ct> :: draw_series”'&quort失败了。

发布于 2025-01-30 14:51:31 字数 1596 浏览 1 评论 0原文

我正在尝试使用板条箱“绘图器”在2D中可视化矢量项目。

fn main() -> Result<(), Box<dyn Error>> {
    let file_path = "out.csv";
    let file = File::open(file_path);
    let mut rdr = csv::ReaderBuilder::new().delimiter(b',').from_path(file_path)?;
    let mut ke: Vec<f64> = Vec::new();  // reading itself is ok
    let mut pe: Vec<f64> = Vec::new();  // reading itself is ok

    for res in rdr.records() {
        let record = res?;

        ke.push(record[0].parse().unwrap());
        pe.push(record[1].parse().unwrap());
        println!("{:?}, {:?}", &record[0], &record[1]);
    }

    let (ke_min, ke_max) = ke.iter().fold((0.0/0.0, 0.0/0.0),|(m,n), v| (v.min(m), v.max(n)));

    let mut backend = BitMapBackend::new("outputs/test.png", (600, 400));
    let root:DrawingArea<_,_> = backend.into();
    root.fill(&RGBColor(255,255,255)).unwrap();
    
    let font = ("san-serif", 20);

    let mut chart: ChartContext<BitMapBackend, Cartesian2d<RangedCoordusize, RangedCoordf64>> = ChartBuilder::on(&root)
        .caption("kinetic energy and potential", font.into_font())
        .build_cartesian_2d(0..ke.len(), ke_min..ke_max)?;
    chart.configure_mesh().draw()?;
    chart.draw_series(LineSeries::new(ke, &RGBColor(255,0,0)));

    Ok(())
}

错误是

error[E0277]: the trait bound `for<'b> &'b DynElement<'static, plotters::prelude::BitMapBackend<'_>, f64>: PointCollection<'b, (usize, f64)>` is not satisfied

几秒钟前从鸡蛋中孵化的IMA Rustacean,所以我不确定如何解决这个问题。

I'm trying to visualize vector items in 2D using the crate "plotters".

fn main() -> Result<(), Box<dyn Error>> {
    let file_path = "out.csv";
    let file = File::open(file_path);
    let mut rdr = csv::ReaderBuilder::new().delimiter(b',').from_path(file_path)?;
    let mut ke: Vec<f64> = Vec::new();  // reading itself is ok
    let mut pe: Vec<f64> = Vec::new();  // reading itself is ok

    for res in rdr.records() {
        let record = res?;

        ke.push(record[0].parse().unwrap());
        pe.push(record[1].parse().unwrap());
        println!("{:?}, {:?}", &record[0], &record[1]);
    }

    let (ke_min, ke_max) = ke.iter().fold((0.0/0.0, 0.0/0.0),|(m,n), v| (v.min(m), v.max(n)));

    let mut backend = BitMapBackend::new("outputs/test.png", (600, 400));
    let root:DrawingArea<_,_> = backend.into();
    root.fill(&RGBColor(255,255,255)).unwrap();
    
    let font = ("san-serif", 20);

    let mut chart: ChartContext<BitMapBackend, Cartesian2d<RangedCoordusize, RangedCoordf64>> = ChartBuilder::on(&root)
        .caption("kinetic energy and potential", font.into_font())
        .build_cartesian_2d(0..ke.len(), ke_min..ke_max)?;
    chart.configure_mesh().draw()?;
    chart.draw_series(LineSeries::new(ke, &RGBColor(255,0,0)));

    Ok(())
}

and the error is

error[E0277]: the trait bound `for<'b> &'b DynElement<'static, plotters::prelude::BitMapBackend<'_>, f64>: PointCollection<'b, (usize, f64)>` is not satisfied

I`m a rustacean which was hatched from an egg a few seconds ago, so I'm not sure how to solve this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

看透却不说透 2025-02-06 14:51:31

首先将行Chart.Draw_Series(lineSeries :: new(ke,&amp; rgbColor(255,0,0)))); 分两行:

let series = LineSeries::new(ke, &RGBColor(255, 0, 0));
chart.draw_series(series);

查看series serieslineseries&lt; bitmapbackend&lt; rgbpixel&gt;,f64&gt;

如果您现在查看错误(一些简化的事情):

the trait bound DynElement<'static, BitMapBackend<'_>, f64>: PointCollection<'b, (usize, f64)> is not satisfied

您可以找到两个部分:&lt; ...,bitmapbackend ...,f64&gt;看起来非常 系列的类型。实际上,这就是错误消息中f64的原因。

现在查看图表的类型:

ChartContext<BitMapBackend, Cartesian2d<RangedCoordusize, RangedCoordf64>>

简化您要创建一个具有X值usize的X值的2D坐标系统,和类型的y值f64 。您在错误消息中也可以找到这两种类型。

但是现在看:您要创建一个 2D 坐标系统(类型的X值usize,类型f64的y值),但仅是在lineseries :: new中,在y值的vec(类型f64)中提供vec
可能您忘记了X值!

如果您查看 需要一种可以迭代创建坐标的类型。因此,在您的情况下,您的类型图表(您明确指定了图表的类型Intiterator&lt; item =(usize,f64)&gt; < /代码>。

我不知道X值将来自哪里,但是如果您真的不在乎,您可以尝试一下,这将创建从0上升的X轴。

chart.draw_series(LineSeries::new(
   ke.into_iter().enumerate(),
   &RGBColor(255, 0, 0),
));

如果要自定义X值,我建议将ke的类型更改为vec&lt;(usize,f64)&gt;,并在您的for-中创建适当的值环形。

(如果您可以包含使用 s,则可以轻松编辑和测试您的代码也将很有帮助。此外,说明错误的行也将很有帮助)

First split the line chart.draw_series(LineSeries::new(ke, &RGBColor(255,0,0))); into two lines:

let series = LineSeries::new(ke, &RGBColor(255, 0, 0));
chart.draw_series(series);

and look at the type of series: LineSeries<BitMapBackend<RGBPixel>, f64>.

If you look now at the error (some things simplified):

the trait bound DynElement<'static, BitMapBackend<'_>, f64>: PointCollection<'b, (usize, f64)> is not satisfied

You can find two parts: <..., BitMapBackend..., f64> looks very similar to the type of series. In fact that's the reason for the f64 in the error message.

Now look at the type of chart:

ChartContext<BitMapBackend, Cartesian2d<RangedCoordusize, RangedCoordf64>>

Simplified you want to create a 2d coordinate system with x-values of type usize and y-values of type f64. These two types you can find also in the error message.

But now look: You want to create a 2d coordinate system (x-values of type usize, y-values of type f64) but only supply an Vec of the y-values (of type f64) in LineSeries::new!
Probably you forgot your x-values!

If you look at LineSeries::new it takes a type which can be iterated to create the coords. So that would be in your case for your type of chart (you explicitly specified the type of chart) IntoIterator<Item = (usize, f64)>.

I don't know where the x-values will come from but if you don't really care you could try this, which will create a x-axis from 0 upwards.

chart.draw_series(LineSeries::new(
   ke.into_iter().enumerate(),
   &RGBColor(255, 0, 0),
));

If you want to customize the x-values, I recommend changing the type of ke to Vec<(usize, f64)> and create the appropriate values in your for-loop.

(It would be also helpful if you could include your uses, so that one can easily edit and test your code. Additionally stating the line of the error would be also helpful)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文