您还可以提供以下值wd_rad
仅仅低于0
并且大于2*pi
using np.where
,添加2*pi
对于小值并减去2*pi
对于大值。np.tile(ws, 2)
and np.tile(conc, 2)
然后关联扩展版本wd_rad
具有相同的浓度值。还可以使用这些扩展值interpolate.griddata
确保浓度值环绕在0
and 2*pi
.
顺便说一句,请注意“jet”是一个看起来不错的色彩图,但实际上非常具有误导性 https://jakevdp.github.io/blog/2014/10/16/how-bad-is-your-colormap/因为它会在错误的位置产生黄色高光。 (此外,将 pandas 列转换为列表非常慢且消耗内存,最好将它们保留为 numpy 数组格式。)
下面的代码假设oz
问题中的数组与conc
.
import matplotlib.pyplot as plt
import numpy as np
from scipy import interpolate
# wd = merge_all_apr['Wind Dir (10s deg)']
# conc = merge_all_apr['Mean_CO2']
# ws = merge_all_apr['Wind Spd (km/h)']
N = 100
wd = np.random.uniform(0, 360, N)
conc = np.random.uniform(0, 40, N)
ws = np.random.uniform(0, 45, N)
wd_rad = np.radians(np.array(wd))
conc = np.array(conc, dtype=np.float)
wd_rad_ext = np.where(wd_rad < np.pi, wd_rad + 2 * np.pi, wd_rad - 2 * np.pi)
wind_speed = np.linspace(min(ws), max(ws), 16)
WD, WS = np.meshgrid(np.linspace(0, 2 * np.pi, 37), wind_speed)
Z = interpolate.griddata((np.hstack([wd_rad, wd_rad_ext]), np.tile(ws, 2)), np.tile(conc, 2),
(WD, WS), method='linear', rescale=True)
fig, axes = plt.subplots(ncols=2, figsize=(10, 4), subplot_kw={"projection": "polar"})
cmap = plt.get_cmap('magma')
for ax in axes:
if ax == axes[0]:
img = ax.pcolormesh(WD, WS, Z, cmap=cmap, vmin=0, vmax=40)
else:
img = ax.scatter(wd_rad, ws, c=conc, cmap=cmap, vmin=0, vmax=40)
ax.set_theta_zero_location('N')
ax.set_theta_direction(-1)
plt.colorbar(img, ax=ax, pad=0.12)
plt.show()
如果您不想插值,但想绘制 10 度宽的线段来表示每个区域,plt.hist2d https://matplotlib.org/3.2.2/api/_as_gen/matplotlib.pyplot.hist2d.html可以就业。
特殊参数hist2d
:
-
bins=(np.linspace(0, 2 * np.pi, 37), np.linspace(min(ws), max(ws), 17))
:风向将被划分为36个区域(37个边界);速度将分为16个区域
-
weights=conc
:使用浓度,而不是通常的统计每个小区域的值数量的直方图;当在同一个小区域测量多个浓度时,它们会被平均
-
cmin=0.001
:当浓度值小于时将区域留空0.001
-
cmap = 'magma_r'
:使用反转的“岩浆”颜色图,因此高值变暗,低值变亮(请参阅the docs https://matplotlib.org/3.2.0/tutorials/colors/colormaps.html关于可能更适合说明数据的其他颜色图,但尽量不要使用“jet”)
返回值hist2d
是直方图值、bin 边界(x 和 y)和彩色块集合(可用作颜色图的输入)的矩阵。
import matplotlib.pyplot as plt
import numpy as np
from scipy import interpolate
N = 100
wd = np.random.uniform(0, 360, N)
conc = np.random.uniform(0, 40, N)
ws = np.random.uniform(0, 45, N)
wd_rad = np.radians(np.array(wd))
conc = np.array(conc, dtype=np.float)
fig, axes = plt.subplots(ncols=2, figsize=(10, 4), subplot_kw={"projection": "polar"})
cmap = 'magma_r'
for ax in axes:
if ax == axes[0]:
_, _, _, img = ax.hist2d(wd_rad, ws, bins=(np.linspace(0, 2 * np.pi, 37), np.linspace(min(ws), max(ws), 17)),
weights=conc, cmin=0.001, cmap=cmap, vmin=0, vmax=40)
else:
img = ax.scatter(wd_rad, ws, c=conc, cmap=cmap, vmin=0, vmax=40)
ax.set_theta_zero_location('N')
ax.set_theta_direction(-1)
plt.colorbar(img, ax=ax, pad=0.12)
plt.show()