我想将unixtime转换为datetime格式,以便用hist2d显示以下数据。 但是,每次我将unixtime转换为datetime
我得到“TypeError:ufunc'isfinite'不支持输入类型,并且根据强制转换规则“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()
我的感觉是您需要使用
numpy.histogram2d
来计算直方图,然后以datetime
格式转换边,最后绘制直方图和转换后的坐标。你知道吗相关问题 更多 >
编程相关推荐