制圖環境:
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為中心的數據繪制一張熱圖
ax = sns.heatmap(uniform_data, center=0)
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, ax=ax)
#設置坐標字體方向
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)
#設置坐標字體方向
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, annot=True, fmt="d", ax=ax)
#設置坐標字體方向
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, annot=True, fmt="d", linewidths=.5, ax=ax)
#設置坐標字體方向
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")
#設置圖片大小
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')
#設置圖片名稱,分辨率,并保存
plt.savefig('cluster.tif',dpi = 300)
plt.show()
注:更多參數的用法請查閱官方文檔
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061
微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元

