【问题标题】:Matplotlib heatmap for multiple timeseries to show distribution over time用于多个时间序列的 Matplotlib 热图以显示随时间的分布
【发布时间】:2019-10-15 02:20:33
【问题描述】:

我有 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 的直方图,我不知道我的强度在输出图中是如何分布的(例如,大致以零为中心),也不知道如何修复刻度。

我想要的是这个(“购物”):

【问题讨论】:

  • 我不确定,如果这是你想要的,但也许试试bins=200
  • 你的 x 刻度应该是什么样子的?
  • 我不知道我的强度是如何分布的:您希望如何查看强度分布?
  • 编辑主帖。希望现在更清楚了。

标签: python numpy matplotlib time-series


【解决方案1】:

试试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()

输出:

【讨论】:

  • 看起来很棒!但是,嗯...... xticks 没有正确排列,这意味着 yticks 可能也不是。
  • 没错,问题是你的heatmap20x20 所以默认情况下ax 在每个轴上有20 刻度,而xs 有21 个元素,正在修复
【解决方案2】:

IIUC,你想要这样的东西吗:

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, xlabels, ylabels = np.histogram2d(x, y, bins=20)

plt.contourf(xlabels[:-1], ylabels[:-1], heatmap.T)
plt.colorbar()
plt.show()

输出:

【讨论】:

  • 几乎,而且确实是一个非常简单的解决方案。但是,刻度线没有正确排列,如果您增加 n_series,您会看到。
【解决方案3】:

好的,我自己找到了一个答案,这使得这个过程比看起来要简单得多。只需使用 skimage 在两个方向将热图大小调整为 1 即可使一切顺利进行。

import matplotlib.pyplot as plt
import numpy as np
import skimage.transform

series_len = 1000
n_series = 10
bins = 20
y = np.random.normal(0, 0.15, series_len * n_series)
x = np.tile(np.arange(0, series_len, 1), n_series)

heatmap, xlabels, ylabels = np.histogram2d(x, y, bins=bins)
heatmap = skimage.transform.resize(heatmap, output_shape = (bins+1, bins+1), mode = "symmetric")

plt.contourf(xlabels, ylabels, heatmap.T)
plt.xlim(0, 1000)
plt.ylim(-0.5, 0.5)
plt.show()

【讨论】:

    猜你喜欢
    • 2021-01-29
    • 2020-11-17
    • 2018-12-27
    • 1970-01-01
    • 2020-12-30
    • 1970-01-01
    • 1970-01-01
    • 2020-10-27
    • 2020-09-23
    相关资源
    最近更新 更多