Seaborn绘制热力图
程序员文章站
2022-07-14 10:06:19
...
import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib as mpl
import matplotlib.pyplot as plt
df1=pd.read_excel('D:/CW/df.xlsx',sheetname=0)#读入数据,注意sheetname是从0开始
df2=pd.read_excel('D:/CW/df.xlsx',sheetname=1)
df3=pd.read_excel('D:/CW/df.xlsx',sheetname=2)
df1_mat=df1[:24].as_matrix()#将数据转换为矩阵,为画直方图做准备
df2_mat=df2[:24].as_matrix()
df3_mat=df3[:24].as_matrix()
a=[]
b=[]
c=[]
for i in range(24): #将3张表的数据存入List
for j in range(9):
if df1_mat[i][j]<5.5:
a.append(df1_mat[i][j])
if df2_mat[i][j]<5.5:
b.append(df2_mat[i][j])
if df3_mat[i][j]<5.5:
c.append(df3_mat[i][j])
ax1 = sns.distplot(a, rug=True, hist=True)
ax1.set_title('Humidity deviation distribution diagram at t=20 degrees Celsius ')
ax1.set_xlabel('Humidity deviationn')
ax1.set_ylabel('Probability')
plt.show()
ax2 = sns.distplot(b, rug=True, hist=True)
ax2.set_title('Humidity deviation distribution diagram at t=0 degrees Celsius ')
ax2.set_xlabel('Humidity deviationn')
ax2.set_ylabel('Probability')
plt.show()
ax3 = sns.distplot(c, rug=True, hist=True)
ax3.set_title('Humidity deviation distribution diagram at t=-30 degrees Celsius ')
ax3.set_xlabel('Humidity deviationn')
ax3.set_ylabel('Probability')
plt.show()
df1_=pd.read_excel('D:/CW/df_.xlsx',sheetname=0)
df2_=pd.read_excel('D:/CW/df_.xlsx',sheetname=1)
df3_=pd.read_excel('D:/CW/df_.xlsx',sheetname=2)
axh1 = sns.heatmap(df1_)#Seaborn对Pandas的对接比较好,可以直接处理DataFrame
axh1.set_title('Humidity error diagram at t=20 degrees Celsius ')
axh1.set_xlabel('Relative Humidity')
axh1.set_ylabel('Serial Number')
plt.show()
axh2 = sns.heatmap(df2_)
axh2.set_title('Humidity error diagram at t=0 degrees Celsius ')
axh2.set_xlabel('Relative Humidity')
axh2.set_ylabel('Serial Number')
plt.show()
axh3 = sns.heatmap(df3_)
axh3.set_title('Humidity error diagram at t=-30 degrees Celsius ')
axh3.set_xlabel('Relative Humidity')
axh3.set_ylabel('Serial Number')
plt.show()