【发布时间】:2020-04-05 04:32:00
【问题描述】:
我想将 unixtime 转换为 datetime 格式以使用 hist2d 显示以下数据。 但是,每次我将 unixtime 转换为 datetime 时
我得到“TypeError: ufunc 'isfinite' not supported for the input types, and the input could not be safe to force to any supported types based on the cast rule ''safe''”
我要绘制的数据:
如果我将 unixtime 作为 xsticks,这就是我绘制的:
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()
【问题讨论】:
-
matplotlib.dates.DateLocator()? -
我会试试的。还在寻找答案,谢谢!
-
不是答案,但这些看起来不像 unix_timestamps。
1575254280000对应于 51887 年的某个时间。你来自未来吗? -
@DizietAsahi 哈哈。抱歉,我忘了说这是以毫秒为单位的 unix_time。
标签: python datetime matplotlib histogram