Add datashader image to matplotlib subplots

Is it possible to add a datashader image to a matplotlib subplot set?

As a specific 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 two by two matplotlib subframe arrays and would like to replace the plot [0,0] ax_r[0]

in the above example with a datashader image a

. Is this possible, and if so, how?

Thank!

+3


source to share


1 answer


As of now [May 2017], the best way to accomplish adding a datashader image to a matplotlib subplot is to use the pull request linked above. It defines the class DSArtist

. Assuming the class exists DSArtist

, the code would look like this:



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

      

+4


source







All Articles