網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
1、matplotlibcpp
matplotlibcpp包,是一個(gè)利用C++實(shí)現(xiàn)的調(diào)用Python接口和Matplotlib實(shí)現(xiàn)繪圖的工具包。github網(wǎng)址。
2、環(huán)境
配置python需要的環(huán)境
sudo apt-get install python3-dev
3、使用
在CmakeList.txt
中添加
find_package(PythonLibs REQUIRED)
include_directories(
${PYTHON_INCLUDE_DIRS}
)
target_link_libraries(${PROJ_NAME}
${PYTHON_LIBRARIES}
)
并且將matplotlibcpp.h 文件添加到頭文件目錄即可。
4、例子
4.1 例子1
#include "matplotlibcpp.h"
#include <cmath>
namespace plt = matplotlibcpp;
int main()
{
// Prepare data.
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);
}
// Set the size of output image to 1200x780 pixels
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);
// Set x-axis to interval [0,1000000]
plt::xlim(0, 1000*1000);
// Add graph title
plt::title("Sample figure");
// Enable legend.
plt::legend();
// Save the image (file format is determined by the extension)
plt::save("./basic.png");
}
圖像會(huì)保存下來(lái),沒(méi)有顯示,需要plt.show()
才會(huì)顯示圖像。
4.2 例子2 動(dòng)態(tài)顯示圖片
#define _USE_MATH_DEFINES
#include <cmath>
#include "../matplotlibcpp.h"
namespace plt = matplotlibcpp;
int main()
{
int n = 1000;
std::vector<double> x, y, z;
for(int i=0; i<n; i++) {
x.push_back(i*i);
y.push_back(sin(2*M_PI*i/360.0));
z.push_back(log(i));
if (i % 10 == 0) {
// Clear previous plot
plt::clf();
// Plot line from given x and y data. Color is selected automatically.
plt::plot(x, y);
// Plot a line whose name will show up as "log(x)" in the legend.
plt::named_plot("log(x)", x, z);
// Set x-axis to interval [0,1000000]
plt::xlim(0, n*n);
// Add graph title
plt::title("Sample figure");
// Enable legend.
plt::legend();
// Display plot continuously
plt::pause(0.01);
}
}
}
注意:這個(gè)畫圖會(huì)占用線程,如果想邊執(zhí)行程序邊畫圖,最好開(kāi)多線程,否則程序會(huì)卡在畫圖這,不會(huì)往后執(zhí)行。
原文鏈接:https://blog.csdn.net/Kevin_Xie86/article/details/125692259
相關(guān)推薦
- 2022-02-27 springboot引入依賴lombok但是@Data(lombok的一個(gè)注解)仍然爆紅
- 2022-02-22 解決:DevTools failed to load SourceMap:... net::ERR_
- 2022-09-03 利用python合并csv文件的方式實(shí)例_python
- 2023-01-18 python中的參數(shù)類型匹配提醒_python
- 2022-05-23 如何在Python?中使用?join()?函數(shù)把列表拼接成一個(gè)字符串_python
- 2021-11-15 Linux網(wǎng)絡(luò)設(shè)置之基礎(chǔ)操作命令詳解_Linux
- 2022-10-15 Python基礎(chǔ)學(xué)習(xí)之函數(shù)和代碼復(fù)用詳解_python
- 2022-05-12 el-table滾動(dòng)懶加載,顯示加載狀態(tài)
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過(guò)濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支