爱气象,爱气象家园! 

气象家园

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博登陆

只需一步, 快速开始

搜索
查看: 1571|回复: 2

[源代码] 类似于联合分布的图表,表现实况更能说明问题

[复制链接]

新浪微博达人勋

发表于 2023-6-18 00:19:34 | 显示全部楼层 |阅读模式

登录后查看更多精彩内容~

您需要 登录 才可以下载或查看,没有帐号?立即注册 新浪微博登陆

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')


能见度.png
密码修改失败请联系微信:mofangbao

新浪微博达人勋

发表于 2023-6-19 10:53:58 | 显示全部楼层
我感觉把柱状图换成折线图会好看一点儿
密码修改失败请联系微信:mofangbao
回复 支持 反对

使用道具 举报

新浪微博达人勋

 楼主| 发表于 2023-6-20 19:40:54 | 显示全部楼层
内马尔 发表于 2023-6-19 10:53
我感觉把柱状图换成折线图会好看一点儿

根据自己需要 这个简单
密码修改失败请联系微信:mofangbao
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册 新浪微博登陆

本版积分规则

Copyright ©2011-2014 bbs.06climate.com All Rights Reserved.  Powered by Discuz! (京ICP-10201084)

本站信息均由会员发表,不代表气象家园立场,禁止在本站发表与国家法律相抵触言论

快速回复 返回顶部 返回列表