您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

使用散点数据集在MatPlotLib中生成热图

使用散点数据集在MatPlotLib中生成热图

使用matplotlib.dats.date2num将时间序列数据转换为数字格式。放下一个跨越x和y范围的矩形网格,并在该图上进行卷积。绘制卷积的伪彩色图,然后将x标签重新格式化为日期。

标签格式有点凌乱,但有据可查。您只需要用DateFormatter和适当的格式设置字符串替换AutoDateFormatter。

您需要为数据调整卷积中的常量。

import numpy as np
import datetime as dt
import pylab as plt
import matplotlib.dates as dates

t0 = dt.date.today()
t1 = t0+dt.timedelta(days=10)

times = np.linspace(dates.date2num(t0), dates.date2num(t1), 10)
dt = times[-1]-times[0]
price =  100 - (times-times.mean())**2
dp = price.max() - price.min()
volume = np.linspace(1, 100, 10)

tgrid = np.linspace(times.min(), times.max(), 100)
pgrid = np.linspace(70, 110, 100)
tgrid, pgrid = np.meshgrid(tgrid, pgrid)
heat = np.zeros_like(tgrid)

for t,p,v in zip(times, price, volume):
    delt = (t-tgrid)**2
    delp = (p-pgrid)**2
    heat += v/( delt + delP*1.e-2 + 5.e-1 )**2

fig = plt.figure()
ax = fig.add_subplot(111)
ax.pcolormesh(tgrid, pgrid, heat, cmap='gist_heat_r')

plt.scatter(times, price, volume, marker='x')

locator = dates.DayLocator()
ax.xaxis.set_major_locator(locator)
ax.xaxis.set_major_formatter(dates.AutoDateFormatter(locator))
fig.autofmt_xdate()

plt.show()
其他 2022/1/1 18:46:30 有412人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶