一、安裝
二、庫簡單例子使用
三、CMakeLists.txt文件
四、庫的相關繪製樣式
‘ ’4.1線條相關屬性標記設置
‘ ’ 4.2線條標記
‘ ’4.3顏色縮寫
五、庫的更多使用介紹
六、c++其它可視化庫
matplotlibcpp是c++的庫,matplotlibcpp庫和python中的matplotlib庫一樣使用,它是python中的matplotlib庫的封裝,它依賴python中的matplotlib庫
一、安裝
matplotlibcpp庫依賴python中的matplotlib庫,所以需要安裝上python中的matplotlib庫即可
sudo apt-get install python-matplotlib python-numpy python2.7-dev
然後到GitHub上下載c++的頭文件,ubuntu下使用git clone 下載就行,下載下來的文件可以放到任意位置,該位置的路徑需要在cmak文件中被包含。
git clone https://github.com/lava/matplotlib-cpp
在自己工程下對應的的CMakeLists.txt文件中增加的內容在下面統一介紹。
二、庫簡單例子使用
如果使用vscode編程需要在c_cpp_properties.json文件下增加matplotlibcpp庫所在路徑。 github中有更多例子
最簡單的基本使用方法:
例子一:
#include "matplotlibcpp.h"
namespace plt = matplotlibcpp;
int main() {
plt::plot({1,3,2,4});
plt::pause(2); //最好加上該句,否則有時候顯示不了圖像,或者圖像顯示很慢
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();
plt::plot(t, x);
plt::title("AN ORDINARY SIN WAVE");
plt::save("xkcd.png");
}
三、CMakeLists.txt文件
如果 CMake 版本<= 3.11:
##python
find_package(PythonLibs 2.7)
#include_directories(${PYTHON_INCLUDE_DIRS}) #使用這方式可以替代下面這行
target_include_directories(myproject PRIVATE ${PYTHON_INCLUDE_DIRS})
##matplotlibcpp
include_directories("/home/jack/slam_book/thirdparty/matplotlib-cpp/") #路徑需要根據自己路徑做調整
target_link_libraries(myproject ${PYTHON_LIBRARIES})
CMake 3.12 以後版本 推薦使用下面方式:
##python
find_package(Python2 COMPONENTS Development NumPy)
#include_directories(${PYTHON_INCLUDE_DIRS}) #使用這方式可以替代下面這行
target_include_directories(myproject PRIVATE ${Python2_INCLUDE_DIRS} ${Python2_NumPy_INCLUDE_DIRS})
##matplotlibcpp 下面路徑需要根據自己路徑做調整
include_directories("/home/jack/slam_book/thirdparty/matplotlib-cpp/")
target_link_libraries(myproject Python2::Python Python2::NumPy)
四、庫的相關繪製樣式
4.1線條相關屬性標記設置:
線條風格linestyle或ls 描述
‘-‘ 實線
‘:’ 虛線
‘–’ 破折線
‘None‘、‘‘,’’ 什麼都不畫
‘-.’ 點劃線
4.2線條標記
標記maker 描述
‘o’ 圓圈
‘.’ 點
‘D’ 菱形
‘s’ 正方形
‘h’ 六邊形1
‘*’ 星號
‘H’ 六邊形2
‘d’ 小菱形
‘_’ 水平線
‘v’ 一角朝下的三角形
‘8’ 八邊形
‘<’ 一角朝左的三角形
‘p’ 五邊形
‘>’ 一角朝右的三角形
‘,’ 像素
‘^’ 一角朝上的三角形
‘+’ 加號
‘\ ‘ 豎線
‘None’,’’,’ ‘ 無
‘x’ X
4.3顏色縮寫
字符 顏色
‘b’ 藍色
‘g’ 綠色
‘r’ 紅色
‘y’ 黃色
‘c’ 青色
‘k’ 黑色
‘m’ 洋紅色
‘w’ 白色
五、庫的更多使用介紹
繪製各種各樣的散點圖:https://www.cnblogs.com/sench/p/9522627.html
Matplotlib庫學習:http://blog.itpub.net/69908432/viewspace-2660537/
python Matplotlib 可視化總結歸納(一) 折線圖、散點圖及其座標軸屬性設置:
https://blog.csdn.net/haikuotiankong7/article/details/90547084
python Matplotlib 可視化總結歸納(二) 繪製多個圖像單獨顯示&多個函數繪製於一張圖:
https://blog.csdn.net/haikuotiankong7/article/details/90551841
六、c++其它可視化庫
6.1、 madplotlib : https://github.com/madplotlib/madplotlib 注意是 ma‘d’plot 不是ma‘t’plot
6.2、 MathGL 庫 :
6.3、 root 庫
6.4、 Gnuplot 庫
6.5、QtChart以及與Qt相關的 庫
6.6、 ChartDirector 庫
注:6.2–6.6的庫相關介紹可在這博客中觀看:https://blog.csdn.net/chyuanrufeng/article/details/102808715