1

I have a .csv file which contains some data where x, y, x1, y1 are the coordinate points, and p is the value. My below code is working very well for plotting, but when I am plotting the data, I am getting a background color like the purple color. I don't want any color in the background. I want the background will be Transparent. My ultimate goal is overlying this result over an image. I am new in Python. Any help will be highly appreciated.

Download link of the .csv file here or link-2 or link-3

I am getting below result result

My Code

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()

As per Ghassen's suggestion I am getting below results. I am still not getting the transparent background.

When Alpha =0

alpha=0

When alpha =0.5

alpha =0.5

When alpha =1

alpha=1

Js541
  • 100
  • 1
  • 13

2 Answers2

1

try with this code :

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()

output: enter image description here

Ghassen
  • 764
  • 6
  • 14
  • Yes, I tried in this way too. When alpha = 0, I am getting nothing. I changed the value of alpha to 0.5 or up to 1. When I am overlaying that image I am not getting background of the image. – Js541 Jun 23 '19 at 00:13
  • When you set alpha to 0 the background desapeare totaly with the curve of data? I can't download the csv file but i test alpha with 0 there is no bckground and with one it's the same as your image – Ghassen Jun 23 '19 at 00:18
  • I have updated the results when alpha=0, 0.5 and 1. Please check my edited question. I also updated .csv file download link. Thanks for your help. – Js541 Jun 23 '19 at 00:47
  • i think we should find how to make background transparent in this stage map = ndimage.filters.gaussian_filter(array, sigma=16) in the application of gaussian filter – Ghassen Jun 23 '19 at 09:06
  • @Js541 try with this link : https://stackoverflow.com/questions/2578752/how-can-i-plot-nan-values-as-a-special-color-with-imshow-in-matplotlib – Ghassen Jun 23 '19 at 12:17
0

I solved this issue by masking out the values where values ==0. The code will be

from mpl_toolkits.axes_grid1 import make_axes_locatable

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

Js541
  • 100
  • 1
  • 13