- 积分
- 915
- 贡献
-
- 精华
- 在线时间
- 小时
- 注册时间
- 2012-4-24
- 最后登录
- 1970-1-1
|
登录后查看更多精彩内容~
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
import pandas as pd
import matplotlib.pyplot as plt
from scipy.ndimage import gaussian_filter
import numpy as np
from matplotlib import rcParams
import seaborn as sns
import matplotlib.ticker as mticker
from matplotlib.gridspec import GridSpec
from matplotlib.ticker import MultipleLocator, AutoLocator, FixedLocator
config = {
"font.family":'Times New Roman', # 设置字体类型
"font.size": 12,
# "mathtext.fontset":'stix',
}
rcParams.update(config)
c="date08 AQI PM2 PM10 SO2 day month year hour VIS_HOR_10MI WIN_S_Avg_2mi WIN_D_Avg_2mi RHU"
c=c.split(" ")
print(c)
data=pd.read_excel(r"I:\disert\202203\2022沙尘天气分析.xlsx")
df=pd.DataFrame(data)
df["VIS_HOR_10MI"]=df["VIS_HOR_10MI"].astype("float")
rainsumday=df.groupby("day")["VIS_HOR_10MI"].mean()
rainsumhour=df.groupby("hour")["VIS_HOR_10MI"].mean()
df2=df.pivot(index='day', columns='hour', values='VIS_HOR_10MI')
df2=df2.fillna(0)
#df2=df2.reset_index()
#day=["12","13","14"]
day2=df2.index
print(day2)
fig = plt.figure(figsize=(12, 6))
gs = GridSpec(4, 6, hspace=0.0, wspace=0.0)
ax1_xy, ax1_x, ax1_y = fig.add_subplot(gs[1:4, 0:4]), fig.add_subplot(gs[0, 0:4]), fig.add_subplot(gs[1:4, 4:])
def set_ax(ax: plt.axes, xy: tuple, tblr: tuple, label_xy: bool, title: str):
"""
设置绘图区域ax的样式
:param ax: 绘图区域
:param xy: (bool, bool) 控制x和y轴的刻度是否可见
:param tblr: (bool, bool, bool, bool) 控制上下左右的边框是否可见
:param label_xy: bool 控制是否显示x和y轴的标签
:param title: str 绘图区域的标题
:return: 0
"""
if title:
ax.set_title(title)
ax.tick_params(bottom=True, top=False, left=True, right=False) # 隐藏刻度间隔线
ax.axes.xaxis.set_visible(xy[0]) # 设置刻度
ax.axes.yaxis.set_visible(xy[1]) # 设置刻度
drc = ['top', 'bottom', 'left', 'right']
for idx in range(4):
ax.spines[drc[idx]].set_visible(tblr[idx])
return 0
set_ax(ax1_xy, (1, 1), (0, 1, 1, 0), True, '')
set_ax(ax1_x, (0, 1), (0, 1, 1, 0), True, '')
set_ax(ax1_y, (1, 0), (0, 1, 1, 0), True, '')
xmajor = MultipleLocator(1) # 主 10 的倍数
ymajor = MultipleLocator(1)
ax1_xy.set_ylim([17.9,31.1])
ax1_xy.set_xlim([-0.1,23.1])
ax1_x.set_xlim([-0.3,23.3])
ax1_y.set_ylim([17.9,31.1])
# 设置 x/y轴刻度,以及刻度标签
# x 轴
#ax.xaxis.set_minor_locator(xminor)
ax1_xy.xaxis.set_major_locator(xmajor)
ax1_xy.yaxis.set_major_locator(ymajor)
#ax1_xy.set_xlim([0,8000])
cf=ax1_xy.contourf(df2.columns.tolist(),day2,gaussian_filter(np.array(df2.values.tolist()), sigma=0.2),np.arange(0,8000,500),cmap=plt.cm.jet_r)
#ax1_xy.set_yticks([0,1,2],position=(0,-1))
#ax3.hist(rainsumhour.values.tolist(),24,orientation='vertical', color='deeppink')
#ax2.hist(rainsumday.values.tolist(), 6,histtype='stepfilled', orientation='horizontal', color='deeppink')
ax1_x.set_yticks([ 1000, 2000,3000,4000,5000])
ax1_y.set_xticks([ 1000, 2000,3000,4000,5000,6000,7500])
font = {'family' : 'simsun',
'color' : 'black',
'weight' : 'normal',
'size' : 12,
}
ax1_xy.set_xlabel(u'时刻',fontdict=font)
ax1_xy.set_ylabel(u'日期',fontdict=font)
ax1_y.set_xlabel(u'能见度/m',fontdict=font)
ax1_x.set_ylabel(u'能见度/m',fontdict=font)
cbar_ax = fig.add_axes([0.85, 0.23, 0.015, 0.4])
ax1_y.set_xlim([0,8000])
ax1_x.set_ylim([0,5000])
cb=plt.colorbar(cf,cax=cbar_ax,ticks=np.arange(0,8000,500),orientation='vertical')
cb.set_label(u'能见度/m',fontdict=font)
for tick in ax1_xy.yaxis.get_major_ticks():
tick.label1.set_fontsize(12)
ax1_x.bar(df2.columns.tolist(),rainsumhour.values,orientation='vertical', color='deeppink',width = 0.5)
#ax1_y.barh(day,rainsumday.values.tolist(),tick_label=day, edgecolor = 'black', linewidth = 2, align = 'center', color = 'g')
ax1_y.barh(y=day2,width=rainsumday.values.tolist(),color='#008080',height=0.5)
plt.savefig('test能见度.png',dpi=600, bbox_inches='tight')
|
-
|