# -*- coding: UTF-8 -*-
__author__ = 'zy'
__time__ = '2020/3/15 15:40'
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.pyplot as plt
# 在我的 notebook 里,要设置下面两行才能显示中文
plt.rcParams['font.family'] = ['sans-serif']
# 如果是在 PyCharm 里,只要下面一行,上面的一行可以删除
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus']=False # 用来正常显示负号
import numpy
def get_matrx(day_time):
df=pd.read_csv(day_time, engine='python', sep=',', encoding="utf_8_sig")
ss=['咸宁市',
'黄石市',
'十堰市',
'武汉市',
'鄂州市',
'荆州市',
'仙桃市',
'恩施土家族苗族自治州',
'天门市',
'潜江市',
'黄冈市',
'襄阳市',
'宜昌市',
'随州市',
'神农架林区',
'孝感市',
'荆门市']
moveout=df[df['mode']=="move_out"]
movein=df[df['mode']=="move_in"]
tmp=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
dict={}
for i in ss:
dict[i]=tmp
result=pd.DataFrame(dict,index=ss,dtype='double')
for index,row in moveout.iterrows():
# print(row)
result.at[row['city1'],row['city2']]=row['value'] #iloc
lst=pd.DataFrame(dict,index=ss,dtype='double')
for index,row in result.iterrows():
for i,value in enumerate(ss):
# if i<ss.index(index):
lst.at[index,value]=((result.at[index,value])/(result.at[index,value]+result.at[value,index]))*2-1
return lst
days=['20200115.txt','20200116.txt','20200117.txt','20200118.txt','20200119.txt','20200120.txt',
'20200121.txt','20200122.txt','20200123.txt']
sslabel=['咸宁市',
'黄石市',
'十堰市',
'武汉市',
'鄂州市',
'荆州市',
'仙桃市',
'恩施州',
'天门市',
'潜江市',
'黄冈市',
'襄阳市',
'宜昌市',
'随州市',
'神农架',
'孝感市',
'荆门市']
# plt.figure(figsize=(60,60),dpi=300)
# 创建第一个画板
fig1=plt.figure()
# 将第一个画板划分为2行1列组成的区块,并获取到第一块区域
ax1 = plt.subplot(331)
# fig2=plt.figure(2)
# 将第一个画板划分为2行1列组成的区块,并获取到第一块区域
ax2 = plt.subplot(332)
# fig3=plt.figure(3)
# 将第一个画板划分为2行1列组成的区块,并获取到第一块区域
ax3 = plt.subplot(333)
# fig4=plt.figure(4)
# 将第一个画板划分为2行1列组成的区块,并获取到第一块区域
ax4 = plt.subplot(334)
# fig5=plt.figure(5)
# 将第一个画板划分为2行1列组成的区块,并获取到第一块区域
ax5 = plt.subplot(335)
# fig6=plt.figure(6)
# 将第一个画板划分为2行1列组成的区块,并获取到第一块区域
ax6 = plt.subplot(336)
# 将第一个画板划分为2行1列组成的区块,并获取到第一块区域
ax7 = plt.subplot(337)
ax8 = plt.subplot(338)
ax9 = plt.subplot(339)
index=0
ax_list=[ax1,ax2,ax3,ax4,ax5,ax6,ax7,ax8,ax9]
# fig_list=[fig1,fig2,fig3,fig4]
for day in days:
# plot correlation matrix
# fig = plt.figure() #调用figure创建一个绘图对象
# ax = fig.add_subplot(111)
correlations=get_matrx(day).values
# print(correlations)
# print('###')
print(ax_list[index])
ax_list[index].matshow(correlations, vmin=-1, vmax=1) #绘制热力图,从-1到1
# fig_list[index].colorbar(cax) #将matshow生成热力图设置为颜色渐变条
# ticks = numpy.arange(0,17,1) #生成0-9,步长为1
# ax_list[index].set_xticks(ticks) #生成刻度
# ax_list[index].set_yticks(ticks)
# ax_list[index].set_xticklabels(sslabel,rotation=90) #生成x轴标签
# ax_list[index].set_yticklabels(sslabel)
index = index + 1
# plt.legend()
# fig1.colorbar(cax)
plt.savefig("result.pdf",dpi=300)
plt.show()