我有 n_series 记录具有相同的帧 0, 1, 2, 3,... 并且想从中制作一个 2D 轮廓。
我发现我可以很容易地做到以下几点:
import matplotlib.pyplot as plt
import numpy as np
series_len = 1000
n_series = 10
y = np.random.normal(0, 0.15, series_len * n_series)
x = np.tile(np.arange(0, series_len, 1), n_series)
heatmap, xbins, ybins = np.histogram2d(x, y, bins=20)
plt.contourf(heatmap.T)
plt.show()
但是因为这只是给出了一个 20x20 的直方图,我不知道我的强度在输出图中是如何分布的(例如大致以零为中心),也不知道如何修复刻度。
最佳答案
尝试set_xticklabels
:
series_len = 1000
n_series = 10
fig, ax = plt.subplots(figsize=(10,6))
np.random.seed(1)
y = np.random.normal(0, 0.15, series_len * n_series)
x = np.tile(np.arange(0, series_len, 1), n_series)
heatmap, xs, ys = np.histogram2d(x, y, bins=20)
fig, ax = plt.subplots(figsize=(10,6))
ax.contourf(heatmap.T)
# the actual x-axis and y-axis are from 0 to 19
# we want to put 11 ticks on the axis
ax.set_xticks(np.linspace(0,19,11))
ax.set_xticklabels(range(0,1001,100))
ax.set_yticks(np.linspace(0,19,11))
ax.set_yticklabels(['{:.3f}'.format(y) for y in ys[::2]])
plt.show()
输出:
关于python - 多个时间序列的 Matplotlib 热图显示随时间的分布,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56361103/