热力图 heatmap
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
from pylab import *
import matplotlib.cm as cm
from matplotlib.colors import LogNorm
一列数画图
df = df.pivot("hour", "minute", "rate")
ax = sns.heatmap(df,annot= False,fmt='d', linewidths=.5, cmap='YlGnBu')
plt.show()
方阵(xy任意标注名称问题未解决)
data = np.array(df)
#sns.heatmap(data,annot= False,annot_kws={'size':9,'weight':'bold', 'color':'w'},fmt='.2f')#true 显示数值
sns.heatmap(data,annot= False,fmt='d', linewidths=.5, cmap='YlGnBu')
plt.show()
方阵
df = pd.read_csv(r'D:任选的一天1120方阵.csv',header = None)
df=df.astype(float)
#sns.heatmap(df)
sns.heatmap(df,cmap='Blues',center=-0.8)#颜色,中心对齐值,
plt.show()
数据清洗
excel可做的处理
分列(根据分隔符 / 同格式时 按长度分)
选择指定数据所在的行,形成新的dataframe
newdf1 = df[df[0] == 'A1.GT.24538' ]
print(newdf1)
去除重复项
newdf1 = newdf1.drop_duplicates()
缺失值查找(以日期为例)
for i in range (19,31):
i=str(i)
print(i)
#date = date.replace(i,i+1)
datei=''
date=('2019-11-', i,'T')
datei = datei.join(date)
#print(datei)
checki = newdf1.loc[newdf1[1] ==datei, :]
print (checki.shape)
把日期分成年月日
并按礼拜分为工作日和非工作日
# 日期
date = df.values[:,1]
hour_minutes = df.values[:,2]
rate = df.values[:, 8]
year = []
month = []
day = []
hour = []
minutes = []
weekday = []
up5 = []
up10 = []
up15 = []
for i in range(3, len(date)):
year.append(int(date[i][0:4]))
month.append(int(date[i][5:7]))
day.append(int(date[i][8:10]))
# print(month)
for i in range(0, len(date)-3):
y = year[i]
m = month[i]
d = day[i]
lis = ['1', '2', '3', '4', '5', '6', '7', ]
dic = dict(enumerate(lis))
#if y.isdigit() and m.isdigit() and d.isdigit() and 1 <= int(m) <= 12 and 1 <= int(d) <= 31:
w = datetime.date(int(y), int(m), int(d))
weekday.append(dic[w.weekday()])
#print(len(weekday))
print(len(weekday))
weekday1 = []
weekend1 = []
weekday = list(map(int, weekday))
#print(type(weekday[1]))
for i in range(len(weekday)):
if (weekday[i] == 6) or (weekday[i] == 7):
#if weekday[i] == 6 :
weekend1.append(df.values[i,:])
else:
weekday1.append(df.values[i,:])
转为dataframe格式
转置
导出
weekday2 = pd.DataFrame([weekday1])
weekday2 = weekday2.T
weekday2.to_csv(r'D:\seu-monash\卢高速\weekday.csv')
本文介绍如何使用Python的seaborn库绘制热力图,并展示了几种不同的数据处理和可视化方式,包括数据清洗、数据筛选及热力图的具体配置。

被折叠的 条评论
为什么被折叠?



