矢量可视化使用绘图器“绘制器中的界限:: ChartContext ::< a,db,ct> :: draw_series”'&quort失败了。
我正在尝试使用板条箱“绘图器”在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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先将行
Chart.Draw_Series(lineSeries :: new(ke,&amp; rgbColor(255,0,0))));
分两行:查看
series
series
:lineseries&lt; bitmapbackend&lt; rgbpixel&gt;,f64&gt;
。如果您现在查看错误(一些简化的事情):
您可以找到两个部分:
&lt; ...,bitmapbackend ...,f64&gt;
看起来非常系列的类型
。实际上,这就是错误消息中f64
的原因。现在查看
图表
的类型:简化您要创建一个具有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轴。
如果要自定义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:and look at the type of
series
:LineSeries<BitMapBackend<RGBPixel>, f64>
.If you look now at the error (some things simplified):
You can find two parts:
<..., BitMapBackend..., f64>
looks very similar to the type ofseries
. In fact that's the reason for thef64
in the error message.Now look at the type of
chart
:Simplified you want to create a 2d coordinate system with x-values of type
usize
and y-values of typef64
. 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 typef64
) but only supply anVec
of the y-values (of typef64
) inLineSeries::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 ofchart
(you explicitly specified the type ofchart
)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.
If you want to customize the x-values, I recommend changing the type of
ke
toVec<(usize, f64)>
and create the appropriate values in your for-loop.(It would be also helpful if you could include your
use
s, so that one can easily edit and test your code. Additionally stating the line of the error would be also helpful)