matplotlib?雙y軸繪制及合并圖例的實(shí)現(xiàn)代碼
Matplotlib 是 Python 的繪圖庫,它能讓使用者很輕松地將數(shù)據(jù)圖形化,并且提供多樣化的輸出格式。
Matplotlib 可以用來繪制各種靜態(tài),動態(tài),交互式的圖表。
Matplotlib 是一個非常強(qiáng)大的 Python 畫圖工具,我們可以使用該工具將很多數(shù)據(jù)通過圖表的形式更直觀的呈現(xiàn)出來。
Matplotlib 可以繪制線圖、散點(diǎn)圖、等高線圖、條形圖、柱狀圖、3D 圖形、甚至是圖形動畫等等。
下面看下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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
對python 中re.sub,replace(),strip()的區(qū)別詳解
今天小編就為大家分享一篇對python 中re.sub,replace(),strip()的區(qū)別詳解,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-07-07python列表倒序的幾種方法(切片、reverse()、reversed())
本文主要介紹了python列表倒序的幾種方法(切片、reverse()、reversed()),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08Python中出現(xiàn)"No?module?named?'requests'"
這篇文章主要給大家介紹了關(guān)于Python中出現(xiàn)"No?module?named?'requests'"的解決辦法,"No?module?named?requests"是Python報(bào)錯提示,意味著你在使用某個Python程序或腳本時,沒有找到名為requests的模塊,需要的朋友可以參考下2023-11-11Python使用Matplotlib實(shí)現(xiàn)Logos設(shè)計(jì)代碼
這篇文章主要介紹了Python使用Matplotlib實(shí)現(xiàn)Logos設(shè)計(jì)代碼,具有一定借鑒價(jià)值,需要的朋友可以參考下。2017-12-12python調(diào)用函數(shù)、類和文件操作簡單實(shí)例總結(jié)
這篇文章主要介紹了python調(diào)用函數(shù)、類和文件操作,結(jié)合簡單實(shí)例形式總結(jié)分析了Python調(diào)用函數(shù)、類和文件操作的各種常見操作技巧,需要的朋友可以參考下2019-11-11python數(shù)據(jù)庫PooledDB連接池初始化使用示例
這篇文章主要為大家介紹了python數(shù)據(jù)庫PooledDB連接池初始化使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08