Matplotlib-cpp是一個(gè)用于在C++中繪制圖表的開(kāi)源庫(kù)。它提供了與Python的Matplotlib庫(kù)類似的功能,使得在C++環(huán)境下進(jìn)行數(shù)據(jù)可視化變得更加便捷?;贛atplotlib-cpp,我們可以使用各種繪圖函數(shù)和樣式選項(xiàng)來(lái)創(chuàng)建各種類型的圖表,包括折線圖、散點(diǎn)圖、柱狀圖等。它與C++的語(yǔ)法和數(shù)據(jù)結(jié)構(gòu)緊密結(jié)合,方便了在C++項(xiàng)目中進(jìn)行圖表繪制和數(shù)據(jù)分析。
1. 安裝
下載matplotlib-cpp
git?clone?https://github.com/lava/matplotlib-cpp.git
2. 基本功能
matplotlib-cpp 主要提供了以下幾種功能:
-
基本繪圖:matplotlib-cpp 支持許多基本的 2D 圖形,包括線圖、散點(diǎn)圖、條形圖、直方圖、餅圖等。
-
子圖:可以在一個(gè)畫(huà)布上創(chuàng)建多個(gè)子圖,每個(gè)子圖可以獨(dú)立繪制。
-
顏色、標(biāo)記和線型:可以自定義每個(gè)圖形的顏色、標(biāo)記(數(shù)據(jù)點(diǎn)的形狀)和線型(實(shí)線、虛線等)。
-
圖例和標(biāo)題:可以為每個(gè)圖形添加圖例和標(biāo)題,以解釋圖形的含義。
-
坐標(biāo)軸設(shè)置:可以自定義坐標(biāo)軸的范圍、刻度和標(biāo)簽。
-
網(wǎng)格線:可以添加網(wǎng)格線,以便于觀察數(shù)據(jù)。
-
文本和標(biāo)注:可以在圖形上添加文本和標(biāo)注,以強(qiáng)調(diào)特定的數(shù)據(jù)點(diǎn)或區(qū)域。
-
保存和顯示:可以將圖形保存為各種格式的文件,或在窗口中直接顯示。
3. 示例
#include "matplotlibcpp.h"
#include <cmath>
namespace plt = matplotlibcpp;
int main()
{
// 準(zhǔn)備數(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è)置輸出圖像的大小為1200x780像素
plt::figure_size(1200, 780);
// 繪制給定x和y數(shù)據(jù)的折線圖,顏色自動(dòng)選擇
plt::plot(x, y);
// 繪制給定x和y數(shù)據(jù)的紅色虛線
plt::plot(x, w, "r--");
// 繪制一條線,其名稱將出現(xiàn)在圖例中為"log(x)"
plt::named_plot("log(x)", x, z);
// 設(shè)置x軸的范圍為[0,1000000]
plt::xlim(0, 1000*1000);
// 添加圖表標(biāo)題
plt::title("Sample figure");
// 啟用圖例
plt::legend();
// 保存圖像(文件格式由擴(kuò)展名確定)
plt::save("./basic.png");
return 0;
}
#include <cmath>
#include "matplotlibcpp.h"
using namespace std;
namespace plt = matplotlibcpp;
int main()
{
// 準(zhǔn)備數(shù)據(jù)
int n = 5000; // 數(shù)據(jù)點(diǎn)個(gè)數(shù)
vector<double> x(n),y(n);
for(int i=0; i<n; ++i) {
double t = 2*M_PI*i/n;
x.at(i) = 16*sin(t)*sin(t)*sin(t);
y.at(i) = 13*cos(t) - 5*cos(2*t) - 2*cos(3*t) - cos(4*t);
}
// plot() 函數(shù)接受任意數(shù)量的 (x, y, format) 三元組
// x 必須是可迭代的(即提供 begin(x) 和 end(x) 函數(shù)),
// y 可以是可調(diào)用的(提供 operator() const 函數(shù))或者可迭代的
plt::plot(x, y, "r-", x, [](double d) { return 12.5+abs(sin(d)); }, "k-");
// 顯示圖表
plt::show();
}
#include "matplotlibcpp.h"
#include <vector>
#include <cmath>
namespace plt = matplotlibcpp;
int main() {
std::vector<double> t(1000);
std::vector<double> x(t.size());
for(size_t i = 0; i < t.size(); i++) {
t[i] = i / 100.0;
x[i] = sin(2.0 * M_PI * 1.0 * t[i]);
}
plt::xkcd(); // 應(yīng)用xkcd風(fēng)格的繪圖
plt::plot(t, x);
plt::title("AN ORDINARY SIN WAVE"); // 設(shè)置圖表標(biāo)題
plt::save("xkcd.png"); // 保存圖像為"xkcd.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();
}
參考文獻(xiàn)
C++圖表繪制的最佳選擇:高效而強(qiáng)大文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-608359.html
C++第三方開(kāi)發(fā)庫(kù) matplotlib-cpp文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-608359.html
到了這里,關(guān)于C++第三方開(kāi)發(fā)庫(kù)matplotlib-cpp的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!