alandefreitas / matplotplusplus

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

quiver: nan issue when `u` or `v` entries have a 0 #279

Closed acxz closed 2 years ago

acxz commented 2 years ago

Bug category

Describe the bug

When an entry for u or v is all zeros, the scaling results in u_scaled/v_scaled to have nans due to a division by u_max, v_max.

Steps to Reproduce

#include <cmath>
#include <matplot/matplot.h>

int main() {
    using namespace matplot;

    // Produces 10 entries
    auto x = iota(0, 2.0 / 20, 2);
    auto y = iota(0, 2.0 / 20, 2);

    // Produces 10 entries
    std::vector<double> theta;
    for (int t = 0; t < x.size(); t++) {
        theta.push_back(0 + 2 * M_PI / x.size() * t);
    }

    auto u =
        transform(theta, [](double theta) { return 1; });
    auto v =
        transform(theta, [](double theta) { return 0; });

    quiver(x, y, u, v);
    show();
    return 0;
}

Output

Empty plot with the following console output: ```console Press ENTER to continue... line 21: warning: Skipping data file with no valid points multiplot> plot '-' with vectors linecolor rgb "0x000071BC" dashtype 1 linewidth 0.5 linetype -1 ^ line 21: x range is invalid line 42: warning: Skipping data file with no valid points multiplot> plot '-' with vectors linecolor rgb "0x000071BC" dashtype 1 linewidth 0.5 linetype -1 ^ line 42: x range is invalid line 42: warning: refresh not possible and replot is disabled line 42: warning: refresh not possible and replot is disabled line 42: warning: refresh not possible and replot is disabled line 42: warning: refresh not possible and replot is disabled ```

Platform

Environment Details:

Additional context

Related discussion, which prompted me in discovering this bug: https://github.com/alandefreitas/matplotplusplus/discussions/278