爱气象,爱气象家园! 

气象家园

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博登陆

只需一步, 快速开始

搜索
查看: 2435|回复: 0

[经验总结] 累计变量直方图主要代码(Python,毕设期间总结)

[复制链接]

新浪微博达人勋

发表于 2023-4-26 09:37:53 | 显示全部楼层 |阅读模式

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

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

x
import matplotlib.pyplot as plt
import numpy as np
from matplotlib import rcParams
from matplotlib.ticker import MultipleLocator, FormatStrFormatter

# plt.rcParams['font.serif']=['Times New Roman']
# plt.rcParams['font.sans-serif'] = ['SimSun'] #'SimSun'是宋体
# plt.yticks(fontfamily='Times New Roman')

font1={'family':'Times New Roman','weight':'bold','size':10}
#设置全局中文宋体,英文为Times New Roman
config = {
            "font.family": 'serif',
            "font.size": 11.5,# 相当于小四大小
            "mathtext.fontset": 'stix',#matplotlib渲染数学字体时使用的字体,和Times New Roman差别不大
            "font.serif": ['SimSun'],#宋体
            'axes.unicode_minus': False # 处理负号,即-号
         }
rcParams.update(config)

# 创建字典
ins_name = ['4800-5000hPa','5000-5200hPa','5200-5400hPa','5400-5600hPa','5600-5800hPa']
mon_name = ['1月','2月','3月','4月','5月','6月','7月','8月','9月','10月','11月','12月']


result1 = dict(zip(ins_name,mon_per1))
fig=plt.figure(figsize=(7,8))

ax1=plt.subplot(111)

width = 0.4
bottom1 = np.zeros(12)
bottom2 = np.zeros(12)
bottom3 = np.zeros(12)

#因为我要画五种强度的柱状图并且强度递进,所以我将色标“hot”分成5份投影到category_colors1
category_colors1 = plt.colormaps['hot'](
    np.linspace(0.15, 0.85, 5))

# =============================================================================
# picture1
# =============================================================================

kk=0
for boolean, weight_count in result1.items():
    p1 = ax1.bar(mon_name, weight_count, width, label=boolean, bottom=bottom1,color = category_colors1[kk])
    bottom1 += weight_count
    kk+=1
plt.yticks(range(0,17,2))

#标注每一个柱形代表的数值大小
for nn in range(12):     
    sum_data = sum(mon_per1[:,nn])
    plt.text(nn-0.35,sum_data+0.65,str(round(sum_data,2))+'%',fontdict = font1,va='center',ha='left')
#设置标签
plt.text(-0.7,13.8,"(a)",fontdict={'size':'15','color':'k'})

#做图注bbox_to_anchor控制图的位置大小,ncol对应图注个数,这里为5
ax1.legend(ncol=len(ins_name), bbox_to_anchor=(-0.08, 1.05),
              loc='lower left', fontsize=8)

picture1.png
密码修改失败请联系微信:mofangbao
您需要登录后才可以回帖 登录 | 立即注册 新浪微博登陆

本版积分规则

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

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

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