使用带有日期时间数据的 pcolor 和 pcolormesh 的着色选项 - 日期插值问题
Shading options using pcolor and pcolormesh with datetime data - date interpolation issues
我有一个大型数据集,其中包含不同时间不同高度的温度。
我希望使用 pcolormesh 将其绘制为热图。
当我这样做时,我收到一条警告 MatplotlibDeprecationWarning: shading='flat' when X and Y have the same dimensions as C is deprecated since 3.3. Either specify the corners of the quadrilaterals with X and Y, or pass shading='auto', 'nearest' or 'gouraud', or set rcParams['pcolor.shading']. This will become an error two minor releases later.
因此,我尝试传递选项 shading='nearest'
以确保保留所有数据(shading='flat'
删除行和列)。这给了我 numpy.core._exceptions.UFuncTypeError: Cannot cast ufunc 'greater_equal' input 0 from dtype('<m8[ns]') to dtype('<m8') with casting rule 'same_kind'
.
形式的错误
在我看来,将四边形角点插值应用于日期数据存在问题(我知道这样做会很棘手),但我一直在搜索,但找不到完成这项工作的方法。我可以尝试将日期完全转换为秒并作为整数进行插值,但我希望它的当前形式在轴刻度上。
有没有简单的解决办法?
TIA
复制错误的简单片段如下:
import matplotlib.pyplot as plt
import pandas as pd
data = [
['2018-12-26 00:00:00',0,5,10],
['2018-12-26 06:00:00',1,6,9 ],
['2018-12-26 12:00:00',2,7,8 ],
['2018-12-26 18:00:00',3,8,7 ],
['2018-12-27 00:00:00',4,9,6 ],
]
df = pd.DataFrame(data)
df.columns = ['date', 0, 10, 20]
df['date'] = pd.to_datetime(df['date'])
dates = df['date']
heights = df.iloc[:,1:].columns
temp = df.iloc[:,1:].transpose()
fig = plt.figure()
im = plt.pcolormesh(dates, heights, temp,
shading='nearest')
我建议使用 Seaborn 库来创建热图。
这是我使用您的示例数据的解决方案:
# Import packages
import seaborn
import numpy as np
import matplotlib.pyplot as plt
# Create sample data
data = [
['2018-12-26 00:00:00',0,5,10],
['2018-12-26 06:00:00',1,6,9 ],
['2018-12-26 12:00:00',2,7,8 ],
['2018-12-26 18:00:00',3,8,7 ],
['2018-12-27 00:00:00',4,9,6 ],
]
# Load sample into dataframe
df = pd.DataFrame(data)
df.columns = ['date', 0, 10, 20]
temp = df.iloc[:,1:].transpose()
# Convert data to numpy array, AND flip the data (and later y tick labels)
# so that the height is in increasing order from bottom to top in the heatmap
data2 = temp.to_numpy()
data2 = np.flip(data2, axis=0)
# Create empty figure
plt.figure(figsize=(15,5))
# Create heatmap (with customized colorscales, x/y ticks, line color, etc.)
seaborn.heatmap(data2, annot=True, linewidths=.5, linecolor='k', square=False,
xticklabels=df.date, yticklabels=np.flip(temp.index), cbar_kws={'label': 'Temperature'},
vmin=np.amin(data2), vmax=np.amax(data2), cmap='hot')
# Add axis labels/rotations and title to the plot
plt.title('Temperature at Different Heights and Times')
plt.xlabel('Date')
plt.xticks(rotation=0)
plt.ylabel('Height')
plt.yticks(rotation=0)
我能够使用完整的日期时间字符串作为 x 刻度。另外,由于您要处理温度,因此我选择了 'Hot' 色阶。
这是我的代码的输出:
正如 Jody Klymak 在上面的评论中所指出的,这个问题已经在较新版本的 Matplotlib 中得到解决。
我是 运行 版本 3.3.3,我的原始代码现在可以正常运行了。
我有一个大型数据集,其中包含不同时间不同高度的温度。 我希望使用 pcolormesh 将其绘制为热图。
当我这样做时,我收到一条警告 MatplotlibDeprecationWarning: shading='flat' when X and Y have the same dimensions as C is deprecated since 3.3. Either specify the corners of the quadrilaterals with X and Y, or pass shading='auto', 'nearest' or 'gouraud', or set rcParams['pcolor.shading']. This will become an error two minor releases later.
因此,我尝试传递选项 shading='nearest'
以确保保留所有数据(shading='flat'
删除行和列)。这给了我 numpy.core._exceptions.UFuncTypeError: Cannot cast ufunc 'greater_equal' input 0 from dtype('<m8[ns]') to dtype('<m8') with casting rule 'same_kind'
.
在我看来,将四边形角点插值应用于日期数据存在问题(我知道这样做会很棘手),但我一直在搜索,但找不到完成这项工作的方法。我可以尝试将日期完全转换为秒并作为整数进行插值,但我希望它的当前形式在轴刻度上。
有没有简单的解决办法? TIA
复制错误的简单片段如下:
import matplotlib.pyplot as plt
import pandas as pd
data = [
['2018-12-26 00:00:00',0,5,10],
['2018-12-26 06:00:00',1,6,9 ],
['2018-12-26 12:00:00',2,7,8 ],
['2018-12-26 18:00:00',3,8,7 ],
['2018-12-27 00:00:00',4,9,6 ],
]
df = pd.DataFrame(data)
df.columns = ['date', 0, 10, 20]
df['date'] = pd.to_datetime(df['date'])
dates = df['date']
heights = df.iloc[:,1:].columns
temp = df.iloc[:,1:].transpose()
fig = plt.figure()
im = plt.pcolormesh(dates, heights, temp,
shading='nearest')
我建议使用 Seaborn 库来创建热图。
这是我使用您的示例数据的解决方案:
# Import packages
import seaborn
import numpy as np
import matplotlib.pyplot as plt
# Create sample data
data = [
['2018-12-26 00:00:00',0,5,10],
['2018-12-26 06:00:00',1,6,9 ],
['2018-12-26 12:00:00',2,7,8 ],
['2018-12-26 18:00:00',3,8,7 ],
['2018-12-27 00:00:00',4,9,6 ],
]
# Load sample into dataframe
df = pd.DataFrame(data)
df.columns = ['date', 0, 10, 20]
temp = df.iloc[:,1:].transpose()
# Convert data to numpy array, AND flip the data (and later y tick labels)
# so that the height is in increasing order from bottom to top in the heatmap
data2 = temp.to_numpy()
data2 = np.flip(data2, axis=0)
# Create empty figure
plt.figure(figsize=(15,5))
# Create heatmap (with customized colorscales, x/y ticks, line color, etc.)
seaborn.heatmap(data2, annot=True, linewidths=.5, linecolor='k', square=False,
xticklabels=df.date, yticklabels=np.flip(temp.index), cbar_kws={'label': 'Temperature'},
vmin=np.amin(data2), vmax=np.amax(data2), cmap='hot')
# Add axis labels/rotations and title to the plot
plt.title('Temperature at Different Heights and Times')
plt.xlabel('Date')
plt.xticks(rotation=0)
plt.ylabel('Height')
plt.yticks(rotation=0)
我能够使用完整的日期时间字符串作为 x 刻度。另外,由于您要处理温度,因此我选择了 'Hot' 色阶。
这是我的代码的输出:
正如 Jody Klymak 在上面的评论中所指出的,这个问题已经在较新版本的 Matplotlib 中得到解决。
我是 运行 版本 3.3.3,我的原始代码现在可以正常运行了。