【问题标题】:Ploting issues from a std::vector<std::array<float, 1500>> y(6) using matplotlib.h (c++)使用 matplotlib.h (c++) 从 std::vector<std::array<float, 1500>> y(6) 绘制问题
【发布时间】:2022-06-30 14:56:25
【问题描述】:

我正在尝试使用 matplotlib 库从向量向量中绘制一些值,但这对我来说是不可能的。我认为我的问题是因为我如何声明此向量,但我找不到另一种处理此类数据的方法。

我需要使用 6x1500 浮点数组,然后绘制所有行。请问有人可以帮我吗?这是我的代码,它是我的主脚本的一个简单示例。

    #include <iostream>
#include "matplotlibcpp.h"
#include <vector>
#include <random>
#include <iostream>
#include <fstream>
#include <cstdio>

namespace plt = matplotlibcpp;
using namespace std;

int main() {
    std::vector<std::array<float, 1500>> y(6);
    float r = static_cast <float> (rand()) / static_cast <float> (RAND_MAX);

        for (int i = 0; i <1500; i++) {
        
        
            for (int j = 0; j < 6; j++) {
                y[j][i] = r;
                cout << "test[" << i << "][" << j << "] = " << y[j][i] << endl;
            }
        
        }
       
    plt::figure();
    plt::plot("log(x)", y.at(0), "b--");
    plt::xlabel("time (s)");
    plt::ylabel("Error (deg)");
    plt::legend();
/*
    plt::figure();
    plt::named_plot("log(x)", y, "b--");
    plt::xlabel("time (s)");
    plt::ylabel("position (deg)");
    plt::legend();
    */
    //plt::title("Trajectory");
    //plt::xlabel("time (s)");
    //plt::ylabel("position (deg)");

    //plt::legend();

    // Set x-axis to interval [0,1000000]
    //plt::xlim(0, n * n);


    //plt::grid();
    plt::show();
    
}

提前致谢!

【问题讨论】:

  • 但对我来说不可能是什么意思?如果有编译时错误,请将其包含在 Q 中。如果编译后的程序没有输出预期的结果,请包含详细信息。

标签: c++ arrays matplotlib plot stdvector


【解决方案1】:

“log(x)”是什么意思?如果您希望它成为标题,请使用

plt::title("log(x)");


如果您希望它是 x 的对数刻度,请使用

plt::semilogx(y.at(0), std::string("b--"));


我觉得

plt::plot(y.at(0), std::string("b--"));

只会解决你的问题。

【讨论】:

    猜你喜欢
    • 2023-03-23
    • 1970-01-01
    • 2011-05-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-12
    相关资源
    最近更新 更多