日期与时间图上数据的颜色映射 [英] Color mapping of data on a date vs time plot

查看:123
本文介绍了日期与时间图上数据的颜色映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在2d图上绘制3个变量x,y,z,其中x(日期)在x轴上,y(时间)在y轴上,z(温度)用色标映射. 我在pandas Dataframe中有三个可用变量,并创建了一个带有datenumber的额外列,以便matplotlib可以使用它.

I am trying to plot 3 variables x,y,z on a 2d plot, with x (date) on the x axis, y (time) on the y axis and z (temperature) mapped with a colorscale. I have the three variables available within a pandas Dataframe and created an extra column with the datenumber so that matplotlib can work with it.

import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.dates as mdates

data=pd.DataFrame() 
data['datenum']=mdates.date2num(data['Date'])

示例:

            Date Time     Tgrad   datenum
0     2016-08-01   00 -0.841203  736177.0
1     2016-08-01   01 -0.629176  736177.0
2     2016-08-01   02 -0.623608  736177.0
3     2016-08-01   03 -0.615145  736177.0
4     2016-08-01   04 -0.726949  736177.0
5     2016-08-01   05 -0.788864  736177.0
6     2016-08-01   06 -0.794655  736177.0
7     2016-08-01   07 -0.775724  736177.0
8     2016-08-01   08 -0.677951  736177.0

我一直在尝试遵循以下建议:

I have been trying to follow this suggestions:

基于x,y,z值的matplotlib 2D图 使用imshow的matplotlib图在xaxis中的日期

但是由于我认为输入数据的形状错误,因此未能成功.我已经尝试过这样的事情:

But have not been successful due to the wrong shape of my input data I think. I have tried something like this:

fig, ax = plt.subplots()
ax.imshow(data['Tgrad'], extent = [min(data['datenum']), max(data['datenum']),min(data['Time']), max(data['Time'])], cmap="autumn", aspect = "auto")
ax.xaxis_date()

但是出现ValueError:

But get a ValueError:

ValueError: setting an array element with a sequence

是否有必要将数据作为numpy数组或任何其他类型?数据以其他格式保存后,该如何映射?

Is it necessary to have the data as numpy array or any other type? And how can I map the data once I have it in a different format?

感谢您的帮助. 弗罗尼

Thanks you for helping. Vroni

推荐答案

imshow需要2d数组作为输入.您需要将数据重新格式化为2d数组:Date x Time,其中Tgrad为值.熊猫通过pivot使得此操作相当容易.确实要求您具有间隔良好的数据点,即类似网格的数据集(每个DateTime值都相同).如果数据点没有整齐地分散在2d空间中,则链接的帖子将很有用.另外,由于matplotlib可以处理数据帧,因此无需转换为numpy数组.

imshow requires a 2d array as input. You'll need to reformat your data into a 2d array: Date x Time with Tgrad as your values. Pandas makes this fairly easy with pivot. It does require that you have nicely spaced data points, i.e., a grid-like data set (same Time values for each Date). The post you linked would be useful if data points were not neatly scattered in 2d space. Also, there's no need to convert to a numpy array as matplotlib can handle dataframes.

C = data.pivot(index='Time', columns='Date', values='Tgrad')

fig, ax = plt.subplots()
ax.imshow(C)

这篇关于日期与时间图上数据的颜色映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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