matplotlib-cpp icon indicating copy to clipboard operation
matplotlib-cpp copied to clipboard

Sharing axes on subplots

Open ElJorko opened this issue 6 years ago • 2 comments

Hello! Is there a way to share the axes on a subplot? If not, is this a functionality that you plan on adding? Say I want to plot a 2x1 plot matrix, on Python I would do this: f, axarr = plt.subplots(2, sharex=True, sharey=False) axarr[0].plot(first_x, first_y) axarr[1].plot(second_x, second_y') I believe this is a fairly common need for matplotlib users. Great work with this library by the way.

ElJorko avatar Oct 14 '19 11:10 ElJorko

I have the same question and implementing such a method to return a matplot py object might not be easy. But I still wish that we can support the feature.

yiakwy avatar Oct 17 '19 13:10 yiakwy

I have refactored this library, and maybe you can refer to it : https://github.com/TING2938/matplotlibcpp

#include <cmath>
#include "matplotlib.hpp"

int main()
{
    auto plt = matplotlibcpp::PLT();
    int n    = 100;
    std::vector<double> x(n), y(n), z(n);
    for (int i = 0; i < n; ++i) {
        x[i] = i * 10.0 / n;
        y[i] = std::sin(x[i]);
        z[i] = std::cos(x[i]);
    }

    auto ax = plt.subplots(2, 1, {7, 7}, {{"sharex", "all"}}).second;

    ax[0].plot(x, y, "-", {{"color", "red"}, {"linewidth", "6"}, {"label", "sin"}});
    ax[0].set_title("sin(x)");
    ax[0].legend();

    ax[1].plot(x, z, "b--");
    ax[1].set_title("cos(x)");

    plt.savefig("subplots.png");
    plt.show();
}

TING2938 avatar Apr 18 '22 09:04 TING2938