本期使用joypy对降水数据处理,探索气象可视化的多元性。
降水数据是气象学中一个重要的指标,它对于了解和预测天气变化非常关键。在本期中,我们将使用 joypy 库来进行降水数据的可视化,展示降水的变化趋势和分布情况。
joypy 是一个基于 matplotlib 的 Python 库,用于创建美观的峰峦图(Ridge Plot)。峰峦图可以同时显示多个组别的分布情况,非常适合用于展示降水数据的多元性。
在接下来的代码中,我们将使用 joypy 来创建降水数据的峰峦图,并展示降水的季节变化和年际变化。让我们开始吧! joypy地址:https://github.com/sbebo/joypy 镜像:气象分析3.9
In [42]:
!pip install joypy -i https://pypi.mirrors.ustc.edu.cn/simple/
Looking in indexes: https://pypi.mirrors.ustc.edu.cn/simple/
Requirement already satisfied: joypy in /opt/conda/lib/python3.9/site-packages (0.2.6)
Requirement already satisfied: pandas>=0.20.0 in /opt/conda/lib/python3.9/site-packages (from joypy) (1.5.3)
Requirement already satisfied: numpy>=1.16.5 in /opt/conda/lib/python3.9/site-packages (from joypy) (1.24.4)
Requirement already satisfied: scipy>=0.11.0 in /opt/conda/lib/python3.9/site-packages (from joypy) (1.10.1)
Requirement already satisfied: matplotlib in /opt/conda/lib/python3.9/site-packages (from joypy) (3.7.2)
Requirement already satisfied: python-dateutil>=2.8.1 in /opt/conda/lib/python3.9/site-packages (from pandas>=0.20.0->joypy) (2.8.1)
Requirement already satisfied: pytz>=2020.1 in /opt/conda/lib/python3.9/site-packages (from pandas>=0.20.0->joypy) (2021.1)
Requirement already satisfied: six>=1.5 in /opt/conda/lib/python3.9/site-packages (from python-dateutil>=2.8.1->pandas>=0.20.0->joypy) (1.16.0)
Requirement already satisfied: cycler>=0.10 in /opt/conda/lib/python3.9/site-packages (from matplotlib->joypy) (0.10.0)
Requirement already satisfied: pillow>=6.2.0 in /opt/conda/lib/python3.9/site-packages (from matplotlib->joypy) (8.4.0)
Requirement already satisfied: fonttools>=4.22.0 in /opt/conda/lib/python3.9/site-packages (from matplotlib->joypy) (4.42.1)
Requirement already satisfied: pyparsing<3.1,>=2.3.1 in /opt/conda/lib/python3.9/site-packages (from matplotlib->joypy) (2.4.7)
Requirement already satisfied: importlib-resources>=3.2.0 in /opt/conda/lib/python3.9/site-packages (from matplotlib->joypy) (5.4.0)
Requirement already satisfied: kiwisolver>=1.0.1 in /opt/conda/lib/python3.9/site-packages (from matplotlib->joypy) (1.3.1)
Requirement already satisfied: contourpy>=1.0.1 in /opt/conda/lib/python3.9/site-packages (from matplotlib->joypy) (1.1.1)
Requirement already satisfied: packaging>=20.0 in /opt/conda/lib/python3.9/site-packages (from matplotlib->joypy) (23.1)
Requirement already satisfied: zipp>=3.1.0 in /opt/conda/lib/python3.9/site-packages (from importlib-resources>=3.2.0->matplotlib->joypy) (3.4.1)
In [1]:
import joypy
import pandas as pd
import numpy as np
from matplotlib import pyplot as plt
from matplotlib import cm
from sklearn.datasets import load_iris
np.random.seed(42)
df = pd.DataFrame(np.random.poisson(10,(24,7)))
df.columns = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
df.head()
Monday | Tuesday | Wednesday | Thursday | Friday | Saturday | Sunday | |
---|---|---|---|---|---|---|---|
0 | 12 | 6 | 11 | 14 | 7 | 8 | 9 |
1 | 11 | 8 | 10 | 7 | 11 | 11 | 8 |
2 | 12 | 6 | 7 | 8 | 10 | 5 | 3 |
3 | 9 | 13 | 8 | 6 | 13 | 12 | 13 |
4 | 9 | 14 | 8 | 8 | 12 | 15 | 13 |
In [2]:
%matplotlib inline
x_range = list(range(24))
fig, axes = joypy.joyplot(df, kind="values", x_range=x_range)
axes[-1].set_xticks(x_range);
In [3]:
time = pd.date_range(start='2000-01-01', end='2023-12-31', freq='d')
np.random.seed(42)
rain = np.random.uniform(0, 100, size=len(time))
pre = pd.DataFrame({'datetime':time, 'rain(mm)':rain})
print(pre)
datetime rain(mm)
0 2000-01-01 37.454012
1 2000-01-02 95.071431
2 2000-01-03 73.199394
3 2000-01-04 59.865848
4 2000-01-05 15.601864
... ... ...
8761 2023-12-27 67.136801
8762 2023-12-28 52.315815
8763 2023-12-29 89.863893
8764 2023-12-30 16.439268
8765 2023-12-31 80.410925
[8766 rows x 2 columns]
先设置一个极端降水阈值。设为95%。
In [4]:
# 将日期列设置为索引:
pre['datetime'] = pd.to_datetime(pre['datetime'])
pre.set_index('datetime', inplace=True)
# 计算极端降水阈值:
threshold = pre['rain(mm)'].quantile(0.95)
# 按年份分组,然后计算每月的极端降水日数:
extreme_precipitation_days = pre[pre['rain(mm)'] > threshold].resample('M').size()
# 添加年份和极端降水日数到原始表格中:
pre['year'] = pre.index.year
pre['quarter'] = pre.index.quarter
pre['month'] = pre.index.month
pre['extreme_precipitation_days'] = extreme_precipitation_days
这样你就可以得到一个包含每年极端降水日数的pandas
Series对象extreme_precipitation_days
。
In [5]:
pre
rain(mm) | year | quarter | month | extreme_precipitation_days | |
---|---|---|---|---|---|
datetime | |||||
2000-01-01 | 37.454012 | 2000 | 1 | 1 | NaN |
2000-01-02 | 95.071431 | 2000 | 1 | 1 | NaN |
2000-01-03 | 73.199394 | 2000 | 1 | 1 | NaN |
2000-01-04 | 59.865848 | 2000 | 1 | 1 | NaN |
2000-01-05 | 15.601864 | 2000 | 1 | 1 | NaN |
... | ... | ... | ... | ... | ... |
2023-12-27 | 67.136801 | 2023 | 4 | 12 | NaN |
2023-12-28 | 52.315815 | 2023 | 4 | 12 | NaN |
2023-12-29 | 89.863893 | 2023 | 4 | 12 | NaN |
2023-12-30 | 16.439268 | 2023 | 4 | 12 | NaN |
2023-12-31 | 80.410925 | 2023 | 4 | 12 | 2.0 |
8766 rows × 5 columns
In [6]:
%matplotlib inline
labels=[y if y%1==0 else None for y in list(pre.year.unique())]
fig, axes = joypy.joyplot(pre, by="year", column="extreme_precipitation_days", labels=labels, range_style='own',
grid="y", linewidth=1, legend=False, fade=True, figsize=(20,12),
title="joypy preciption test",
kind="counts", bins=50)
显示效果欠佳,还是直接看降水量分布
In [7]:
# 创建山峦图
fig, ax = joypy.joyplot(pre,
by='year',
column='rain(mm)',
range_style='own',
grid='y',
linewidth=1,
fade=True,
legend=False,
kind="counts", bins=30,
figsize=(20,12),
title='Precipitation Over Years')
设置一下x轴范围,画一下年分布、季度分布、月分布
In [8]:
fig, ax = joypy.joyplot(pre,
by='year',
column='rain(mm)',
grid='y',
linewidth=1,
fade=True,
legend=False,
range_style='all',
x_range=[0,110],
kind="counts", bins=50,
figsize=(20,12),
title='Precipitation Over Years')
In [11]:
fig, ax = joypy.joyplot(pre,
by='quarter',
column='rain(mm)',
grid='y',
linewidth=0.8,
fade=True,
legend=False,
range_style='all',
x_range=[0,110],
kind="counts", bins=50,
figsize=(20,12),
title='Precipitation Over quarter ')
/opt/conda/lib/python3.9/site-packages/joypy/joyplot.py:582: UserWarning: Glyph 9 ( ) missing from current font.
fig.tight_layout(h_pad=h_pad)
/opt/conda/lib/python3.9/site-packages/IPython/core/events.py:89: UserWarning: Glyph 9 ( ) missing from current font.
func(*args, **kwargs)
/opt/conda/lib/python3.9/site-packages/IPython/core/pylabtools.py:134: UserWarning: Glyph 9 ( ) missing from current font.
fig.canvas.print_figure(bytes_io, **kw)
In [10]:
fig, ax = joypy.joyplot(pre,
by='month',
column='rain(mm)',
grid='y',
linewidth=0.8,
fade=True,
legend=False,
range_style='all',
x_range=[0,110],
kind="counts", bins=50,
figsize=(20,12),
title='Precipitation Over months')
点击链接可在线运行