我想将不同的空间天气指数可视化为热图或图像。目标是在 y 轴上绘制小时数,在 x 轴上绘制日期,并将空间天气的强度绘制为每天的值。请看一下给定的图以了解我到底想要实现什么。
这是我正在使用的数据:https://drive.google.com/file/d/1cz5i2n6zGGsQVDejRpteai8Zs07_58vL/view?usp=sharing
我的目标:
我想从 pcolormesh、pcolor、imshow 或 seaborn 的热图中制作任何图形。
记住,KP > 40 = 类似的风暴,dst < -30 = storm.
地震日是2020/09/01
我不知道如何将日期时间/小时和值绘制为 pcolormesh、imshow、pcolor 或 heatmap。我很困惑如何去做。
这是我到目前为止所做的!
X,Y = np.meshgrid(sw_indices.index.day, sw_indices.index.hour)
from scipy.interpolate import griddata
Z = griddata((sw_indices.index.day, sw_indices.index.hour),
sw_indices.kp, (X,Y), method="nearest")
fig, ax = plt.subplots()
ax.pcolormesh(X, Y, Z)
下面的示例读取数据、格式化数据,然后使用
imshow
绘制它。
import pandas as pd
from matplotlib import pyplot as plt, dates as mdates
import numpy as np
#
#Load and format data
#
df = pd.read_csv('spaceWeather.csv')[1:]
df = df.rename(columns={'Unnamed: 0': 'date'})
df.date = pd.to_datetime(df.date)
df = df.sort_values(by='date')
# df[['year', 'doy', 'hr']] = df[['year', 'doy', 'hr']].astype(int)
df['year'] = df['date'].dt.year
df['doy'] = df['date'].dt.day_of_year
df['hr'] = df['date'].dt.hour
#Store data in numpy array "data_array"
n_hours = 24
n_days = df.doy.unique().size
data_array = np.zeros((n_hours, n_days)) * np.NaN
dates = []
for i, day in enumerate(df.doy.unique()):
day_df = df[df['date'].dt.day_of_year == day]
data_array[:len(day_df), i] = day_df['dst']
dates.append(day_df.date.iloc[0])
#
#Plot
#
f, ax = plt.subplots(figsize=(11, 3))
im = ax.imshow(
data_array,
cmap='jet_r',
vmin=-40, #adjust colorbar range - comment if auto-scale
vmax=20, #
interpolation='none',
aspect='auto',
origin='lower',
extent=[dates[0], dates[-1], 0, n_hours - 1]
)
#Format x axis with dates:
ax.set_xticks(mdates.date2num(dates), dates)
ax.xaxis.set_major_formatter(mdates.DateFormatter(fmt='%b %d'))
ax.tick_params(axis='x', rotation=90)
# ax.set_xlabel('date')
#Format y axis
ax.set_yticks([0, 6, 12, 18, 23])
ax.set_ylabel('UT')
#Add colorbar
ax_pos = ax.get_position()
cax = f.add_axes([
ax_pos.x0 + ax_pos.width * 1.02, ax_pos.y0, ax_pos.width / 30, ax_pos.height
])
f.colorbar(im, cax=cax, label='nT')
#Title
ax.set_title('Dst', fontweight='bold')