我希望使用python中的Datashader模块执行类似于pyplot.scatter的操作,独立地为每个点指定一个个体(x,y),RGB\十六x值:
#what i'd like to do, but using Datashader:
import numpy as np
#make sample arrays
n = int(1e+8)
point_array = np.random.normal(0, 1, [n, 2])
color_array = np.random.randint(0, 256, [n, 3])/255 # RGB. I can
#convert between it and hex if needed
#the part I need - make an image similar to plt.scatter, using datashader instead:
import matplotlib.pyplot as plt
fig = plt.figure()
plot = fig.add_subplot(111)
fig.canvas.draw()
plot.scatter(point_array[:, 0], point_array[:, 1], c=color_array)
img = np.fromstring(fig.canvas.tostring_rgb(), dtype=np.uint8, sep='')
img = img.reshape(fig.canvas.get_width_height()[::-1] + (3,))因此,img是一个RGB numpy数组(或者PIL数组,或者任何可以通过python保存为图像的东西)。
Things我试过
我看过datashader.Canvas.points以及它是如何处理三维熊猫数组的,我认为我可以使用它的color_key,只有红色、绿色和蓝色的值,在标签之间进行“线性插值”,但我并没有成功地让它真正发挥作用(我总是把熊猫方面的东西粘在一起,因为我通常只对所有的东西都使用numpy )。
发布于 2018-06-21 20:43:44
我认为上面的代码可以简化为:
import numpy as np, pandas as pd, matplotlib.pyplot as plt
%matplotlib inline
np.random.seed(0)
n = int(1e+4)
p = np.random.normal(0, 1, [n, 2])
c = np.random.randint(0, 256, [n, 3])/255.0
plt.scatter(p[:,0], p[:,1], c=c);

如果datashader提供了一种使用RGB值的方便方法(尽管可以打开一个请求该值的问题!),那就太好了,但是现在您可以计算每个点的平均R、G、B值:
import datashader as ds, datashader.transfer_functions as tf
df = pd.DataFrame.from_dict(dict(x=p[:,0], y=p[:,1], r=c[:,0], g=c[:,1], b=c[:,2]))
cvs = ds.Canvas(plot_width=70, plot_height=40)
a = cvs.points(df,'x','y', ds.summary(r=ds.mean('r'),g=ds.mean('g'),b=ds.mean('b')))结果将是一个包含r、g、b通道的Xarray数据集,每个通道的标度为0到1.0。然后,您可以根据自己的喜好将这些通道组合成图像,例如使用HoloViews:
import holoviews as hv
hv.extension('bokeh')
hv.RGB(np.dstack([a.r.values, a.g.values, a.b.values])).options(width=450, invert_yaxis=True)

请注意,Datashader目前只支持无限小的点,而不像Matplotlib示例那样支持磁盘/填充的圆圈,这就是为什么我使用如此小的分辨率(使点可见以供比较)。扩展Datashader以呈现一个非零范围的形状是有用的,但它不在当前的路线图上。
https://stackoverflow.com/questions/50935037
复制相似问题