如何将hist2d xaxis刻度设置为日期时间格式?

问题描述 投票:0回答:1

我想将unixtime转换为datetime格式,以使用hist2d显示以下数据。但是,每次我将unixtime转换为datetime

我得到了“ TypeError:输入类型不支持ufunc'isfinite',并且根据转换规则” safe”,不能将输入安全地强制转换为任何受支持的类型。

我要绘制的数据:

The data I want to display

这是我将unixtime用作xsticks时绘制的内容:

This is what I plotted if I put unixtime as xsticks

这是应该的情节This is supposed to be

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import LinearSegmentedColormap

# example data
x = df_1['unix_time']
y = df_1['bid_ask_spread']

# Set up x_ticks
time_frame = np.linspace(1575254259187, 1575513459187, 73)
x_ticks = []
for i in time_frame:
    x_ticks.append(to_datetime(i))

# make a custom colormap with transparency
ncolors = 256
color_array = plt.get_cmap('YlOrRd')(range(ncolors))
color_array[:, -1] = np.linspace(0, 1, ncolors)
cmap = LinearSegmentedColormap.from_list(name='YlOrRd_alpha', colors=color_array)

fig, ax1 = plt.subplots(1, 1, figsize=(16,9), dpi=80)

ax1.hist2d(x, y, bins=[71, 81], cmap=cmap, edgecolor='white')

# ax1.set_xticks(x_ticks[::5])
ax1.set_ylim(bottom=0)

plt.show()
python datetime matplotlib histogram
1个回答
1
投票
不支持ufunc'isfinite'。

[我的感觉是,您需要使用numpy.histogram2d计算直方图,然后以datetime格式转换边,最后绘制直方图与转换后的坐标。

© www.soinside.com 2019 - 2024. All rights reserved.