alandefreitas / matplotplusplus

Matplot++: A C++ Graphics Library for Data Visualization 📊🗾
https://alandefreitas.github.io/matplotplusplus/
MIT License
4.11k stars 313 forks source link

Exporting figure dumps all to console instead of file #226

Open aliaksei135 opened 2 years ago

aliaksei135 commented 2 years ago

Bug category

Describe the bug

When trying to export a figure as per docs, it dumps the raw output of the image into the console after this error:

gnuplot> t output "fig/PlotTest.eps"
         ^
         line 0: invalid command

%!PS-Adobe-2.0
%%Creator: gnuplot 5.4 patchlevel 3
%%CreationDate: Wed Feb 02 14:19:48 2022
%%DocumentFonts: (atend)
<... remainder of the eps file>

Versions:

It sort of looks like it is cutting off the the "se" in "set" since the gnuplot command it is going for seems to be set output "fig/PlotTest.eps" like here, which would probably work. Could it be sending down the pipe before gnuplot is ready or is something lost in the pipe?

Steps to Reproduce MRE:

template <typename Scalar, typename Matrix>
static std::vector<std::vector<Scalar>> fromEigenMatrix(const Matrix& M)
{
    std::vector<std::vector<Scalar>> m;
    m.resize(M.rows(), std::vector<Scalar>(M.cols(), 0));
    for (size_t i = 0; i < m.size(); i++)
        for (size_t j = 0; j < m.front().size(); j++)
            m[i][j] = M(i, j);
    return m;
}

template <typename Scalar>
static void outputMat(const Eigen::Matrix<Scalar, Eigen::Dynamic, Eigen::Dynamic>& m, const std::string& titleStr)
{
    const auto min = m.minCoeff();
    const auto max = m.maxCoeff();

    const auto& vec = fromEigenMatrix<Scalar>(m);

    auto fig = figure(true);
    auto cax = fig->current_axes();
    auto img = image(cax, vec);
    title(cax, titleStr);
    xlabel(cax, "X");
    ylabel(cax, "Y");

    if (min != max)
    {
        colormap(cax, palette::viridis());
        colorbar(cax).limits({min, max});
    }

    save(fig, "fig/" + titleStr + ".eps");
}

int main(){
    Eigen::MatrixXd mat(700,500);
    mat.setRandom();

    outputMat(mat, "PlotTest");
}