Python 如何将日期数组传递到pcolor绘图?
我有矩阵数据,其中一个轴与日期有关。但是,我在将此数据作为轴传递到Python 如何将日期数组传递到pcolor绘图?,python,datetime,numpy,matplotlib,plot,Python,Datetime,Numpy,Matplotlib,Plot,我有矩阵数据,其中一个轴与日期有关。但是,我在将此数据作为轴传递到pcolor时遇到问题。我的虚拟数据如下: In [219]: X = [datetime.date.today() + datetime.timedelta(days=i) for i in range(4)] In [220]: Y = arange(5) In [221]: Z = arange(4*5).reshape(4, 5) 原始尝试pcolor(Y,X,Z)失败,因为pcolor不喜欢获取列表对象: In [
pcolor
时遇到问题。我的虚拟数据如下:
In [219]: X = [datetime.date.today() + datetime.timedelta(days=i) for i in range(4)]
In [220]: Y = arange(5)
In [221]: Z = arange(4*5).reshape(4, 5)
原始尝试pcolor(Y,X,Z)
失败,因为pcolor
不喜欢获取列表
对象:
In [222]: pcolor(Y, X, Z)
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-222-1ece18b4bc13> in <module>()
----> 1 pcolor(Y, X, Z)
/export/data/home/gholl/venv/gerrit/lib/python3.4/site-packages/matplotlib/pyplot.py in pcolor(*args, **kwargs)
2926 ax.hold(hold)
2927 try:
-> 2928 ret = ax.pcolor(*args, **kwargs)
2929 draw_if_interactive()
2930 finally:
/export/data/home/gholl/venv/gerrit/lib/python3.4/site-packages/matplotlib/axes.py in pcolor(self, *args, **kwargs)
7545 shading = kwargs.pop('shading', 'flat')
7546
-> 7547 X, Y, C = self._pcolorargs('pcolor', *args, allmatch=False)
7548 Ny, Nx = X.shape
7549
/export/data/home/gholl/venv/gerrit/lib/python3.4/site-packages/matplotlib/axes.py in _pcolorargs(funcname, *args, **kw)
7357
7358 Nx = X.shape[-1]
-> 7359 Ny = Y.shape[0]
7360 if len(X.shape) != 2 or X.shape[0] == 1:
7361 x = X.reshape(1, Nx)
AttributeError: 'list' object has no attribute 'shape'
最后,将其转换为适当的numpy.datetime64
对象也不能解决这种情况,因为无效类型升级失败
:
In [224]: pcolor(Y, numpy.array(X, dtype="datetime64[D]"), Z)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-224-0ac06cfafa35> in <module>()
----> 1 pcolor(Y, numpy.array(X, dtype="datetime64[D]"), Z)
/export/data/home/gholl/venv/gerrit/lib/python3.4/site-packages/matplotlib/pyplot.py in pcolor(*args, **kwargs)
2926 ax.hold(hold)
2927 try:
-> 2928 ret = ax.pcolor(*args, **kwargs)
2929 draw_if_interactive()
2930 finally:
/export/data/home/gholl/venv/gerrit/lib/python3.4/site-packages/matplotlib/axes.py in pcolor(self, *args, **kwargs)
7577 X4[:, newaxis], Y4[:, newaxis],
7578 X1[:, newaxis], Y1[:, newaxis]),
-> 7579 axis=1)
7580 verts = xy.reshape((npoly, 5, 2))
7581
TypeError: invalid type promotion
[224]中的pcolor(Y,numpy.array(X,dtype=“datetime64[D]”,Z)
---------------------------------------------------------------------------
TypeError回溯(最近一次调用上次)
在()
---->1 pcolor(Y,numpy.array(X,dtype=“datetime64[D]”),Z)
/pcolor中的export/data/home/gholl/venv/gerrit/lib/python3.4/site-packages/matplotlib/pyplot.py(*args,**kwargs)
2926斧头保持(保持)
2927尝试:
->2928 ret=ax.pcolor(*args,**kwargs)
2929 draw_if_interactive()
2930最后:
/pcolor中的export/data/home/gholl/venv/gerrit/lib/python3.4/site-packages/matplotlib/axes.py(self,*args,**kwargs)
7577 X4[:,新轴]、Y4[:,新轴],
7578 X1[:,新轴]、Y1[:,新轴],
->7579轴=1)
7580顶点=xy。重塑形状((npoly,5,2))
7581
TypeError:无效的类型升级
正确的方法是什么?在
请注意,对的回答使用的是散点,而不是pcolor,因此对我的情况没有帮助。Matplotlib使用简单的浮点数来表示日期时间。因此,您必须首先转换它们,然后告诉轴必须将标签格式化为日期。Matplotlib为以下各项提供函数
date2num
:
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import datetime
import numpy as np
# Your original data (with adapted sizes)
x = [datetime.date.today() + datetime.timedelta(days=i) for i in range(4)]
y = np.arange(5)
z = np.arange(3*4).reshape(3, 4).T
# Convert to numbers
x = mdates.date2num(x)
# Create the figure
fig, ax = plt.subplots(1,1)
plt.pcolor(x,y,z)
# Setup the DateFormatter for the x axis
date_format = mdates.DateFormatter('%D')
ax.xaxis.set_major_formatter(date_format)
# Rotates the labels to fit
fig.autofmt_xdate()
plt.show()
其他一些评论:
- 对于
而言,x和y向量表示平铺的角点。因此,它们需要比数据长1个元素pcolor
- 提供了如何在matplotlib中处理日期的良好概述
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import datetime
import numpy as np
# Your original data (with adapted sizes)
x = [datetime.date.today() + datetime.timedelta(days=i) for i in range(4)]
y = np.arange(5)
z = np.arange(3*4).reshape(3, 4).T
# Convert to numbers
x = mdates.date2num(x)
# Create the figure
fig, ax = plt.subplots(1,1)
plt.pcolor(x,y,z)
# Setup the DateFormatter for the x axis
date_format = mdates.DateFormatter('%D')
ax.xaxis.set_major_formatter(date_format)
# Rotates the labels to fit
fig.autofmt_xdate()
plt.show()