Skip to content

TobiasWallner/plotpp

Repository files navigation

Plotpp

A plotting library for C++ that uses Gnuplot as a backend. Allows plotting from custom containers/vectors if they use std::begin()/std::end() iterators as well as plotting from built in arrays or alternatively have .data()/.size() methods.

The library is very versatile. Containers can be mixed and matched not only in type but also in the way they are being passed. Pass 3 different containers as pass by reference, copy or move - no problem, the library will adapt.

Documentation

Requirements

  • Gnuplot: Homepage Windows Download Linux install:
    Note: The project can be build without gnuplot, since this library will only communicate with gnuplot through pipes. But you will need gnuplot to display the graphical plots.
  • C++20

Dependencies

All dependencies are managed using CPM, a setup-free CMake dependency management system. So if you already use CMake for your C++ project, you do not have to do anything. Just include this CMake project however you like and all dependencies will be downloaded and build for you.

Examples

You can see all examples in the examples/ folder.

Line Plot

int main() {
	std::vector<float> x = linspace<float>(-2, 2, 100);
	std::vector<double> y = apply_func(x, [](double x){return -x + x * x * x;});
	
	using namespace plotpp;
	
	Figure fig("Line Plot from XY");
	fig.add(line(&x, std::move(y)).label("f1"));
	fig.grid();
	fig.show();
	fig.save("line-plot.svg");
	
    return 0;
}

Image of a line plot

Arrow Plot

int main() {
	using namespace plotpp;
	
	double arrow_x1[] = {-1, -2, -3, -4, -5};
	double arrow_y1[] = {-1, -2, -1, -2, -3};
	double arrow_x2[] = {-2, -3, -4, -5, -6};
	double arrow_y2[] = {-3, -4, -2, -3, -5};
	
	Figure fig("Arrow-Plot");
	fig.add(arrows(&arrow_x1, &arrow_y1, &arrow_x2, &arrow_y2).label("arrow plot"));
	fig.show();
	fig.save("arrows.svg");
	
    return 0;
}

Image of an arrow plot

Multiplot

int main(){
	using namespace plotpp;
	
	const auto x = linspace(-3.1415, 3.1415, 50);
	
	Multiplot mplt(2, 2, "Multiplot");
	mplt.at(0, 0).add(line(&x, sin(x)).label("Top-Left"));
	mplt.at(0, 1).add(points(&x, cos(x))).label("Top-Right"));
	mplt.at(1, 1).add(line(&x, tan(x)).label("Bottom-Right"));
	mplt.show();
	mplt.save("multiplot.jpg");
	
	return 0;
}

Multiplot Image

Integration

CMake: CPM

Get and include the CPM script, then use CPMAddPackage() to add git repositories as libraries to your project

include(CPM.cmake)

CPMAddPackage("gh:TobiasWallner/plotpp#main")

target_link_libraries(YOUR_PROJECT_NAME PUBLIC plotpp)

Optionally: set a download cache for your libraries by setting the environment variable CPM_SOURCE_CACHE to a directory of your choice

clone plotpp into your project

git clone https://github.com/TobiasWallner/plotpp.git

include the project into CMake via add_subdirectory

add_subdirectory(Path/to/plotpp)

target_link_libraries(YOUR_PROJECT_NAME PUBLIC plotpp)

Compiles with:

  • GCC version 13.2.0
  • Clang version 16.0.3
  • Visual Studio 17 2022

About

A plotting library for C++ that uses Gnuplot as its backend without creating temporary files.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published