python必備庫(kù)Matplotlib畫(huà)圖神器
前言:
Matplotlib
通常與 NumPy、Pandas 一起使用,是數(shù)據(jù)分析中不可或缺的重要工具之一。
Matplotlib
是 Python 中類(lèi)似 MATLAB 的繪圖工具,如果您熟悉 MATLAB,那么可以很快的熟悉它。Matplotlib 提供了一套面向?qū)ο罄L圖的 API,它可以輕松地配合 Python GUI 工具包(比如 PyQt,WxPython、Tkinter)在應(yīng)用程序中嵌入圖形。與此同時(shí),它也支持以腳本的形式在 Python、IPython Shell、Jupyter Notebook 以及 Web 應(yīng)用的服務(wù)器中使用。
官網(wǎng)地址:
可以看看docs
官網(wǎng)就相當(dāng)詳細(xì)了,可以直接參考官網(wǎng)。
1.安裝方法
pip安裝:
pip3 install matplotlib -i https://pypi.tuna.tsinghua.edu.cn/simple
conda安裝:
conda install matplotlib
測(cè)試是否成功:
import numpy as np? from matplotlib import pyplot as plt? ? x = np.arange(1,11)? y = ?2 ?* x + ?5? plt.title("Matplotlib demo")? plt.xlabel("x axis caption")? plt.ylabel("y axis caption")? plt.plot(x,y)? plt.show()
成功出現(xiàn)下圖就可以動(dòng)手改造了。
2.用好官網(wǎng)的例子
最簡(jiǎn)單的應(yīng)用-折線圖
fig, ax = plt.subplots() ?# Create a figure containing a single axes. ax.plot([1, 2, 3, 4], [1, 4, 2, 3]); ?# Plot some data on the axes.
添加注釋的方法
fig, ax = plt.subplots(figsize=(5, 2.7)) t = np.arange(0.0, 5.0, 0.01) s = np.cos(2 * np.pi * t) line, = ax.plot(t, s, lw=2) ax.annotate('local max', xy=(2, 1), xytext=(3, 1.5), ? ? ? ? ? ? arrowprops=dict(facecolor='black', shrink=0.05)) ax.set_ylim(-2, 2);
柱狀圖-Bar Label
import matplotlib.pyplot as plt import numpy as np N = 5 menMeans = (20, 35, 30, 35, -27) womenMeans = (25, 32, 34, 20, -25) menStd = (2, 3, 4, 1, 2) womenStd = (3, 5, 2, 3, 3) ind = np.arange(N) ? ?# the x locations for the groups width = 0.35 ? ? ? # the width of the bars: can also be len(x) sequence fig, ax = plt.subplots() p1 = ax.bar(ind, menMeans, width, yerr=menStd, label='Men') p2 = ax.bar(ind, womenMeans, width, ? ? ? ? ? ? bottom=menMeans, yerr=womenStd, label='Women') ax.axhline(0, color='grey', linewidth=0.8) ax.set_ylabel('Scores') ax.set_title('Scores by group and gender') ax.set_xticks(ind, labels=['G1', 'G2', 'G3', 'G4', 'G5']) ax.legend() # Label with label_type 'center' instead of the default 'edge' ax.bar_label(p1, label_type='center') ax.bar_label(p2, label_type='center') ax.bar_label(p2) plt.show()
正常run會(huì)出現(xiàn)下圖:
折線圖之CSD
計(jì)算兩個(gè)信號(hào)的交叉譜密度Compute the cross spectral density of two signals
import numpy as np import matplotlib.pyplot as plt fig, (ax1, ax2) = plt.subplots(2, 1) # make a little extra space between the subplots fig.subplots_adjust(hspace=0.5) dt = 0.01 t = np.arange(0, 30, dt) # Fixing random state for reproducibility np.random.seed(19680801) nse1 = np.random.randn(len(t)) ? ? ? ? ? ? ? ? # white noise 1 nse2 = np.random.randn(len(t)) ? ? ? ? ? ? ? ? # white noise 2 r = np.exp(-t / 0.05) cnse1 = np.convolve(nse1, r, mode='same') * dt ? # colored noise 1 cnse2 = np.convolve(nse2, r, mode='same') * dt ? # colored noise 2 # two signals with a coherent part and a random part s1 = 0.01 * np.sin(2 * np.pi * 10 * t) + cnse1 s2 = 0.01 * np.sin(2 * np.pi * 10 * t) + cnse2 ax1.plot(t, s1, t, s2) ax1.set_xlim(0, 5) ax1.set_xlabel('time') ax1.set_ylabel('s1 and s2') ax1.grid(True) cxy, f = ax2.csd(s1, s2, 256, 1. / dt) ax2.set_ylabel('CSD (db)') plt.show()
到此這篇關(guān)于python必備庫(kù)Matplotlib畫(huà)圖神器的文章就介紹到這了,更多相關(guān)Matplotlib畫(huà)圖神器內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python PyTorch實(shí)現(xiàn)Timer計(jì)時(shí)器
這篇文章主要為大家詳細(xì)介紹了Python PyTorch如何實(shí)現(xiàn)簡(jiǎn)單的Timer計(jì)時(shí)器,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-08-08Python中動(dòng)態(tài)檢測(cè)編碼chardet的使用教程
最近利用python抓取一些網(wǎng)上的數(shù)據(jù),遇到了編碼的問(wèn)題。非常頭痛,幸運(yùn)的是找到了解決的方法,下面這篇文章主要跟大家介紹了關(guān)于Python中動(dòng)態(tài)檢測(cè)編碼chardet的使用方法,需要的朋友可以參考借鑒,下面來(lái)一起看看吧。2017-07-07pandas數(shù)據(jù)清洗(缺失值和重復(fù)值的處理)
這篇文章主要介紹了pandas數(shù)據(jù)清洗(缺失值和重復(fù)值的處理),pandas對(duì)大數(shù)據(jù)有很多便捷的清洗用法,尤其針對(duì)缺失值和重復(fù)值,詳細(xì)介紹感興趣的小伙伴可以參考下面文章內(nèi)容2022-08-08Python操作遠(yuǎn)程服務(wù)器 paramiko模塊詳細(xì)介紹
這篇文章主要介紹了Python操作遠(yuǎn)程服務(wù)器 paramiko模塊詳細(xì)介紹,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-08-08通過(guò)pycharm的database設(shè)置進(jìn)行數(shù)據(jù)庫(kù)的可視化方式
這篇文章主要介紹了通過(guò)pycharm的database設(shè)置進(jìn)行數(shù)據(jù)庫(kù)的可視化方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-09-09python Pexpect 實(shí)現(xiàn)輸密碼 scp 拷貝的方法
今天小編就為大家分享一篇python Pexpect 實(shí)現(xiàn)輸密碼 scp 拷貝的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-01-01Python 獲取指定文件夾下的目錄和文件的實(shí)現(xiàn)
這篇文章主要介紹了Python 獲取指定文件夾下的目錄和文件的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08pytorch 模型的train模式與eval模式實(shí)例
今天小編就為大家分享一篇pytorch 模型的train模式與eval模式實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-02-02