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

发布于 2024-11-16 02:29:06 字数 574 浏览 2 评论 0原文

我的问题几乎与这个问题完全相同但是,我对答案不满意,因为我想生成实际的热图,而不明确对数据进行分箱。

准确地说,我想显示散点数据和自定义内核之间卷积的结果函数,例如 1/x^2。

我应该如何用 matplotlib 实现这个?

编辑:基本上,我所做的是这个< /a>.结果在此处。我想保留所有内容,轴、标题、标签等等。基本上只需将情节更改为我所描述的那样,同时尽可能少地重新实现。

My question is almost exactly similar to this one. However, I'm not satisfied with the answers, because I want to generate an actual heatmap, without explicitely binning the data.

To be precise, I would like to display the function that is the result of a convolution between the scatter data and a custom kernel, such as 1/x^2.

How should I implement this with matplotlib?

EDIT: Basically, what I have done is this. The result is here. I'd like to keep everything, the axis, the title, the labels and so on. Basically just change the plot to be like I described, while re-implementing as little as possible.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

时常饿 2024-11-23 02:29:06

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

标签格式有点混乱,但记录得相当好。您只需将 AutoDateFormatter 替换为 DateFormatter 和适当的格式字符串即可。

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

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()

脚本输出

Convert your time series data into a numeric format with matplotlib.dats.date2num. Lay down a rectangular grid that spans your x and y ranges and do your convolution on that plot. Make a pseudo-color plot of your convolution and then reformat the x labels to be dates.

The label formatting is a little messy, but reasonably well documented. You just need to replace AutoDateFormatter with DateFormatter and an appropriate formatting string.

You'll need to tweak the constants in the convolution for your data.

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()

Script output

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文