使用imshow的matplotlib图在xaxis中的日期 [英] Dates in the xaxis for a matplotlib plot with imshow

查看:193
本文介绍了使用imshow的matplotlib图在xaxis中的日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我是使用matplotlib编程的新手.我已经使用imshow()和数组创建了一个颜色图.起初,轴只是我数组的行号和列号.我使用extent =(xmin,xmax,ymin,ymax)分别获得Unix时间和海拔高度的x轴.

So I am new to programming with matplotlib. I have created a color plot using imshow() and an array. At first the axis were just the row and column number of my array. I used extent = (xmin,xmax,ymin,ymax) to get the x-axis in unix time and altitude, respectively.

我想将x轴从unix时间(982376726,982377321)更改为UT(02:25:26,02:35:21).我已经在HH:MM:SS中创建了时间范围的列表.我不确定如何用这些新数字替换当前的x轴,而又不更改颜色图(或使其消失).

I want to change the x-axis from unix time (982376726,982377321) to UT(02:25:26, 02:35:21). I have created a list of the time range in HH:MM:SS. I am not sure how to replace my current x-axis with these new numbers, without changing the color plot (or making it disappear).

我当时在看datetime.time,但我对此感到困惑.

I was looking at datetime.time but I got confused with it.

任何帮助将不胜感激!

推荐答案

我整理了一些示例代码,这些代码可以帮助您解决问题.

I have put together some example code which should help you with your problem.

代码首先使用numpy.random生成一些随机数据.然后,它会计算x极限和y极限,其中x极限将基于问题中给出的两个unix时间戳,而y极限只是通用数字.

The code first generates some randomised data using numpy.random. It then calculates your x-limits and y-limits where the x-limits will be based off of two unix timestamps given in your question and the y-limits are just generic numbers.

然后,代码绘制随机数据并使用pyplot方法将x轴格式转换为表示良好的字符串(而不是unix时间戳或数组编号).

The code then plots the randomised data and uses pyplot methods to convert the x-axis formatting to nicely represented strings (rather than unix timestamps or array numbers).

该代码注释良好,应解释您所需的所有内容,否则请注释并要求澄清.

The code is well commented and should explain everything you need, if not please comment and ask for clarification.

import numpy as np
import matplotlib.pyplot as plt

import matplotlib.dates as mdates

import datetime as dt

# Generate some random data for imshow
N = 10
arr = np.random.random((N, N))

# Create your x-limits. Using two of your unix timestamps you first
# create a list of datetime.datetime objects using map.
x_lims = list(map(dt.datetime.fromtimestamp, [982376726, 982377321]))

# You can then convert these datetime.datetime objects to the correct
# format for matplotlib to work with.
x_lims = mdates.date2num(x_lims)

# Set some generic y-limits.
y_lims = [0, 100]

fig, ax = plt.subplots()

# Using ax.imshow we set two keyword arguments. The first is extent.
# We give extent the values from x_lims and y_lims above.
# We also set the aspect to "auto" which should set the plot up nicely.
ax.imshow(arr, extent = [x_lims[0], x_lims[1],  y_lims[0], y_lims[1]], 
          aspect='auto')

# We tell Matplotlib that the x-axis is filled with datetime data, 
# this converts it from a float (which is the output of date2num) 
# into a nice datetime string.
ax.xaxis_date()

# We can use a DateFormatter to choose how this datetime string will look.
# I have chosen HH:MM:SS though you could add DD/MM/YY if you had data
# over different days.
date_format = mdates.DateFormatter('%H:%M:%S')

ax.xaxis.set_major_formatter(date_format)

# This simply sets the x-axis data to diagonal so it fits better.
fig.autofmt_xdate()

plt.show()

这篇关于使用imshow的matplotlib图在xaxis中的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆