Python实战系列(一)——基于Numpy和Matplotlib的统计分析和图表绘制

         2021学年第2学期数量地理学课程第一次作业,基于python进行简单的统计分析和图像绘制,本系列主要用于记录代码,大家可进行参考,初学者,Python编码习惯不好,见谅。

from scipy import stats
import xlrd
import matplotlib.pyplot as plt
import numpy as np
# import pandas as pd
data = xlrd.open_workbook(r'C:\Users\JeasunLok\Desktop\Ex01_Monthly.Precipitation.in.Shanghai.and.Guangzhou.1996-2006.xls')
table = data.sheets()[0]
sh = table.col_values(3)
gz = table.col_values(4)
# del sh[0]
# del gz[0]
# print(gz);print(sh)

error = ''
gz_index = gz.index(error)
gz[gz_index] = 0
del error, gz_index

# # 平均数
# shymean = [np.mean(sh[1:12]), np.mean(sh[13:24]), np.mean(sh[25:36]), np.mean(sh[37:48]), np.mean(sh[49:60]),
#            np.mean(sh[61:72]), np.mean(sh[73:84]), np.mean(sh[85:96]), np.mean(sh[97:108]), np.mean(sh[109:120]),
#            np.mean(sh[121:132])]
# gzymean = [np.mean(gz[1:12]), np.mean(gz[13:24]), np.mean(gz[25:36]), np.mean(gz[37:48]), np.mean(gz[49:60]),
#            np.mean(gz[61:72]), np.mean(gz[73:84]), np.mean(gz[85:96]), np.mean(gz[97:108]), np.mean(gz[109:120]),
#            np.mean(gz[121:132])]
# shmmean = [np.mean(sh[1:133:12]), np.mean(sh[2:133:12]), np.mean(sh[3:133:12]), np.mean(sh[4:133:12]),
#            np.mean(sh[5:133:12]), np.mean(sh[6:133:12]), np.mean(sh[7:133:12]), np.mean(sh[8:133:12]),
#            np.mean(sh[9:133:12]), np.mean(sh[10:133:12]), np.mean(sh[11:133:12]), np.mean(sh[12:133:12]), ]
# gzmmean = [np.mean(gz[1:133:12]), np.mean(gz[2:133:12]), np.mean(gz[3:133:12]), np.mean(gz[4:133:12]),
#            np.mean(gz[5:133:12]), np.mean(gz[6:133:12]), np.mean(gz[7:133:12]), np.mean(gz[8:133:12]),
#            np.mean(gz[9:133:12]), np.mean(gz[10:133:12]), np.mean(gz[11:133:12]), np.mean(gz[12:133:12]), ]
#
# # 中位数
# shymedian = [np.median(sh[1:12]), np.median(sh[13:24]), np.median(sh[25:36]), np.median(sh[37:48]),
#              np.median(sh[49:60]), np.median(sh[61:72]), np.median(sh[73:84]), np.median(sh[85:96]),
#              np.median(sh[97:108]), np.median(sh[109:120]), np.median(sh[121:132])]
# gzymedian = [np.median(gz[1:12]), np.median(gz[13:24]), np.median(gz[25:36]), np.median(gz[37:48]),
#              np.median(gz[49:60]), np.median(gz[61:72]), np.median(gz[73:84]), np.median(gz[85:96]),
#              np.median(gz[97:108]), np.median(gz[109:120]), np.median(gz[121:132])]
# shmmedian = [np.median(sh[1:133:12]), np.median(sh[2:133:12]), np.median(sh[3:133:12]), np.median(sh[4:133:12]),
#              np.median(sh[5:133:12]), np.median(sh[6:133:12]), np.median(sh[7:133:12]), np.median(sh[8:133:12]),
#              np.median(sh[9:133:12]), np.median(sh[10:133:12]), np.median(sh[11:133:12]), np.median(sh[12:133:12]), ]
# gzmmedian = [np.median(gz[1:133:12]), np.median(gz[2:133:12]), np.median(gz[3:133:12]), np.median(gz[4:133:12]),
#              np.median(gz[5:133:12]), np.median(gz[6:133:12]), np.median(gz[7:133:12]), np.median(gz[8:133:12]),
#              np.median(gz[9:133:12]), np.median(gz[10:133:12]), np.median(gz[11:133:12]), np.median(gz[12:133:12]), ]
#
# shymode = [stats.mode(sh[1:13]), stats.mode(sh[13:25]), stats.mode(sh[25:37]), stats.mode(sh[37:49]),
#            stats.mode(sh[49:61]), stats.mode(sh[61:73]), stats.mode(sh[73:85]), stats.mode(sh[85:97]),
#            stats.mode(sh[97:109]), stats.mode(sh[109:121]), stats.mode(sh[121:133])]
# gzymode = [stats.mode(gz[1:13]), stats.mode(gz[13:25]), stats.mode(gz[25:37]), stats.mode(gz[37:49]),
#            stats.mode(gz[49:61]), stats.mode(gz[61:73]), stats.mode(gz[73:85]), stats.mode(gz[85:97]),
#            stats.mode(gz[97:109]), stats.mode(gz[109:121]), stats.mode(gz[121:133])]
# shmmode = [stats.mode(sh[1:133:12]), stats.mode(sh[2:133:12]), stats.mode(sh[3:133:12]), stats.mode(sh[4:133:12]),
#            stats.mode(sh[5:133:12]), stats.mode(sh[6:133:12]), stats.mode(sh[7:133:12]), stats.mode(sh[8:133:12]),
#            stats.mode(sh[9:133:12]), stats.mode(sh[10:133:12]), stats.mode(sh[11:133:12]), stats.mode(sh[12:133:12])]
# gzmmode = [stats.mode(gz[1:133:12]), stats.mode(gz[2:133:12]), stats.mode(gz[3:133:12]), stats.mode(gz[4:133:12]),
#            stats.mode(gz[5:133:12]), stats.mode(gz[6:133:12]), stats.mode(gz[7:133:12]), stats.mode(gz[8:133:12]),
#            stats.mode(gz[9:133:12]), stats.mode(gz[10:133:12]), stats.mode(gz[11:133:12]), stats.mode(gz[12:133:12])]

# 数据处理
shy = [sh[1:13], sh[13:25], sh[25:37], sh[37:49], sh[49:61], sh[61:73], sh[73:85], sh[85:97], sh[97:109],
       sh[109:121], sh[121:133]]
shm = [sh[1:133:12], sh[2:133:12], sh[3:133:12], sh[4:133:12], sh[5:133:12], sh[6:133:12], sh[7:133:12], sh[8:133:12],
       sh[9:133:12], sh[10:133:12], sh[11:133:12], sh[12:133:12]]
gzy = [gz[1:13], gz[13:25], gz[25:37], gz[37:49], gz[49:61], gz[61:73], gz[73:85], gz[85:97], gz[97:109],
       gz[109:121], gz[121:133]]
gzm = [gz[1:133:12], gz[2:133:12], gz[3:133:12], gz[4:133:12], gz[5:133:12], gz[6:133:12], gz[7:133:12], gz[8:133:12],
       gz[9:133:12], gz[10:133:12], gz[11:133:12], gz[12:133:12]]

shydata = np.array(shy)
shmdata = np.array(shm)
gzydata = np.array(gzy)
gzmdata = np.array(gzm)

# 平均数
shymean = np.mean(shydata, axis=1)
shmmean = np.mean(shmdata, axis=1)
gzymean = np.mean(gzydata, axis=1)
gzmmean = np.mean(gzmdata, axis=1)

# 中位数
shymedian = np.median(shydata, axis=1)
shmmedian = np.median(shmdata, axis=1)
gzymedian = np.median(gzydata, axis=1)
gzmmedian = np.median(gzmdata, axis=1)

# 众数 没有什么意义
# shymode = [stats.mode(sh[1:13]), stats.mode(sh[13:25]), stats.mode(sh[25:37]), stats.mode(sh[37:49]),
#            stats.mode(sh[49:61]), stats.mode(sh[61:73]), stats.mode(sh[73:85]), stats.mode(sh[85:97]),
#            stats.mode(sh[97:109]), stats.mode(sh[109:121]), stats.mode(sh[121:133])]
# gzymode = [stats.mode(gz[1:13]), stats.mode(gz[13:25]), stats.mode(gz[25:37]), stats.mode(gz[37:49]),
#            stats.mode(gz[49:61]), stats.mode(gz[61:73]), stats.mode(gz[73:85]), stats.mode(gz[85:97]),
#            stats.mode(gz[97:109]), stats.mode(gz[109:121]), stats.mode(gz[121:133])]
# shmmode = [stats.mode(sh[1:133:12]), stats.mode(sh[2:133:12]), stats.mode(sh[3:133:12]), stats.mode(sh[4:133:12]),
#            stats.mode(sh[5:133:12]), stats.mode(sh[6:133:12]), stats.mode(sh[7:133:12]), stats.mode(sh[8:133:12]),
#            stats.mode(sh[9:133:12]), stats.mode(sh[10:133:12]), stats.mode(sh[11:133:12]), stats.mode(sh[12:133:12])]
# gzmmode = [stats.mode(gz[1:133:12]), stats.mode(gz[2:133:12]), stats.mode(gz[3:133:12]), stats.mode(gz[4:133:12]),
#            stats.mode(gz[5:133:12]), stats.mode(gz[6:133:12]), stats.mode(gz[7:133:12]), stats.mode(gz[8:133:12]),
#            stats.mode(gz[9:133:12]), stats.mode(gz[10:133:12]), stats.mode(gz[11:133:12]), stats.mode(gz[12:133:12])]
# shymode  = np.array(shymode)
# shmmode  = np.array(shmmode)
# gzymode  = np.array(gzymode)
# gzmmode  = np.array(gzmmode)

# 极差
shyptp = np.ptp(shydata, axis=1)
shmptp = np.ptp(shmdata, axis=1)
gzyptp = np.ptp(gzydata, axis=1)
gzmptp = np.ptp(gzmdata, axis=1)

# 四分位差
i = 0
shyiqr = np.zeros((11, 3))
shmiqr = np.zeros((12, 3))
gzyiqr = np.zeros((11, 3))
gzmiqr = np.zeros((12, 3))
while i < 11:
    shyiqr[i] = np.percentile(shydata[i], (25, 50, 75), interpolation='midpoint')
    shmiqr[i] = np.percentile(shmdata[i], (25, 50, 75), interpolation='midpoint')
    gzyiqr[i] = np.percentile(gzydata[i], (25, 50, 75), interpolation='midpoint')
    gzmiqr[i] = np.percentile(gzmdata[i], (25, 50, 75), interpolation='midpoint')
    i = i + 1
shmiqr[11] = np.percentile(shmdata[11], (25, 50, 75), interpolation='midpoint')
gzmiqr[11] = np.percentile(gzmdata[11], (25, 50, 75), interpolation='midpoint')


# 方差
shyvar = np.var(shydata, axis=1)
shmvar = np.var(shmdata, axis=1)
gzyvar = np.var(gzydata, axis=1)
gzmvar = np.var(gzmdata, axis=1)

# 标准差
shystd = np.std(shydata, axis=1)
shmstd = np.std(shmdata, axis=1)
gzystd = np.std(gzydata, axis=1)
gzmstd = np.std(gzmdata, axis=1)

# CV
shyCV = shystd/shymean
shmCV = shmstd/shmmean
gzyCV = gzystd/gzymean
gzmCV = gzmstd/gzmmean

# 偏度


def skewness(data):
    numdata = len(data)
    meandata = np.mean(data)
    stddata = np.std(data)
    summary = 0
    i = 0
    while i < numdata:
        summary = summary + (((data[i]-meandata)/stddata)**3)
        i = i+1
    return (numdata/((numdata-1)*(numdata-2))*summary)


shysk = [skewness(shydata[0]), skewness(shydata[1]), skewness(shydata[2]), skewness(shydata[3]), skewness(shydata[4]),
         skewness(shydata[5]), skewness(shydata[6]), skewness(shydata[7]), skewness(shydata[8]), skewness(shydata[9]),
         skewness(shydata[10])]
shmsk = [skewness(shmdata[0]), skewness(shmdata[1]), skewness(shmdata[2]), skewness(shmdata[3]), skewness(shmdata[4]),
         skewness(shmdata[5]), skewness(shmdata[6]), skewness(shmdata[7]), skewness(shmdata[8]), skewness(shmdata[9]),
         skewness(shmdata[10]), skewness(shmdata[11])]
gzysk = [skewness(gzydata[0]), skewness(gzydata[1]), skewness(gzydata[2]), skewness(gzydata[3]), skewness(gzydata[4]),
         skewness(gzydata[5]), skewness(gzydata[6]), skewness(gzydata[7]), skewness(gzydata[8]), skewness(gzydata[9]),
         skewness(gzydata[10])]
gzmsk = [skewness(gzmdata[0]), skewness(gzmdata[1]), skewness(gzmdata[2]), skewness(gzmdata[3]), skewness(gzmdata[4]),
         skewness(gzmdata[5]), skewness(gzmdata[6]), skewness(gzmdata[7]), skewness(gzmdata[8]), skewness(gzmdata[9]),
         skewness(gzmdata[10]), skewness(gzmdata[11])]
shysk = np.array(shysk)
shmsk = np.array(shmsk)
gzysk = np.array(gzysk)
gzmsk = np.array(gzmsk)

# 峰度


def kurtosis(data):
    numdata = len(data)
    meandata = np.mean(data)
    stddata = np.std(data)
    summary = 0
    i = 0
    while i < numdata:
        summary = summary + ((data[i]-meandata)**4)
        i = i+1
    return summary/((numdata-1)*(stddata**4))


shykts = [kurtosis(shydata[0]), kurtosis(shydata[1]), kurtosis(shydata[2]), kurtosis(shydata[3]), kurtosis(shydata[4]),
          kurtosis(shydata[5]), kurtosis(shydata[6]), kurtosis(shydata[7]), kurtosis(shydata[8]), kurtosis(shydata[9]),
          kurtosis(shydata[10])]
shmkts = [kurtosis(shmdata[0]), kurtosis(shmdata[1]), kurtosis(shmdata[2]), kurtosis(shmdata[3]), kurtosis(shmdata[4]),
          kurtosis(shmdata[5]), kurtosis(shmdata[6]), kurtosis(shmdata[7]), kurtosis(shmdata[8]), kurtosis(shmdata[9]),
          kurtosis(shmdata[10]), kurtosis(shmdata[11])]
gzykts = [kurtosis(gzydata[0]), kurtosis(gzydata[1]), kurtosis(gzydata[2]), kurtosis(gzydata[3]), kurtosis(gzydata[4]),
          kurtosis(gzydata[5]), kurtosis(gzydata[6]), kurtosis(gzydata[7]), kurtosis(gzydata[8]), kurtosis(gzydata[9]),
          kurtosis(gzydata[10])]
gzmkts = [kurtosis(gzmdata[0]), kurtosis(gzmdata[1]), kurtosis(gzmdata[2]), kurtosis(gzmdata[3]), kurtosis(gzmdata[4]),
          kurtosis(gzmdata[5]), kurtosis(gzmdata[6]), kurtosis(gzmdata[7]), kurtosis(gzmdata[8]), kurtosis(gzmdata[9]),
          kurtosis(gzmdata[10]), kurtosis(gzmdata[11])]
shykts = np.array(shykts)
shmkts = np.array(shmkts)
gzykts = np.array(gzykts)
gzmkts = np.array(gzmkts)

# 开始绘图
config = {
    "font.family": "serif",  # 使用衬线体
    "font.serif": ["Microsoft YaHei"],  # 全局默认使用衬线宋体
    "font.size": 16,
    "axes.unicode_minus": False,
    "mathtext.fontset": "stix",  # 设置 LaTeX 字体,stix 近似于 Times 字体
}
plt.rcParams.update(config)

ticklabels_style = {
    "fontname": "Arial",
    "fontsize": 10,
}
# 绘图一:均值,中位数
# 年际
labels_year = np.array([1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006])
# shymean gzymean
# x_year = np.arange(len(labels_year))
width = 0.35
fig, ax1 = plt.subplots()
rects1 = ax1.bar(labels_year - width/2, shymean, width, label='上海年均降雨量', color='#80AFBF', ec='black', lw=1)
rects2 = ax1.bar(labels_year + width/2, gzymean, width, label='广州年均降雨量', color='#E2C3C9', ec='black', lw=1)
ax1.grid(axis='y', linestyle='-.', alpha=0.2)
ax1.set_title('上海与广州年际降雨量均值与中位数对比(1996-2006)', weight='bold', fontsize=26)
ax1.set_ylabel('降雨量(单位:$\mathrm{mm}$)', weight='bold', fontsize=22)
ax1.set_ylim(bottom=0, top=250)
ax1.set_yticks(np.arange(0, 251, step=25))
plt.yticks(fontsize=18, weight='bold')
ax1.set_xlabel('年份', weight='bold', fontsize=22)
ax1.set_xticks(labels_year)
plt.xticks(fontsize=18, weight='bold')
plt.gca().spines['left'].set_linewidth(2)
plt.gca().spines['bottom'].set_linewidth(2)
plt.gca().spines['right'].set_linewidth(2)
plt.gca().spines['top'].set_linewidth(2)
ax1.tick_params(direction='out', width=3)
# legend_form = {'weight': 'bold', 'size': 20}
ax1.legend(fontsize=20, frameon=False, loc='upper left')
ax1.bar_label(rects1, padding=3, fmt='%.1f', fontsize=10, weight='bold')
ax1.bar_label(rects2, padding=3, fmt='%.1f', fontsize=10, weight='bold')


ax2 = plt.twinx()
plt.plot(labels_year - width/2, shymedian , '-.', color='#608595', marker='o', label='上海年际降雨量中位数')
for a, b in zip(labels_year - width/2, np.round(shymedian, 1)):
    plt.text(a, b, b, ha='center', va='top', fontsize=10, weight='bold')
plt.plot(labels_year + width/2, gzymedian , '-.', color='#C07A92', marker='o', label='广州年际降雨量中位数')
for a, b in zip(labels_year + width/2, np.round(gzymedian, 1)):
    plt.text(a, b, b, ha='center', va='top', fontsize=10, weight='bold')
ax2.axes.yaxis.set_visible(False)
plt.legend(fontsize=20, frameon=False, loc="upper right")
fig.tight_layout()
plt.show()
# plt.savefig("上海与广州年降雨量均值与中位数对比(1996-2006).jpg")


# 月际
labels_month = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])
# shmmean gzmmean
# x_month = np.arange(len(labels_month))
width = 0.35
fig, ax3 = plt.subplots()
rects3 = ax3.bar(labels_month - width/2, shmmean, width, label='上海十年间月均降雨量', color='#80AFBF', ec='black', lw=1)
rects4 = ax3.bar(labels_month + width/2, gzmmean, width, label='广州十年间月均降雨量', color='#E2C3C9', ec='black', lw=1)
ax3.grid(axis='y', linestyle='-.', alpha=0.2)
ax3.set_title('上海与广州十年间各月降雨量均值与中位数对比(1996-2006)', weight='bold', fontsize=26)
ax3.set_ylabel('降雨量(单位:$\mathrm{mm}$)', weight='bold', fontsize=22)
ax3.set_ylim(bottom=0, top=350)
ax3.set_yticks(np.arange(0, 351, step=25))
plt.yticks(fontsize=18, weight='bold')
ax3.set_xlabel('月份', weight='bold', fontsize=22)
ax3.set_xticks(labels_month)
ax3.set_xticklabels(['1月', '2月', '3月', '4月', '5月', '6月', '7月', '8月', '9月', '10月', '11月', '12月'])
plt.xticks(fontsize=18, weight='bold')
plt.gca().spines['left'].set_linewidth(2)
plt.gca().spines['bottom'].set_linewidth(2)
plt.gca().spines['right'].set_linewidth(2)
plt.gca().spines['top'].set_linewidth(2)
ax3.tick_params(direction='out', width=3)
legend_form = {'weight': 'bold', 'size': 20}
ax3.legend(fontsize=20, frameon=False, loc='upper left')
ax3.bar_label(rects3, padding=3, fmt='%.1f', fontsize=10, weight='bold')
ax3.bar_label(rects4, padding=3, fmt='%.1f', fontsize=10, weight='bold')


ax4 = plt.twinx()
plt.plot(labels_month - width/2, shmmedian, '-.', color='#608595', marker='o', label='上海十年间月降雨量中位数')
for a, b in zip(labels_month - width/2, np.round(shmmedian, 1)):
    plt.text(a, b, b, ha='center', va='top', fontsize=10, weight='bold')
plt.plot(labels_month + width/2, gzmmedian, '-.', color='#C07A92', marker='o', label='广州十年间月降雨量中位数')
for a, b in zip(labels_month + width/2, np.round(gzmmedian, 1)):
    plt.text(a, b, b, ha='center', va='top', fontsize=10, weight='bold')
ax4.axes.yaxis.set_visible(False)
plt.legend(fontsize=20, frameon=False, loc="upper right")
plt.show()

# 绘图二:方差,标准差,CV,峰度,偏度(选择标准差绘图)
# 年际
fig, ax5 = plt.subplots()
plt.plot(labels_year, shystd, color='#1C5C82', marker='o', label='上海年际降雨量标准差', linewidth=3)
for a, b in zip(labels_year, np.round(shystd, 1)):
    plt.text(a, b, b, ha='right', va='bottom', fontsize=10, weight='bold')
plt.plot(labels_year, gzystd, color='#75C5DE', marker='o', label='广州年际降雨量标准差', linewidth=3)
for a, b in zip(labels_year, np.round(gzystd, 1)):
    plt.text(a, b, b, ha='left', va='bottom', fontsize=10, weight='bold')

ax5.grid(linestyle='-.', alpha=0.2)
ax5.set_title('上海与广州年际降雨量标准差,变异系数,偏度及峰度对比(1996-2006)', weight='bold', fontsize=26)
ax5.set_ylabel('降雨量标准差(单位:$\mathrm{mm}$)', weight='bold', fontsize=22)
ax5.set_ylim(bottom=0, top=500)
ax5.set_yticks(np.arange(0, 501, step=50))
plt.yticks(fontsize=18, weight='bold')
ax5.set_xlabel('月份', weight='bold', fontsize=22)
ax5.set_xticks(labels_year)
plt.xticks(fontsize=18, weight='bold')
plt.gca().spines['left'].set_linewidth(2)
plt.gca().spines['bottom'].set_linewidth(2)
plt.gca().spines['right'].set_linewidth(2)
plt.gca().spines['top'].set_linewidth(2)
ax5.tick_params(direction='out', width=3)
plt.legend(fontsize=13, frameon=False, loc="upper left")
fig.tight_layout()
plt.show()

ax6 = plt.twinx()

plt.plot(labels_year, shysk, '-.', color='r', marker='s', label='上海年际降雨量偏度', linewidth=3)
# for a, b in zip(labels_year, np.round(shysk, 1)):
#     plt.text(a, b, b, ha='center', va='bottom', fontsize=10, weight='bold')
plt.plot(labels_year, gzysk, '-.', color='salmon', marker='s', label='广州年际降雨量偏度', linewidth=3)
# for a, b in zip(labels_year, np.round(gzysk, 1)):
#     plt.text(a, b, b, ha='center', va='bottom', fontsize=10, weight='bold')
plt.plot(labels_year, shykts, ':', color='g', marker='p', label='上海年际降雨量峰度', linewidth=3)
# for a, b in zip(labels_year, np.round(shykts, 1)):
#     plt.text(a, b, b, ha='center', va='bottom', fontsize=10, weight='bold')
plt.plot(labels_year, gzykts, ':', color='lightgreen', marker='p', label='广州年际降雨量峰度', linewidth=3)
# for a, b in zip(labels_year, np.round(gzykts, 1)):
#     plt.text(a, b, b, ha='center', va='bottom', fontsize=10, weight='bold')

plt.plot(labels_year, shyCV, '--', color='goldenrod', marker='^', label='上海年际降雨量变异系数', linewidth=3)
for a, b in zip(labels_year, np.round(shyCV, 1)):
    plt.text(a, b, b, ha='right', va='top', fontsize=10, weight='bold')
plt.plot(labels_year, gzyCV, '--', color='wheat', marker='^', label='广州年际降雨量变异系数', linewidth=3)
for a, b in zip(labels_year, np.round(gzyCV, 1)):
    plt.text(a, b, b, ha='left', va='top', fontsize=10, weight='bold')

plt.legend(fontsize=13, frameon=False, loc="upper center")
ax6.set_ylabel('变异系数,偏度及峰度系数(无单位)', weight='bold', fontsize=22)
ax6.set_ylim(bottom=-5, top=10)
ax6.set_yticks(np.arange(-10, 10.1, step=2))
plt.yticks(fontsize=18, weight='bold')
ax6.tick_params(direction='out', width=3)

# 月际
fig, ax7 = plt.subplots()
plt.plot(labels_month, shmstd, color='#1C5C82', marker='o', label='上海年际降雨量标准差', linewidth=3)
for a, b in zip(labels_month, np.round(shmstd, 1)):
    plt.text(a, b, b, ha='left', va='top', fontsize=10, weight='bold')
plt.plot(labels_month, gzmstd, color='#75C5DE', marker='o', label='广州年际降雨量标准差', linewidth=3)
for a, b in zip(labels_month, np.round(gzmstd, 1)):
    plt.text(a, b, b, ha='left', va='bottom', fontsize=10, weight='bold')

ax7.grid(linestyle='-.', alpha=0.2)
ax7.set_title('上海与广州十年间各月降雨量标准差,变异系数,偏度及峰度对比(1996-2006)', weight='bold', fontsize=26)
ax7.set_ylabel('降雨量标准差(单位:$\mathrm{mm}$)', weight='bold', fontsize=22)
ax7.set_ylim(bottom=0, top=400)
ax7.set_yticks(np.arange(0, 401, step=40))
plt.yticks(fontsize=18, weight='bold')
ax7.set_xlabel('月份', weight='bold', fontsize=22)
ax7.set_xticks(labels_month)
ax7.set_xticklabels(['1月', '2月', '3月', '4月', '5月', '6月', '7月', '8月', '9月', '10月', '11月', '12月'])
plt.xticks(fontsize=18, weight='bold')
plt.gca().spines['left'].set_linewidth(2)
plt.gca().spines['bottom'].set_linewidth(2)
plt.gca().spines['right'].set_linewidth(2)
plt.gca().spines['top'].set_linewidth(2)
ax7.tick_params(direction='out', width=3)
plt.legend(fontsize=13, frameon=False, loc="upper left")
fig.tight_layout()
plt.show()

ax8 = plt.twinx()

plt.plot(labels_month, shmsk, '-.', color='r', marker='s', label='上海十年间各月降雨量偏度', linewidth=3)
# for a, b in zip(labels_year, np.round(shmsk, 1)):
#     plt.text(a, b, b, ha='center', va='bottom', fontsize=10, weight='bold')
plt.plot(labels_month, gzmsk, '-.', color='salmon', marker='s', label='广州十年间各月降雨量偏度', linewidth=3)
# for a, b in zip(labels_year, np.round(gzmsk, 1)):
#     plt.text(a, b, b, ha='center', va='bottom', fontsize=10, weight='bold')
plt.plot(labels_month, shmkts, ':', color='g', marker='p', label='上海十年间各月降雨量峰度', linewidth=3)
# for a, b in zip(labels_year, np.round(shmkts, 1)):
#     plt.text(a, b, b, ha='center', va='bottom', fontsize=10, weight='bold')
plt.plot(labels_month, gzmkts, ':', color='lightgreen', marker='p', label='广州十年间各月降雨量峰度', linewidth=3)
# for a, b in zip(labels_year, np.round(gzmkts, 1)):
#     plt.text(a, b, b, ha='center', va='bottom', fontsize=10, weight='bold')

plt.plot(labels_month, shmCV, '--', color='goldenrod', marker='^', label='上海十年间各月降雨量变异系数', linewidth=3)
for a, b in zip(labels_month, np.round(shmCV, 1)):
    plt.text(a, b, b, ha='right', va='top', fontsize=10, weight='bold')
plt.plot(labels_month, gzmCV, '--', color='wheat', marker='^', label='广州十年间各月降雨量变异系数', linewidth=3)
for a, b in zip(labels_month, np.round(gzmCV, 1)):
    plt.text(a, b, b, ha='left', va='bottom', fontsize=10, weight='bold')

plt.legend(fontsize=13, frameon=False, loc="upper center")
ax8.set_ylabel('变异系数,偏度及峰度系数(无单位)', weight='bold', fontsize=22)
ax8.set_ylim(bottom=-8, top=12)
ax8.set_yticks(np.arange(-8, 12.1, step=2))
plt.yticks(fontsize=18, weight='bold')
ax8.tick_params(direction='out', width=3)

# 绘图三:四分位差
# 月际
# shydata = np.array(shy)
# shmdata = np.array(shm)
# gzydata = np.array(gzy)
# gzmdata = np.array(gzm)
fig, ax9 = plt.subplots()
sh_month_box = ax9.boxplot(shydata, positions=[3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36], widths=1, patch_artist=True,
                 showmeans=True, showfliers=True,
                 medianprops={"color": "white", "linewidth": 1.5},
                 boxprops={"facecolor": "#5480BB", "edgecolor": "white",
                           "linewidth": 3},
                 whiskerprops={"color": "#5480BB", "linewidth": 3},
                 capprops={"color": "#5480BB", "linewidth": 3})

gz_month_box = ax9.boxplot(gzydata, positions=[4, 7, 10, 13, 16, 19, 22, 25, 28, 31, 34, 37],  \
                           widths=1, patch_artist=True,
                           showmeans=True, showfliers=True,
                           medianprops={"color": "white", "linewidth": 1.5},
                           boxprops={"facecolor": "#DEF1FF", "edgecolor": "white",
                                    "linewidth": 3},
                           whiskerprops={"color": "#DEF1FF", "linewidth": 3},
                           capprops={"color": "#DEF1FF", "linewidth": 3})

ax9.set_title('上海与广州十年间各月降雨量箱型图(1996-2006)', weight='bold', fontsize=26)
ax9.set_ylabel('降雨量(单位:$\mathrm{mm}$)', weight='bold', fontsize=22)
ax9.set_ylim(bottom=0, top=650)
ax9.set_yticks(np.arange(0, 651, step=50))
plt.yticks(fontsize=18, weight='bold')
ax9.set_xlabel('月份(左为上海,右为广州)', weight='bold', fontsize=22)
ax9.set_xticks(np.arange(0.5, 39.6, step=3))
ax9.set_xticklabels(['', '1月', '2月', '3月', '4月', '5月', '6月', '7月', '8月', '9月', '10月', '11月', '12月', ''])
plt.xticks(fontsize=18, weight='bold')
plt.gca().spines['left'].set_linewidth(2)
plt.gca().spines['bottom'].set_linewidth(2)
plt.gca().spines['right'].set_linewidth(2)
plt.gca().spines['top'].set_linewidth(2)
ax9.tick_params(direction='out', width=3)
ax9.grid(axis='y', linestyle='-.', alpha=0.2)
plt.show()

# 年际
# shydata = np.array(shy)
# shmdata = np.array(shm)
# gzydata = np.array(gzy)
# gzmdata = np.array(gzm)
fig, ax10 = plt.subplots()
sh_year_box = ax10.boxplot(shmdata, positions=[3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33],     
                           widths=1, patch_artist=True,
                           showmeans=True, showfliers=True,
                           medianprops={"color": "white", "linewidth": 1.5},
                           boxprops={"facecolor": "#5480BB", "edgecolor": "white",
                           "linewidth": 3},
                           whiskerprops={"color": "#5480BB", "linewidth": 3},
                           capprops={"color": "#5480BB", "linewidth": 3})

gz_year_box = ax10.boxplot(gzmdata, positions=[4, 7, 10, 13, 16, 19, 22, 25, 28, 31, 34],  
                           widths=1, patch_artist=True,
                           showmeans=True, showfliers=True,
                           medianprops={"color": "white", "linewidth": 1.5},
                           boxprops={"facecolor": "#DEF1FF", "edgecolor": "white",
                                    "linewidth": 3},
                           whiskerprops={"color": "#DEF1FF", "linewidth": 3},
                           capprops={"color": "#DEF1FF", "linewidth": 3})

ax10.set_title('上海与广州年际降雨量箱型图(1996-2006)', weight='bold', fontsize=26)
ax10.set_ylabel('降雨量(单位:$\mathrm{mm}$)', weight='bold', fontsize=22)
ax10.set_ylim(bottom=0, top=650)
ax10.set_yticks(np.arange(0, 651, step=50))
plt.yticks(fontsize=18, weight='bold')
ax10.set_xlabel('年份(左为上海,右为广州)', weight='bold', fontsize=22)
ax10.set_xticks(np.arange(0.5, 36.6, step=3))
ax10.set_xticklabels(['','1996', '1997', '1998', '1999', '2000', '2001', '2002', '2003', '2004', '2005', '2006', ''])
plt.xticks(fontsize=18, weight='bold')
plt.gca().spines['left'].set_linewidth(2)
plt.gca().spines['bottom'].set_linewidth(2)
plt.gca().spines['right'].set_linewidth(2)
plt.gca().spines['top'].set_linewidth(2)
ax10.tick_params(direction='out', width=3)
ax10.grid(axis='y', linestyle='-.', alpha=0.2)
plt.show()

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值