将数据着色器图像添加到 matplotlib 子图 [英] Add datashader image to matplotlib subplots
问题描述
是否可以将数据着色器图像添加到一组matplotlib子图中?
Is it possible to add a datashader image to a set of matplotlib subplots?
作为一个具体示例,
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子图组成的2 x 2数组,想用数据着色器图像 a 代码>.这可能吗?如果可以,怎么办?
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?
谢谢!
推荐答案
更新 [2021 年 1 月] 根据下面 James A. Bednar 的评论,Datashader 0.12 现在包括原生 Matplotlib 支持.
Update [January 2021] Datashader 0.12 now includes native Matplotlib support as per comment from James A. Bednar below.
截至[ 2017年5月",完成向matplotlib子图中添加数据着色器图像的最佳方法是使用
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()
这篇关于将数据着色器图像添加到 matplotlib 子图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!