Generate a heatmap in MatPlotLib using a scatter dataset

My question is almost exactly like this one . However , I am not satisfied with the answers, because I want to create a real heat map without explicitly extracting the data.

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

How do I implement this with matplotlib?

EDIT : Basically, I did this this . The result is here . I would like to save everything, axis, name, labels and so on. Basically, just change the plot as I described, while re-introducing as little as possible.

+4
source share
1 answer

Convert time series data to a number format using matplotlib.dats.date2num. Put a rectangular grid that spans your x and y ranges and will convolve in that section. Create a pseudo-color graph of your convolution, and then reformat the x-labels for the date.

Label formatting is a bit dirty, but well documented enough. You just need to replace AutoDateFormatter with DateFormatter and the corresponding formatting string.

You will need to configure convolution constants 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

+13
source

All Articles