📊 Easily Plot with CVPlot: The “Mini Matplotlib” in C++
Are you using C++ for image processing, robotics, scientific computing, or algorithm experimentation, but struggling with:
“I want to draw a line chart, scatter plot, or display data in real-time, but C++ doesn’t have a plotting tool as simple and easy to use as Python’s Matplotlib?”
Don’t worry! Today we will introduce an lightweight, easy-to-use, and GUI framework-independent open-source C++ plotting library — CVPlot.
🔍 What is CVPlot?
CVPlot is an open-source C++ plotting library designed specifically for computer vision and numerical experiments. Its name comes from “CV” (Computer Vision) + “Plot” (plotting), but its applications extend far beyond CV.
✅ Core Features:
| Feature | Description |
|---|---|
| 🎯 Lightweight and Simple | Header-only, no compilation required |
| 🖼️ Depends on OpenCV | Uses cv::Mat to display images, seamless integration |
| 📈 Supports Various Charts | Line charts, scatter plots, bar charts, heatmaps, etc. |
| 🔄 Real-time Plotting | Supports dynamic updates, suitable for visualizing algorithm processes |
| 📦 No External GUI Dependencies | Directly displays using OpenCV’s imshow |
Project address: https://github.com/pkuros/cvplot
🛠️ Installation and Configuration (Ubuntu/Linux Example)
CVPlot is a header-only library, which means you only need to download the header file to use it.
1. Install OpenCV (Prerequisite)
sudo apt-get install libopencv-dev
2. Download CVPlot Header File
git clone https://github.com/pkuros/cvplot.git
You will get a cvplot.h file, just place it in your project directory.
💡 First Example: Plotting a Sine Wave
Let’s use CVPlot to draw a simple sine function graph.
✅ Project Structure
cvplot_demo/
├── cvplot.h
├── main.cpp
✅ main.cpp Code
#include <iostream>
#include <vector>
#include <cmath>
#include "cvplot.h" // Include CVPlot
#include <opencv2/opencv.hpp>
int main() {
// 1. Prepare data: x from 0 to 4π, y = sin(x)
std::vector<float> x, y;
for (float t = 0; t <= 4 * M_PI; t += 0.1) {
x.push_back(t);
y.push_back(std::sin(t));
}
// 2. Use CVPlot to plot
cvplot::Figure fig;
fig.plot(x, y, cvplot::PlotStyle::Line, cv::Scalar(255, 0, 0)); // Red line
fig.title("y = sin(x)");
fig.xlabel("x");
fig.ylabel("y");
// 3. Display image (using OpenCV)
cv::Mat plot = fig.render(600, 400); // Width 600, height 400
cv::imshow("Sine Wave", plot);
cv::waitKey(0); // Wait for key press to close
return 0;
}
✅ Compilation Command
g++ main.cpp -o sine_plot `pkg-config --cflags --libs opencv4` # OpenCV 4
# or
g++ main.cpp -o sine_plot `pkg-config --cflags --libs opencv` # OpenCV 2/3
🖨️ Result
You will see a window displaying a red sine wave with a title and axis labels.
🧮 Going Further: Plotting Scatter Plot + Multiple Curves
Let’s plot two sets of data: sine and cosine, and display them using a scatter plot.
#include <vector>
#include <cmath>
#include "cvplot.h"
#include <opencv2/opencv.hpp>
int main() {
std::vector<float> x, y1, y2;
for (float t = 0; t <= 2 * M_PI; t += 0.2) {
x.push_back(t);
y1.push_back(std::sin(t));
y2.push_back(std::cos(t));
}
cvplot::Figure fig;
// Plot sin(x) as a blue line
fig.plot(x, y1, cvplot::PlotStyle::Line, cv::Scalar(255, 0, 0));
// Plot cos(x) as green points
fig.plot(x, y2, cvplot::PlotStyle::Points, cv::Scalar(0, 255, 0));
fig.title("Sine vs Cosine");
fig.xlabel("x (radians)");
fig.ylabel("y");
fig.legend({"sin(x)", "cos(x)"});
cv::Mat plot = fig.render(700, 500);
cv::imshow("Trig Functions", plot);
cv::waitKey(0);
return 0;
}
🖨️ Result
- Blue line:
sin(x) - Green points:
cos(x) - With legend, title, and axes
Very suitable for comparing algorithm outputs or data distributions!
📊 Supported Chart Types
CVPlot supports various plotting methods:
| Chart Type | Method |
|---|---|
| Line Chart | fig.plot(x, y, PlotStyle::Line) |
| Scatter Plot | fig.plot(x, y, PlotStyle::Points) |
| Bar Chart | fig.bar(labels, values) |
| Heatmap | fig.imshow(matrix) (requires cv::Mat) |
| Text Annotation | fig.text(x, y, "label") |
Example: Bar Chart
std::vector<std::string> labels = {"A", "B", "C", "D"};
std::vector<float> values = {3.5, 2.0, 4.8, 1.2};
cvplot::Figure fig;
fig.bar(labels, values, cv::Scalar(0, 128, 255));
fig.title("Bar Chart Example");
fig.ylabel("Score");
cv::Mat bar_plot = fig.render(500, 400);
cv::imshow("Bar Chart", bar_plot);
cv::waitKey(0);
⚙️ Advanced Techniques: Real-time Dynamic Plotting
CVPlot is also suitable for real-time visualization, such as sensor data streams or algorithm iteration processes.
cvplot::Figure fig;
cv::namedWindow("Real-time Plot", cv::WINDOW_AUTOSIZE);
for (int frame = 0; frame < 100; ++frame) {
std::vector<float> x, y;
float phase = frame * 0.1;
for (float t = 0; t < 4 * M_PI; t += 0.2) {
x.push_back(t);
y.push_back(std::sin(t + phase)); // Dynamic phase
}
fig.clear();
fig.plot(x, y, cvplot::PlotStyle::Line, cv::Scalar(0, 255, 255));
fig.title("Real-time Sine Wave");
cv::Mat plot = fig.render(600, 400);
cv::imshow("Real-time Plot", plot);
cv::waitKey(100); // Pause 100ms per frame
}
You will see the sine wave “moving”! Very suitable for debugging filters, control algorithms, etc.
✅ Summary of Advantages
| Advantage | Description |
|---|---|
| 📦 Lightweight | Only one header file, no compilation dependencies |
| 🧩 Easy Integration | Based on OpenCV, suitable for CV/robotics projects |
| 🎨 Aesthetic | Automatic coloring, axes, legends |
| 🚀 Fast | Quick rendering, suitable for real-time applications |
| 📚 Open Source | MIT license, free to use |
🚫 Cautions
- Depends on OpenCV: OpenCV must be installed and linked.
- Non-interactive: Cannot zoom or drag charts (suitable for display, not for complex analysis).
- Still under active development: API may change slightly, recommended to follow GitHub.
✅ Conclusion
If you are developing algorithms, robotics, or image processing in C++, and want to quickly visualize data, CVPlot is an excellent choice.
It is like the C++ version of
matplotlib.pyplot, simple, direct, and efficient.
📌 One-sentence recommendation:
“With CVPlot, I finally don’t have to draw axes by hand!”