如何使背景透明?

时间:2019-06-22 22:01:59

标签: python-3.x numpy-ndarray alpha-transparency

我有一个.csv文件,其中包含一些数据,其中x,y,x1,y1是坐标点,p是值。我下面的代码在绘制时效果很好,但是在绘制数据时,我得到的背景色是紫色。我不要背景颜色。我希望背景为透明。我的最终目标是在图像上覆盖此结果。我是Python新手。任何帮助将不胜感激。

.csv文件herelink-2link-3的下载链接

我的结果低于result

我的代码

import matplotlib.pyplot as plt
from scipy import ndimage
import numpy as np
import pandas as pd
from skimage import transform
from PIL import Image
import cv2

x_dim=1200
y_dim=1200

# Read CSV 
df = pd.read_csv("flower_feature.csv")

# Create numpy array of zeros os same size
array = np.zeros((x_dim, y_dim), dtype=np.uint8)

for index, row in df.iterrows():
    x = np.int(row["x"])
    y = np.int(row["y"])
    x1 = np.int(row["x1"])
    y1 = np.int(row["y1"])
    p = row["p"]
    array[x:x1,y:y1] = p

map = ndimage.filters.gaussian_filter(array, sigma=16)
plt.imshow(map)
plt.show()

根据Ghassen的建议,我得到的结果低于预期。我仍然没有透明的背景。

当Alpha = 0时

alpha=0

当alpha = 0.5

alpha =0.5

当alpha = 1时

alpha=1

2 个答案:

答案 0 :(得分:0)

尝试使用此代码:

import matplotlib.pyplot as plt
from scipy import ndimage
import numpy as np
import pandas as pd


x_dim=1200
y_dim=1200

# Read CSV 
df = pd.read_csv("/home/rosafi/Downloads/flower_feature.csv")

# Create numpy array of zeros os same size
array = np.ones((x_dim, y_dim), dtype=np.uint8)

for index, row in df.iterrows():
    x = np.int(row["x"])
    y = np.int(row["y"])
    x1 = np.int(row["x1"])
    y1 = np.int(row["y1"])
    p = row["p"]
    array[x:x1,y:y1] = p

map = ndimage.filters.gaussian_filter(array, sigma=16)
map = np.ma.masked_where(map == 0, map)
plt.imshow(map)
plt.show()

输出: enter image description here

答案 1 :(得分:0)

我通过屏蔽值== 0的值解决了这个问题。该代码将是

from mpl_toolkits.axes_grid1 import make_axes_locatable

masked_data = np.ma.masked_where(map == 0, map)