Python-Seaborn熱圖繪制的實(shí)現(xiàn)方法
制圖環(huán)境:
pycharm
python-3.6
Seaborn-0.8
熱圖
import numpy as np import seaborn as sns import matplotlib.pyplot as plt sns.set() np.random.seed(0) uniform_data = np.random.rand(10, 12) ax = sns.heatmap(uniform_data) plt.show()
# 改變顏色映射的值范圍 ax = sns.heatmap(uniform_data, vmin=0, vmax=1) plt.show()
uniform_data = np.random.randn(10, 12) #為以0為中心的數(shù)據(jù)繪制一張熱圖 ax = sns.heatmap(uniform_data, center=0) plt.show()
import matplotlib.pyplot as plt import seaborn as sns sns.set() #用行和列標(biāo)簽繪制 flights_long = sns.load_dataset("flights") flights = flights_long.pivot("month", "year", "passengers") # 繪制x-y-z的熱力圖,比如 年-月-銷量 的熱力圖 f, ax = plt.subplots(figsize=(9, 6)) sns.heatmap(flights, ax=ax) #設(shè)置坐標(biāo)字體方向 label_y = ax.get_yticklabels() plt.setp(label_y, rotation=360, horizontalalignment='right') label_x = ax.get_xticklabels() plt.setp(label_x, rotation=45, horizontalalignment='right') plt.show()
import matplotlib.pyplot as plt import seaborn as sns sns.set() flights_long = sns.load_dataset("flights") flights = flights_long.pivot("month", "year", "passengers") # 繪制x-y-z的熱力圖,比如 年-月-銷量 的熱力圖 f, ax = plt.subplots(figsize=(9, 6)) #使用不同的顏色 sns.heatmap(flights, fmt="d",cmap='YlGnBu', ax=ax) #設(shè)置坐標(biāo)字體方向 label_y = ax.get_yticklabels() plt.setp(label_y, rotation=360, horizontalalignment='right') label_x = ax.get_xticklabels() plt.setp(label_x, rotation=45, horizontalalignment='right') plt.show()
注釋熱圖
import matplotlib.pyplot as plt import seaborn as sns sns.set() flights_long = sns.load_dataset("flights") flights = flights_long.pivot("month", "year", "passengers") # 繪制x-y-z的熱力圖,比如 年-月-銷量 的熱力圖 f, ax = plt.subplots(figsize=(9, 6)) #繪制熱力圖,還要將數(shù)值寫到熱力圖上 sns.heatmap(flights, annot=True, fmt="d", ax=ax) #設(shè)置坐標(biāo)字體方向 label_y = ax.get_yticklabels() plt.setp(label_y, rotation=360, horizontalalignment='right') label_x = ax.get_xticklabels() plt.setp(label_x, rotation=45, horizontalalignment='right') plt.show()
import matplotlib.pyplot as plt import seaborn as sns sns.set() flights_long = sns.load_dataset("flights") flights = flights_long.pivot("month", "year", "passengers") # 繪制x-y-z的熱力圖,比如 年-月-銷量 的熱力圖 f, ax = plt.subplots(figsize=(9, 6)) #繪制熱力圖,還要將數(shù)值寫到熱力圖上 #每個網(wǎng)格上用線隔開 sns.heatmap(flights, annot=True, fmt="d", linewidths=.5, ax=ax) #設(shè)置坐標(biāo)字體方向 label_y = ax.get_yticklabels() plt.setp(label_y, rotation=360, horizontalalignment='right') label_x = ax.get_xticklabels() plt.setp(label_x, rotation=45, horizontalalignment='right') plt.show()
聚類熱圖
import matplotlib.pyplot as plt import seaborn as sns sns.set() flights_long = sns.load_dataset("flights") flights = flights_long.pivot("month", "year", "passengers") # 繪制x-y-z的熱力圖,比如 年-月-銷量 的聚類熱圖 g= sns.clustermap(flights, fmt="d",cmap='YlGnBu') ax = g.ax_heatmap label_y = ax.get_yticklabels() plt.setp(label_y, rotation=360, horizontalalignment='left') plt.show()
import matplotlib.pyplot as plt import seaborn as sns sns.set(color_codes=True) iris = sns.load_dataset("iris") species = iris.pop("species") #設(shè)置圖片大小 g= sns.clustermap(iris, fmt="d",cmap='YlGnBu',figsize=(6,9)) ax = g.ax_heatmap label_y = ax.get_yticklabels() plt.setp(label_y, rotation=360, horizontalalignment='left') #設(shè)置圖片名稱,分辨率,并保存 plt.savefig('cluster.tif',dpi = 300) plt.show()
注:更多參數(shù)的用法請查閱官方文檔
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Python幫你解決手機(jī)qq微信內(nèi)存占用太多問題
你有沒有發(fā)現(xiàn)以前16G內(nèi)存也可以裝幾個游戲玩,現(xiàn)在128G的卻日常使用都不夠了?更不用說裝什么游戲,這其實(shí)是軟件內(nèi)存占用過多導(dǎo)致的,今天我們用python來清理下2022-02-02解析numpy中的iscomplex方法及實(shí)際應(yīng)用
NumPy 的 iscomplex 方法為檢查數(shù)組中的元素是否為復(fù)數(shù)提供了一種高效且易于使用的接口,本文介紹了 iscomplex 方法的基本概念、使用方法以及它在解決實(shí)際問題中的應(yīng)用,需要的朋友可以參考下2024-06-06Python利用帶權(quán)重隨機(jī)數(shù)解決抽獎和游戲爆裝備問題
帶權(quán)重隨機(jī)數(shù)即是隨機(jī)數(shù)各個區(qū)間段被抽中的概率根據(jù)權(quán)重而不同,這里我們就來看一下Python利用帶權(quán)重隨機(jī)數(shù)解決抽獎和游戲爆裝備問題的方法,首先還是來進(jìn)一步解釋帶權(quán)隨機(jī)數(shù):2016-06-06Python實(shí)現(xiàn)隨機(jī)生成算術(shù)題的示例代碼
這篇文章主要為大家詳細(xì)介紹了如何利用Python實(shí)現(xiàn)隨機(jī)生成算術(shù)題的功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-04-04Python實(shí)現(xiàn)希爾伯特變換(Hilbert transform)的示例代碼
希爾伯特變換(Hilbert transform)是一個對函數(shù)產(chǎn)生定義域相同的函數(shù)的線性算子,而且希爾伯特變換在信號處理中很重要,所以本文和大家分享了Python實(shí)現(xiàn)希爾伯特變換的代碼,需要的可以參考一下2023-04-04詳解python中的lambda與sorted函數(shù)
這篇文章主要介紹了python中的lambda與sorted函數(shù)的相關(guān)資料,幫助大家更好的理解和學(xué)習(xí)python,感興趣的朋友可以了解下2020-09-09Python使用cx_Oracle模塊將oracle中數(shù)據(jù)導(dǎo)出到csv文件的方法
這篇文章主要介紹了Python使用cx_Oracle模塊將oracle中數(shù)據(jù)導(dǎo)出到csv文件的方法,涉及Python中cx_Oracle模塊與csv模塊操作Oracle數(shù)據(jù)庫及csv文件的相關(guān)技巧,需要的朋友可以參考下2015-05-05