??★,°:.☆( ̄▽ ̄)/$:.°★ ??
這篇文章主要介紹matplotlib-cpp圖表庫配置與使用。
無專精則不能成,無涉獵則不能通。——梁啟超
歡迎來到我的博客,一起學(xué)習(xí),共同進步。
喜歡的朋友可以關(guān)注一下,下次更新不迷路??
??1. 項目介紹
項目Github地址:https://github.com/lava/matplotlib-cpp
matplotlib-cpp 是一個用于 C++ 的簡易接口,它允許你在 C++ 程序中使用 Python 的 matplotlib 庫
來繪制圖表。這個庫提供了一個類似于 matplotlib 的 API
,使得在 C++ 中生成各種類型的圖表變得更加簡單和方便。
以下是 matplotlib-cpp 的一些主要特點和功能:
1.輕量級:matplotlib-cpp 是一個輕量級的庫,只包含少量的頭文件,并且沒有其他的依賴項。這使得它很容易集成到你的項目中。
2.簡單易用:matplotlib-cpp 提供了與 matplotlib 類似的函數(shù)和方法,使得在 C++ 中繪制圖表變得直觀和易于理解。你可以使用類似于 Python 的語法來創(chuàng)建圖表、設(shè)置圖表屬性和保存圖表。
3.支持多種圖表類型:matplotlib-cpp 支持繪制多種類型的圖表,包括線圖、散點圖、柱狀圖、餅圖、等高線圖等。你可以選擇適合你數(shù)據(jù)展示需求的圖表類型。
4.支持自定義設(shè)置:你可以自定義圖表的各種屬性,如標(biāo)題、標(biāo)簽、坐標(biāo)軸范圍、圖例、顏色等。這樣你可以根據(jù)具體需求來設(shè)計和美化圖表。
5.與 Python 的無縫集成:使用 matplotlib-cpp,你可以在 C++ 代碼中調(diào)用 Python 的 matplotlib 庫來生成圖表。這使得你可以利用 Python 在圖表方面豐富的生態(tài)系統(tǒng)和強大的功能來擴展你的 C++ 應(yīng)用程序。
??2. 環(huán)境配置
下面進行環(huán)境配置:
# 安裝python包
sudo apt install python3 python3-pip
pip3 install matplotlib numpy
# 源碼編譯
git clone https://github.com/lava/matplotlib-cpp.git
cd matplotlib-cpp
mkdir build && cd build
cmake ..
make
sudo make install
編譯程序:
# ubuntu18
g++ -o main main.cpp -std=c++11 -I/usr/include/python2.7 -lpython2.7
# ubuntu20
g++ -o main main.cpp -std=c++11 -I/usr/include/python3.8 -lpython3.8
??3. 使用說明
下面進行使用分析:
最簡單的示例:
#include "matplotlibcpp.h"
namespace plt = matplotlibcpp;
int main() {
plt::plot({1,3,2,4});
plt::show();
}
另一個復(fù)雜的示例,將圖表保存為圖片:
#include "matplotlibcpp.h"
#include <cmath>
namespace plt = matplotlibcpp;
int main()
{
// 數(shù)據(jù)處理
int n = 5000;
std::vector<double> x(n), y(n), z(n), w(n,2);
for(int i=0; i<n; ++i) {
x.at(i) = i*i;
y.at(i) = sin(2*M_PI*i/360.0);
z.at(i) = log(i);
}
// 設(shè)置分辨率
plt::figure_size(1200, 780);
// Plot line from given x and y data. Color is selected automatically.
plt::plot(x, y);
// Plot a red dashed line from given x and y data.
plt::plot(x, w,"r--");
// Plot a line whose name will show up as "log(x)" in the legend.
plt::named_plot("log(x)", x, z);
// 設(shè)置x軸
plt::xlim(0, 1000*1000);
// 圖表標(biāo)題
plt::title("Sample figure");
// 添加圖例
plt::legend();
// 保存為照片
plt::save("./basic.png");
}
一個三維圖形示例:
#include "matplotlibcpp.h"
namespace plt = matplotlibcpp;
int main()
{
std::vector<std::vector<double>> x, y, z;
for (double i = -5; i <= 5; i += 0.25) {
std::vector<double> x_row, y_row, z_row;
for (double j = -5; j <= 5; j += 0.25) {
x_row.push_back(i);
y_row.push_back(j);
z_row.push_back(::std::sin(::std::hypot(i, j)));
}
x.push_back(x_row);
y.push_back(y_row);
z.push_back(z_row);
}
plt::plot_surface(x, y, z);
plt::show();
}
讀取txt文件中的xy兩列數(shù)據(jù)并顯示:
#include <iostream>
#include <fstream>
#include "matplotlibcpp.h"
namespace plt = matplotlibcpp;
int main() {
std::vector<double> x_data, y_data;
double x, y;
std::ifstream file("data.txt");
if (file.is_open()) {
while (file >> x >> y) {
x_data.push_back(x);
y_data.push_back(y);
}
file.close();
} else {
std::cerr << "Failed to open file." << std::endl;
return 1;
}
// 繪制圖表
plt::plot(x_data, y_data);
plt::show();
return 0;
}
文章來源:http://www.zghlxwxcb.cn/news/detail-631263.html
以上。文章來源地址http://www.zghlxwxcb.cn/news/detail-631263.html
到了這里,關(guān)于【C++】開源:matplotlib-cpp靜態(tài)圖表庫配置與使用的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!