步骤

功能

具体操作

目标

第一步

库包 引用

import numpy as np

import pandas as pd

import matplotlib.pyplot as plt

import matplotlib.ticker as mticker

from matplotlib.dates import MonthLocator, DateFormatter

plt.rcParams['font.sans-serif'] = ['SimHei']

plt.rcParams['axes.unicode_minus'] = False

①引导学生分析问题、选择库包;

②熟悉各库包的引用方式;

③坐标横轴时间轴的显示需要的函数;

④掌握如何正确显示中文与负号的设置。

第二步

数据 读取

df = pd.read_csv(r"D:\ersst5.nino.mth.91-20.ascii", sep = "\s+")

nino34_anom = df['ANOM.3']

nino34_anom.plot()

①掌握pandas提供的文本文件读取函数read_csv();

②列的方式提取所需数据;

③简单应用plot()函数初步展示数据,分析需改进的地方。

第三步

数据 处理

y = nino34_anom[768:804]

tm_rng = pd.date_range('20131231', periods = 36, freq = 'M')

①掌握一维数组的切片功能;

②掌握pandas提供的时间序列的生成函数date_range()。

第四步

可视化

fig = plt.figure(figsize = (10, 4), dpi = 800)

ax = fig.add_subplot(111)

ax.bar(tm_rng, y, width = 15, color = np.where(y > 0.5, 'red', 'black'), edgecolor = 'b', linewidth = 0.5)

ax.xaxis.set_major_locator(MonthLocator(interval = 6))

ax.xaxis.set_major_formatter(DateFormatter("%Y-%m"))

ax.tick_params(axis = 'both', which = 'both', direction = 'in')

ax.yaxis.set_minor_locator(mticker.MultipleLocator(0.5))

ax.axhline(y = 0.0, c = 'k', ls = '-', lw = 1)

ax.axhline(y = 0.5, c = 'k', ls = '--', lw = 1)

ax.set_ylim(-3.0, 3.0)

ax.set_xlabel('年-月')

ax.set_ylabel('SSTA (℃)')

①熟练掌握绘图四步曲(画布,绘图区add_subplot(),绘图,辅助设置);

②掌握柱状图绘制函数bar();

③熟悉where()函数的灵活应用;

④掌握辅助线的添加函数axhline();

⑤了解时间横轴显示设计;

⑥掌握纵坐标范围设置函数ylim();

⑦了解如何添加横纵坐标名称;

⑧了解次坐标轴刻度的设置函数。

第五步

结果 保存

fig.savefig('D:/2014-2016.png', dpi = 800, bbox_inches = "tight")

①应用savefig()命令保存图片;

②引导学生对图片进行简单分析。