将数据着色器图像添加到 matplotlib 子图中

Add datashader image to matplotlib subplots

是否可以将数据着色器图像添加到一组 matplotlib 子图中?

举个具体的例子,

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

我有一个二乘二的 matplotlib 子图数组,我想用数据着色器图像 a 替换上面示例中的 [0,0] 图 ax_r[0]。这可能吗?如果可能,怎么做?

谢谢!

更新 [2021 年 1 月] Datashader 0.12 现在包含本机 Matplotlib 支持,根据下面 James A. Bednar 的评论。

截至目前 [2017 年 5 月] 将数据着色器图像添加到 matplotlib 子图的最佳方法是使用上面链接的 pull request。它定义了一个DSArtistclass。假设 DSArtist class 存在,代码如下:

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