是否可以与matplotlib进行添加剂混合? [英] Is it possible to do additive blending with matplotlib?

查看:123
本文介绍了是否可以与matplotlib进行添加剂混合?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在处理重叠的高密度散点图或不同颜色的线图时,可以方便地实现加法混合方案,其中每个标记的RGB颜色加在一起以在画布中产生最终颜色.这是2D和3D渲染引擎中的常见操作.

When dealing with overlapping high density scatter or line plots of different colors it can be convenient to implement additive blending schemes, where the RGB colors of each marker add together to produce the final color in the canvas. This is a common operation in 2D and 3D render engines.

但是,在Matplotlib中,我仅发现了对alpha/不透明度混合的支持.有什么回旋的方法吗?还是我坚持渲染到位图,然后将它们混合到某些绘画程序中?

However, in Matplotlib I've only found support for alpha/opacity blending. Is there any roundabout way of doing it or am I stuck with rendering to bitmap and then blending them in some paint program?

这是一些示例代码和手动解决方案.

这将产生两个部分重叠的随机分布:

This will produce two partially overlapping random distributions:

x1 = randn(1000)
y1 = randn(1000)
x2 = randn(1000) * 5
y2 = randn(1000)
scatter(x1,y1,c='b',edgecolors='none')
scatter(x2,y2,c='r',edgecolors='none')

这将在matplotlib中产生以下内容:

This will produce in matplotlib the following:

如您所见,有一些重叠的蓝点被红点遮住,我们希望看到它们.通过在matplotlib中使用Alpha/不透明度混合,您可以执行以下操作:

As you can see, there are some overlapping blue points that are occluded by red points and we would like to see them. By using alpha/opacity blending in matplotlib, you can do:

scatter(x1,y1,c='b',edgecolors='none',alpha=0.5)
scatter(x2,y2,c='r',edgecolors='none',alpha=0.5)

将产生以下内容:

但是我真正想要的是以下内容:

But what I really want is the following:

我可以通过将每个图独立渲染为位图来手动完成此操作:

I can do it manually by rendering each plot independently to a bitmap:

xlim = plt.xlim()
ylim = plt.ylim()
scatter(x1,y1,c='b',edgecolors='none')
plt.xlim(xlim)
plt.ylim(ylim)
scatter(x2,y2,c='r',edgecolors='none')
plt.xlim(xlim)
plt.ylim(ylim)
plt.savefig(r'scatter_blue.png',transparent=True)
plt.savefig(r'scatter_red.png',transparent=True)

哪个给了我以下图片:

然后您可以做的是将它们作为独立的图层加载到Paint.NET/PhotoShop/gimp中,然后进行添加剂混合.

What you can do then is load them as independent layers in Paint.NET/PhotoShop/gimp and just additive blend them.

现在理想的情况是能够在Matplotlib中以编程方式执行此操作,因为我将处理数百个这样的程序!

Now ideal would be to be able to do this programmatically in Matplotlib, since I'll be processing hundreds of these!

推荐答案

如果仅需要图像作为结果,则可以将画布缓冲区作为numpy数组获取,然后进行混合,下面是一个示例:

If you only need an image as the result, you can get the canvas buffer as a numpy array, and then do the blending, here is an example:

from matplotlib import pyplot as plt
import numpy as np

fig, ax = plt.subplots()
ax.scatter(x1,y1,c='b',edgecolors='none')
ax.set_xlim(-4, 4)
ax.set_ylim(-4, 4)
ax.patch.set_facecolor("none")
ax.patch.set_edgecolor("none")
fig.canvas.draw()

w, h = fig.canvas.get_width_height()
img = np.frombuffer(fig.canvas.buffer_rgba(), np.uint8).reshape(h, w, -1).copy()

ax.clear()
ax.scatter(x2,y2,c='r',edgecolors='none')
ax.set_xlim(-4, 4)
ax.set_ylim(-4, 4)
ax.patch.set_facecolor("none")
ax.patch.set_edgecolor("none")
fig.canvas.draw()

img2 = np.frombuffer(fig.canvas.buffer_rgba(), np.uint8).reshape(h, w, -1).copy()

img[img[:, :, -1] == 0] = 0
img2[img2[:, :, -1] == 0] = 0

fig.clf()

plt.imshow(np.maximum(img, img2))
plt.subplots_adjust(0, 0, 1, 1)
plt.axis("off")
plt.show()

结果:

这篇关于是否可以与matplotlib进行添加剂混合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆