matplotlib?雙y軸繪制及合并圖例的實(shí)現(xiàn)代碼
Matplotlib 是 Python 的繪圖庫(kù),它能讓使用者很輕松地將數(shù)據(jù)圖形化,并且提供多樣化的輸出格式。
Matplotlib 可以用來(lái)繪制各種靜態(tài),動(dòng)態(tài),交互式的圖表。
Matplotlib 是一個(gè)非常強(qiáng)大的 Python 畫(huà)圖工具,我們可以使用該工具將很多數(shù)據(jù)通過(guò)圖表的形式更直觀的呈現(xiàn)出來(lái)。
Matplotlib 可以繪制線圖、散點(diǎn)圖、等高線圖、條形圖、柱狀圖、3D 圖形、甚至是圖形動(dòng)畫(huà)等等。
下面看下matplotlib 雙y軸繪制及合并圖例。
1.雙y軸繪制 關(guān)鍵函數(shù):twinx()
# -*- coding: utf-8 -*-
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import rc
rc('mathtext', default='regular')
time = np.arange(10)
temp = np.random.random(10)*30
Swdown = np.random.random(10)*100-10
Rn = np.random.random(10)*100-10
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(time, Swdown, '-', label = 'Swdown')
ax.plot(time, Rn, '-', label = 'Rn')
ax2 = ax.twinx()
ax2.plot(time, temp, '-r', label = 'temp')
ax.legend(loc=0)
ax.grid()
ax.set_xlabel("Time (h)")
ax.set_ylabel(r"Radiation ($MJ\,m^{-2}\,d^{-1}$)")
ax2.set_ylabel(r"Temperature ($^circ$C)")
ax2.set_ylim(0, 35)
ax.set_ylim(-20,100)
ax2.legend(loc=0)
合并圖例
# -*- coding: utf-8 -*-
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import rc
rc('mathtext', default='regular')
time = np.arange(10)
temp = np.random.random(10)*30
Swdown = np.random.random(10)*100-10
Rn = np.random.random(10)*100-10
fig = plt.figure()
ax = fig.add_subplot(111)
lns1 = ax.plot(time, Swdown, '-', label = 'Swdown')
lns2 = ax.plot(time, Rn, '-', label = 'Rn')
ax2 = ax.twinx()
lns3 = ax2.plot(time, temp, '-r', label = 'temp')
# added these three lines
lns = lns1+lns2+lns3
labs = [l.get_label() for l in lns]
ax.legend(lns, labs, loc=0)
ax.grid()
ax.set_xlabel("Time (h)")
ax.set_ylabel(r"Radiation ($MJ\,m^{-2}\,d^{-1}$)")
ax2.set_ylabel(r"Temperature ($^circ$C)")
ax2.set_ylim(0, 35)
ax.set_ylim(-20,100)

使用Figure.legend()
# -*- coding: utf-8 -*-
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0,10)
y = np.linspace(0,10)
z = np.sin(x/3)**2*98
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(x,y, '-', label = 'Quantity 1')
ax2 = ax.twinx()
ax2.plot(x,z, '-r', label = 'Quantity 2')
fig.legend(loc=1, bbox_to_anchor=(1,1), bbox_transform=ax.transAxes)
ax.set_xlabel("x [units]")
ax.set_ylabel(r"Quantity 1")
ax2.set_ylabel(r"Quantity 2")
到此這篇關(guān)于matplotlib 雙y軸繪制及合并圖例的文章就介紹到這了,更多相關(guān)matplotlib 雙y軸內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python數(shù)據(jù)分析Numpy中常用相關(guān)性函數(shù)
這篇文章主要為大家介紹了Python數(shù)據(jù)分析Numpy中常用相關(guān)性函數(shù)講解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06
Python Spyder 調(diào)出縮進(jìn)對(duì)齊線的操作
這篇文章主要介紹了Python Spyder 調(diào)出縮進(jìn)對(duì)齊線的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-02-02
Django配置Mysql數(shù)據(jù)庫(kù)連接的實(shí)現(xiàn)
本文主要介紹了Django配置Mysql數(shù)據(jù)庫(kù)連接的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-03-03
在pandas中遍歷DataFrame行的實(shí)現(xiàn)方法
這篇文章主要介紹了在pandas中遍歷DataFrame行的實(shí)現(xiàn)方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10
Python3爬蟲(chóng)之自動(dòng)查詢(xún)天氣并實(shí)現(xiàn)語(yǔ)音播報(bào)
這篇文章主要介紹了Python3爬蟲(chóng)之自動(dòng)查詢(xún)天氣并實(shí)現(xiàn)語(yǔ)音播報(bào),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-02-02
python實(shí)現(xiàn)跨excel的工作表sheet之間的復(fù)制方法
今天小編就為大家分享一篇python實(shí)現(xiàn)跨excel的工作表sheet之間的復(fù)制方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-05-05
python實(shí)現(xiàn)BP神經(jīng)網(wǎng)絡(luò)回歸預(yù)測(cè)模型
這篇文章主要介紹了python實(shí)現(xiàn)BP神經(jīng)網(wǎng)絡(luò)回歸預(yù)測(cè)模型,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08
PyTorch數(shù)據(jù)讀取的實(shí)現(xiàn)示例
這篇文章主要介紹了PyTorch數(shù)據(jù)讀取的實(shí)現(xiàn)示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03

