在Matplotlib中基于像素值设置透明度 [英] Setting Transparency Based on Pixel Values in Matplotlib

查看:392
本文介绍了在Matplotlib中基于像素值设置透明度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用matplotlib为正在研究的论文绘制一些图形.我有2D numpy数组中的两组数据:一个ascii hillshade栅格,我可以使用以下方式高兴地绘制和调整:

I am attempting to use matplotlib to plot some figures for a paper I am working on. I have two sets of data in 2D numpy arrays: An ascii hillshade raster which I can happily plot and tweak using:

import matplotlib.pyplot as pp
import numpy as np

hillshade = np.genfromtxt('hs.asc', delimiter=' ', skip_header=6)[:,:-1]

pp.imshow(hillshade, vmin=0, vmax=255)
pp.gray()
pp.show()

哪个给:

还有第二个ascii栅格,该栅格描绘了流经景观的河流的属性.可以按照与上述相同的方式来绘制此数据,但是将不对应于河流网络的数组中的值分配为-9999的无数据值.目的是将无数据值设置为透明,以便河流值覆盖山体阴影.

And a second ascii raster which delineates properties of a river flowing across the landscape. This data can be plotted in the same manner as above, however values in the array which do not correspond to the river network are assigned a no data value of -9999. The aim is to have the no data values set to be transparent so the river values overlie the hillshade.

这是河流数据,理想情况下,此处表示为0的每个像素都是完全透明的.

This is the river data, ideally every pixel represented here as 0 would be completely transparent.

对此进行了一些研究,似乎我可以将我的数据转换为RGBA数组并设置alpha值,以仅使不需要的单元格透明.但是,river数组中的值是浮点数,不能进行转换(因为原始值是图形的整个点),我相信imshow函数只能使用RGBA格式来接受无符号整数.

Having done some research on this it seems I may be able to convert my data into an RGBA array and set the alpha values to only make the unwanted cells transparent. However, the values in the river array are floats and cannot be transformed (as the original values are the whole point of the figure) and I believe the imshow function can only take unsigned integers if using the RGBA format.

有什么办法可以解决这个限制?我曾希望我可以简单地用像素值和alpha值创建一个元组,然后像这样绘制它们,但这似乎是不可能的.

Is there any way around this limitation? I had hoped I could simply create a tuple with the pixel value and the alpha value and plot them like that, but this does not seem possible.

我也尝试过使用PIL尝试创建无数据值透明的河流数据的PNG文件,但是这似乎会自动将像素值缩放为0-255,从而丢失了值I需要保存.

I have also had a play with PIL to attempt to create a PNG file of the river data with the no data value transparent, however this seems to automatically scale the pixel values to 0-255, thereby losing the values I need to preserve.

我欢迎任何人对这个问题有任何见识.

I would welcome any insight anyone has on this problem.

推荐答案

只需屏蔽您的河流"数组.

例如

rivers = np.ma.masked_where(rivers == 0, rivers)

以这种方式叠加两个图的快速示例:

As a quick example of overlaying two plots in this manner:

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.cm as cm

# Generate some data...
gray_data = np.arange(10000).reshape(100, 100)

masked_data = np.random.random((100,100))
masked_data = np.ma.masked_where(masked_data < 0.9, masked_data)

# Overlay the two images
fig, ax = plt.subplots()
ax.imshow(gray_data, cmap=cm.gray)
ax.imshow(masked_data, cmap=cm.jet, interpolation='none')
plt.show()

此外,请注意,imshow将很高兴接受其RGBA格式的浮点数.它只是希望一切都在0到1之间.

Also, on a side note, imshow will happily accept floats for its RGBA format. It just expects everything to be in a range between 0 and 1.

这篇关于在Matplotlib中基于像素值设置透明度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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