6

Is it possible to add a datashader image to a set of matplotlib subplots?

As a concrete example,

import numpy as np
import pandas as pd
import matplotlib.pylab as plt

import datashader as ds
import datashader.transfer_functions as tf
from datashader.utils import export_image

from functools import partial

background = "white"
export = partial(export_image, background = background, export_path="export")

N = 10000
df = pd.DataFrame(np.random.random((N, 3)), columns = ['x','y', 'z'])

f, ax = plt.subplots(2, 2)
ax_r = ax.ravel()

ax_r[0].scatter(df['x'], df['y'], df['z'].mean())
ax_r[1].hist(df['x'])
ax_r[2].hist(df['y'])
ax_r[3].plot(df['z'])

cvs = ds.Canvas(plot_width=100, plot_height=100)
agg = cvs.points(df, 'x', 'y', ds.mean('z'))
a = export(tf.shade(agg, cmap=['lightblue', 'darkblue'], how='eq_hist'), 'test')

Where I have a two by two array of matplotlib subplots and would like to replace the [0,0] plot ax_r[0] in the above example with the datashader image a. Is this possible, and if so, how?

Thanks!

not link
  • 858
  • 1
  • 16
  • 29
  • 1
    Have a look and the thread in this PR: https://github.com/bokeh/datashader/pull/200 (If I had more time I would write up an answer.) – Steven C. Howell May 03 '17 at 20:14
  • 1
    @StevenC.Howell Thanks! I tried searching the Github repo for something similar but came up blank. I appreciate the link. – not link May 03 '17 at 20:33
  • 2
    I'm not a matplotlib user myself (except [via](https://anaconda.org/jbednar/census-hv-mpl/notebook) [HoloViews](https://anaconda.org/jbednar/holoviews_datashader/notebook)), but if anyone has time to finish up the DSArtist class as described in [PR 200](https://github.com/bokeh/datashader/pull/200), I'd be happy to accept it as something shipped with datashader... – James A. Bednar May 04 '17 at 02:22
  • @JamesA.Bednar Thanks! I'll probably be in touch in the PR thread then, because I'm not sure exactly what is missing from the given PR and I'd be glad to finish it if I am able. – not link May 05 '17 at 16:20

1 Answers1

6

Update [January 2021] Datashader 0.12 now includes native Matplotlib support as per comment from James A. Bednar below.

As of right now [May 2017] the best way to accomplish adding a datashader image to a matplotlib subplot is to use the pull request linked to above. It defines a DSArtist class. Assuming the DSArtist class exists, the code would be as follows:

N = 10000
df = pd.DataFrame(np.random.random((N, 3)), columns = ['x','y', 'z'])

f, ax = plt.subplots(2, 2)
ax_r = ax.ravel()

da = DSArtist(ax_r[0], df, 'x', 'y', ds.mean('z'), norm = mcolors.LogNorm())
ax_r[0].add_artist(da)

ax_r[1].hist(df['x'])
ax_r[2].hist(df['y'])
ax_r[3].plot(df['z'])

plt.tight_layout()
plt.show()
not link
  • 858
  • 1
  • 16
  • 29
  • @StevenC.Howell I think this is a minimal working example. Thanks again for pointing me in the right direction. – not link May 03 '17 at 21:07
  • 1
    Datashader 0.12 now includes native Matplotlib support; see https://datashader.org/getting_started/Interactivity.html#Native-support-for-Matplotlib . As of 1/2021 the plots there aren't rendered on the website, but they do work when run locally! – James A. Bednar Jan 16 '21 at 22:29