'The matplotlibcpp show an error when I use subplot() in cpp

I tried to use matplotlibcpp.h for plotting graph in c++ code. Normal graphs are plotted well. However, when I write plt::subplot(); the program throw runtime error with "Call to subplot() failed". How can solve this problem?

Below is my source code.

#include <iostream>
#include <vector>
#include <map>
#include <string>
#include <matplotlib.h>

namespace plt = matplotlibcpp;

using std::cout;
using std::map;
using std::string;
using std::vector;

int main() 
{
    vector<int> x1(10);
    vector<int> x2(10);
    vector<int> y1(10);
    vector<int> y2(10);

    for(int i = 0; i <10; ++i)
    {
        x1[i] = i;
        y1[i] = i;
        x2[i] = i;
        y2[i] = i*2;
    }

    plt::subplot(1,2,1); // <- error raised point
    plt::plot(x1,y1);
    plt::title("y=x");
    
    plt::subplot(1,2,2);
    plt::plot(x2,y2,"k-");
    plt::title("y=2x");

    plt::show();
}

The full error message is

terminate called after throwing an instance of 'std::runtime_error'
  what():  Call to subplot() failed.
Aborted

And, my compile option is

g++ matplotlib_test.cpp -I/usr/include/python3.8 -lpython3.8 -o matplotlib_test

My running environment is wsl2(windows-subsystem-linux) and ubuntu-20.04-LTS

Lastly, could you recommend what is the best way for plotting graph with c++ code?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source