Is it possible to add a datashader image to a set of matplotlib subplots?
As a concrete example,
JavaScript
x
28
28
1
import numpy as np
2
import pandas as pd
3
import matplotlib.pylab as plt
4
5
import datashader as ds
6
import datashader.transfer_functions as tf
7
from datashader.utils import export_image
8
9
from functools import partial
10
11
background = "white"
12
export = partial(export_image, background = background, export_path="export")
13
14
N = 10000
15
df = pd.DataFrame(np.random.random((N, 3)), columns = ['x','y', 'z'])
16
17
f, ax = plt.subplots(2, 2)
18
ax_r = ax.ravel()
19
20
ax_r[0].scatter(df['x'], df['y'], df['z'].mean())
21
ax_r[1].hist(df['x'])
22
ax_r[2].hist(df['y'])
23
ax_r[3].plot(df['z'])
24
25
cvs = ds.Canvas(plot_width=100, plot_height=100)
26
agg = cvs.points(df, 'x', 'y', ds.mean('z'))
27
a = export(tf.shade(agg, cmap=['lightblue', 'darkblue'], how='eq_hist'), 'test')
28
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!
Advertisement
Answer
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:
JavaScript
1
16
16
1
N = 10000
2
df = pd.DataFrame(np.random.random((N, 3)), columns = ['x','y', 'z'])
3
4
f, ax = plt.subplots(2, 2)
5
ax_r = ax.ravel()
6
7
da = DSArtist(ax_r[0], df, 'x', 'y', ds.mean('z'), norm = mcolors.LogNorm())
8
ax_r[0].add_artist(da)
9
10
ax_r[1].hist(df['x'])
11
ax_r[2].hist(df['y'])
12
ax_r[3].plot(df['z'])
13
14
plt.tight_layout()
15
plt.show()
16