python - 以 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()
谷歌不断产生结果绘制日期,这不是我需要的,所以我在这里问。
解决方案
您可以首先将图像的范围设置为数据覆盖的秒数范围。然后,您可以使用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()
推荐阅读
- java - Spring JPA 无法将参数传递到引号中
- python - 垃圾收集器会在 .read() 之后删除引用吗?
- npm - 如何将 react 和 react-redux 安装到我的项目中?
- r - 使用 plotly 和 shiny 制作交互式条形图有颜色和后退按钮
- javascript - 选择另一个下拉值时生成一个 select2 字段
- android - 带有嵌套底部导航的 Android 导航
- html - 响应式弹性盒
- reactjs - prevProps.xx 和 this.props.xx 始终相同,即使 xx 发生变化
- python - 无法通过python烧瓶访问mysql数据库
- python - 一个类的多个实例被覆盖