首页 > 解决方案 > 以 x 轴为时间绘制图像

问题描述

我正在尝试绘制一个形状为 (x, 88, 1) 的张量,其中 x 约为 10,000。x 处的每个值都是间隔为 0.028 秒的时间点。

如何使用 matplotlib 将 x 的值绘制为以分钟为单位的时间:秒?目前我的图表出来像

在此处输入图像描述

示例代码

    plt.imshow(missing, aspect='auto', origin='lower', cmap='gray_r', vmin=0, vmax=1)
    plt.figure(1, figsize=(8.5, 11))
    plt.xlabel('Sample #')
    plt.colorbar()
    plt.clf()

谷歌不断产生结果绘制日期,这不是我需要的,所以我在这里问。

标签: pythonmatplotlib

解决方案


您可以首先将图像的范围设置为数据覆盖的秒数范围。然后,您可以使用FuncFormatter. 然后,您可以将刻度的位置设置为不错的数字,例如 30 秒间隔。

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.ticker as mticker

data = np.random.rand(88, 10000)

interval = .028  # seconds
extent = [0, data.shape[1]*interval, 0, data.shape[0]]

plt.imshow(data, extent=extent, aspect='auto', origin='lower', 
           cmap='gray_r', vmin=0, vmax=1)

# Format the seconds on the axis as min:sec
def fmtsec(x,pos):
    return "{:02d}:{:02d}".format(int(x//60), int(x%60)) 
plt.gca().xaxis.set_major_formatter(mticker.FuncFormatter(fmtsec))
# Use nice tick positions as multiples of 30 seconds
plt.gca().xaxis.set_major_locator(mticker.MultipleLocator(30))

plt.xlabel('Time')
plt.colorbar()
plt.show()

在此处输入图像描述


推荐阅读