import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
import numpy as np
from matplotlib.ticker import MultipleLocator, FormatStrFormatter

tables = pd.read_csv('E:/ZhouYifan_Master/Wave Energy/2018/02/height_period_energy.csv')
# print(tables.shape)
# print(list(tables.columns))
# print(tables.head())
swh=np.array(tables['swh_swanxzm_year'])
# print(swh.shape)
# print(swh)
tmm10=np.array(tables['tmm10_swanxzm_year'])
energy=np.array(tables['energy_swanxzm_year'])
index_swh=np.array(tables['index_swh'])
index_tmm10=np.array(tables['index_tmm10'])
T_segment=np.linspace(0,15,16)
H_segment=np.linspace(0,5.5,12)
count_HT=np.zeros((len(H_segment),len(T_segment)))
color_J=np.zeros((len(H_segment),len(T_segment)))
T_segment=np.linspace(0,16,17)
H_plot=np.linspace(0,11,12)
ro=1030
g=9.8
A=64*np.pi/(ro*g*g)
x_plot=np.linspace(0,16,1601)
y_plot=np.linspace(0,12,1201)
def f(x,y):
    return x*(6-0.5*y)*(6-0.5*y)/A
X, Y=np.meshgrid(x_plot, y_plot)
Z = f(X, Y)
for i in range(len(swh)):
    count_HT[index_swh[i]-1,index_tmm10[i]-1]=count_HT[index_swh[i]-1,index_tmm10[i]-1]+1
    color_J[index_swh[i]-1,index_tmm10[i]-1]=count_HT[index_swh[i]-1,index_tmm10[i]-1]+energy[i]

count_HT=np.flip(count_HT,axis=0)
color_J=np.flip(color_J,axis=0)
mask=np.zeros_like(count_HT)
mask[np.where(count_HT==0)] = True

sns.set(font='Times New Roman',font_scale=4.5) 
fig = plt.figure(figsize=(54,36))
axes = plt.subplot()

sns_plot = sns.heatmap(color_J,mask=mask,cmap='OrRd',annot=count_HT,fmt=".0f",annot_kws={'size':50,'weight':'bold'},square=True)
sns_plot.tick_params(labelsize=15, direction='in')

cax = plt.gcf().axes[-1]
# colorbar 中 top='off', bottom='off', left='off', right='off'表示上下左右侧的刻度线全部不显示
cax.tick_params(labelsize=60, direction='out', top='off', bottom='off', left='on', right='on')

C=plt.contour(X, Y, Z, [2000, 20000, 50000, 100000, 200000], colors='k',linewidths=3)  #生成等值线图
# plt.contour(X,Y,f(X,Y),8)
manual_locations = [(1, 12-2*np.sqrt(2000*A/1)), (3, 12-2*np.sqrt(20000*A/3)),  
                    (5, 12-2*np.sqrt(50000*A/5)), (7, 12-2*np.sqrt(100000*A/7)),   
                    (13.6, 12-2*np.sqrt(200000*A/13.6))]
plt.clabel(C,inline=1, fontsize=60, manual=manual_locations)

# plt.plot(x_plot, 12-2*np.sqrt(2000*A/x_plot), color='black')
# plt.plot(x_plot, 12-2*np.sqrt(20000*A/x_plot), color='black')
# plt.plot(x_plot, 12-2*np.sqrt(50000*A/x_plot), color='black')
# plt.plot(x_plot, 12-2*np.sqrt(100000*A/x_plot), color='black')
# plt.plot(x_plot, 12-2*np.sqrt(200000*A/x_plot), color='black')

# xmajorLocator   = MultipleLocator(1)   #将x轴主刻度标签设置为1
# sns_plot.xaxis.set_major_locator(xmajorLocator)
# xminorLocator   = MultipleLocator(0.5)   #将x轴次刻度标签设置为0.5
# sns_plot.xaxis.set_minor_locator(xminorLocator)
# # xmajorFormatter = FormatStrFormatter('%s') #设置x轴标签文本的格式
# # sns_plot.xaxis.set_major_formatter(xmajorFormatter)
# plt.xticks(T_segment,['0','1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16'])

# plt.yticks(rotation = 0)   #设置y轴表明文字的放向,0为平放90为竖着放
# ymajorLocator   = MultipleLocator(1)  
# sns_plot.yaxis.set_major_locator(ymajorLocator)
# ymajorFormatter = FormatStrFormatter('%s') #设置y轴标签文本的格式
# sns_plot.yaxis.set_major_formatter(ymajorFormatter)
# plt.yticks(H_plot,['6','5.5','5','4.5','4','3.5','3','2.5','2','1.5','1','0.5'])

# axes.minorticks_on()
# # 显示副刻度线
# axes.tick_params(axis="both", which="major", direction="out", width=1, length=5)
# axes.tick_params(axis="both", which="minor", direction="out", width=1, length=3)

plt.xlabel('$T_e$ [s]',fontsize=80, color='k',font='Times New Roman') #x轴label的文本和字体大小
plt.ylabel('$H_{m0}$ [m]',fontsize=80, color='k',font='Times New Roman') #y轴label的文本和字体大小
plt.xticks(fontsize=60) #x轴刻度的字体大小(文本包含在pd_data中了)
plt.yticks(fontsize=60) #y轴刻度的字体大小(文本包含在pd_data中了)
# plt.title('title',fontsize=20) #图片标题文本和字体大小
# plt.rcParams['font.family'] = 'Times New Roman'

# plt.savefig('E:/ZhouYifan_Master/Wave Energy/2018/02/height_period_energy.png',dpi=600)
plt.show()

上述代码作图为什么没有刻度线内容:在生成热力图的时候,使用了 `square=True` 参数,将热力图的宽高比设置为1,使得横纵坐标的刻度线被隐藏了。如果需要显示刻度线,可以将该参数去掉即可。同时,可以使用 `aspect` 参数来调整热力图的宽高比。例如:

```python
sns_plot = sns.heatmap(color_J, mask=mask, cmap='OrRd', annot=count_HT, fmt=".0f", annot_kws={'size':50,'weight':'bold'}, square=False, aspect=1.2)

其中,aspect 参数可以根据具体情况进行调整。

Python Seaborn热力图绘制与等值线叠加示例

原文地址: https://www.cveoy.top/t/topic/mHHI 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录