Plotting graphs in C++

17,204

Solution 1

You could try https://github.com/lava/matplotlib-cpp, which looks like it is just a wrapper around matplotlib anyway, so you are still calling/using Python and matplotlib in the end. With this you probably can copy your code nearly verbatim to "C++".

Solution 2

An excellent C++ library to plot graphs is ROOT. It was developed by CERN for physicists. It also includes a C++ shell, in case you want to use C++ with an interactive prompt.

You can find the documentation, download links, and lots of examples, at https://root.cern/.

There is also a stackoverflow question by someone trying to plot points: CERN ROOT: Is is possible to plot pairs of x-y data points?

Share:
17,204
SHIVANSHU SAHOO
Author by

SHIVANSHU SAHOO

Updated on June 08, 2022

Comments

  • SHIVANSHU SAHOO
    SHIVANSHU SAHOO about 2 years

    I have made the following graph using matplotlib in python.I have also attached the code I used to make this.enter image description here

    The code for the arena

    import matplotlib.pyplot as plt
    import matplotlib.patches as patches
    
    obs_boundary = [
                [0, 0, 10, 600],
                [0, 600, 900, 10],
                [10, 0, 900, 10],
                [900, 10, 10, 600]
            ]
    obs_cir_own = [
        [50,500,10],
        [100,300,10],
        [240,240,10],
        [300,400,10],
        [190,50,10]
    
            ]
    obs_cir_opp = [
                [700, 420, 10],
                [460, 200, 10],
                [550, 500, 10],
                [670, 70, 10],
                [800, 230, 10],
                [600,300,10]
            ]
    fig, ax = plt.subplots()
    
    for (ox, oy, w, h) in obs_boundary:
        print(ox, oy, w, h)        
        ax.add_patch(
                    patches.Rectangle(
                        (ox, oy), w, h,
                        edgecolor='black',
                        facecolor='black',
                        fill=True
                    )
                )
    
    for (ox, oy,r) in obs_cir_own:
                ax.add_patch(
                    patches.Circle(
                        (ox, oy), r,
                        edgecolor='black',
                        facecolor='green',
                        fill=True
                    )
                )
    for (ox, oy, r) in obs_cir_opp:
                ax.add_patch(
                    patches.Circle(
                        (ox, oy), r,
                        edgecolor='black',
                        facecolor='red',
                        fill=True
                    )
                )
     
    plt.plot(50,50, "bs", linewidth=30)
    plt.plot(870, 550, "ys", linewidth=30)           
    name='arena'
    plt.title(name)
    plt.axis("equal")
    

    So, I want to implement a similar arena using C++ and I have no idea how to do it? I researched I got to know something about qtplot again I dont know much about qt. So, is qtplot the only way or there are some easier way. Please tell me how to implement this in C++.

  • G. Sliepen
    G. Sliepen almost 4 years
    While I'm happy you mention the Scientific OpenGL Tutorial which I wrote, I think it's not the way to go if you just want a simple library to plot graphs :)
  • SHIVANSHU SAHOO
    SHIVANSHU SAHOO almost 4 years
    actually i will use it to test a path planning algorithm which is in c++, so can i use this to my purpose? means can i call it from a c++ code?
  • SHIVANSHU SAHOO
    SHIVANSHU SAHOO almost 4 years
    @G.Sliepen so can you suggest a easier way to plot graphs?
  • gibs
    gibs almost 4 years
    What about plotutils then? gnu.org/software/plotutils . It is an official C++ library: en.cppreference.com/w/cpp/links/libs
  • G. Sliepen
    G. Sliepen almost 4 years
    @SHIVANSHUSAHOO I'm afraid I don't have a good answer myself. It looks like matplotlib-cpp is a good option for you, as mentioned by Jan Crhistoph Terasa.