Quick-start to run a mathplot example

This page is going to show you how to build and run one of the mathplot 2D graph examples.

Quick-start assumption: You’re using a Debian flavour of Linux. If you’re using another type of Linux, then I hope you can figure out the packages you’ll need from the list below and how to install them. In all other ways, the process will be the same. If you’re using a Mac, see README.build.mac for help getting dependencies in place. It’s README.build.windows for Windows users (vcpkg is used to obtain dependencies).

Install dependencies

This command will install dependencies for building graph1.cpp and (almost) all the other examples (assuming a Debian-like OS)

sudo apt install build-essential cmake git ninja-build \
                 librapidxml-dev freeglut3-dev libglu1-mesa-dev libxmu-dev \
                 libxi-dev libglfw3-dev libfreetype-dev libhdf5-dev

Clone the code

Get your copy of the mathplot code from Github.

git clone git@github.com:sebsjames/mathplot

Prepare the build

cd mathplot
git submodule init    # Initialize and update the submodules (json, maths and maths_config)
git submodule update
mkdir build           # Create a build directory
cd build

Do the build

You first run cmake to generate makefiles, then make will execute the compiler calls.

CC=clang-20 CXX=clang++-20 cmake .. -GNinja # Call cmake to generate the ninja build files
ninja graph1                                # Compile a single one of the examples

(There are lots of other examples—Try ninja graph2 or ninja convolve.)

Run the program

./examples/graph1   # Run the program. You should see a graph of a cubic function.
# After closing the graph1 program, open its source code and modify something (see examples/graph2.cpp for ideas)
gedit ../examples/graph1.cpp

The program graph1.cpp is:

// Visualize a graph. Minimal example showing how a default graph appears
#include <memory>
import mplot.visual;
import mplot.graphvisual; // exports sm.vvec and sm.vec

int main()
{
    // Set up a mplot::Visual 'scene environment'.
    mplot::Visual v(1024, 768, "Made with mplot::GraphVisual");
    // Create a GraphVisual object (obtaining a unique_ptr to the object) with a spatial offset within the scene of 0,0,0
    auto gv = std::make_unique<mplot::GraphVisual<double>> (sm::vec<float>({0,0,0}));
    // This mandatory line of boilerplate code sets the parent pointer in GraphVisual and binds some functions
    gv->set_parent (v.get_id());
    // Data for the x axis. A vvec is like std::vector, but with built-in maths methods
    sm::vvec<double> x;
    // This works like numpy's linspace() (the 3 args are "start", "end" and "num"):
    x.linspace (-0.5, 0.8, 14);
    // Set a graph up of y = x^3
    gv->setdata (x, x.pow(3));
    // finalize() makes the GraphVisual compute the vertices of the OpenGL model
    gv->finalize();
    // Add the GraphVisual OpenGL model to the Visual scene, transferring ownership of the unique_ptr
    v.addVisualModel (gv);
    // Render the scene on the screen until user quits with 'Ctrl-q'
    v.keepOpen();
    // Because v owns the unique_ptr to the GraphVisual, its memory will be deallocated when v goes out of scope.
    return 0;
}

The program generates a clean looking graph…

Screenshot of graph1.cpp output showing a cubic function

…and the code compares favourably (in terms of amount of boilerplate code) with the equivalent Python, graph1.py:

# Visualize the graph from graph1.cpp in Python
import matplotlib.pyplot as plt
import numpy as np

# Create some data for the x axis
x = np.linspace(-0.5, 0.8, 14)
# Set a graph up of y = x^3
plt.plot(x, np.power(x,3), '-o')
# Add labels
plt.title('Made with Python/numpy/matplotlib')
plt.xlabel('x')
plt.ylabel('y')
# Render the graph on the screen until user quits with 'q'
plt.show()

This site uses Just the Docs, a documentation theme for Jekyll.