mardi 4 août 2015

Plot a distance map by apllyind the distance tranform to a list of vlues x and y, in PYTHON

I want to plot a distance map by apllying the distance tranform to a list of vlues x and y, in PYTHON.

I have started to plot the data from a CSV file:

"""PLOTTING DATA FROM A CSV FILE""""" #csv: comma separeted variables file
from matplotlib import pyplot as plt
from matplotlib import style
from pandas import DataFrame

df = DataFrame.from_csv('d:\Users\Raquel\Desktop/test.csv', header=0,sep=';')#unpact values into x and y using pandas to load my csv file

style.use('ggplot')

plt.scatter(df['x'].values,df['y'].values)
plt.title ('Data plot of a Tile')
plt.xlabel('xobj')
plt.ylabel('yobj')

plt.show()

Then I took the coordinates x and y from the file and I applied the distance transform:

 """DISTANCE MAP"""
#distance map: morphology distance transform gives the closeste distance of each pixel to its nearest boundary pixel
   from scipy.ndimage.morphology import distance_transform_edt

# Coordinates
x = df['x'].values
y = df['y'].values

# Distance Transform
dis = distance_transform_edt(x, y) #distance transform applied to x,y
plt.subplot(2,2,1),plt.imshow(dis, origin='lower')
plt.title('Distance Map')

from scipy.ndimage.filters import gaussian_filter #convolution with the gaussian
gf = gaussian_filter(dis, sigma= 2.5)#gaussian filter applied to dis
plt.subplot(2,2,2),plt.imshow(gf, origin='lower')
plt.title('Distance Map with GF')

After that I applied the contour to the Distance Map with the gaussian filter:

"""CONTOUR"""
   import matplotlib.pyplot as plt
   import numpy as np

plt.subplot(2,2,3),plt.contour(gf)
plt.title('Contour')

plt.show()

To finalise I plot the points in the back of the plotting of the contour immage:

"""CONTOUR & HOT SPOTS"""

from matplotlib.pyplot import (colorbar)
plt.subplot(2,2,4),plt.contour(gf) #plotting the  HS in the back of the           image with a transparency = alpha
plt.imshow(dis, origin='lower', alpha=0.19)
plt.title ('Contour & Hot Spots')

plt.show()

When I run my script I have this message and I don't know how to make my script work...

   Traceback (most recent call last):
   File "D:/Users/Raquel/PycharmProjects/BASES/Density Map/v.density map of a tile1.py", line 20, in <module>
   dis = distance_transform_edt(x, y) #distance transform applied to x,y
   File "D:\Users\Raquel\Anaconda\lib\site-packages\scipy\ndimage\morphology.py", line 2111, in distance_transform_edt
sampling = _ni_support._normalize_sequence(sampling, input.ndim)
   File "D:\Users\Raquel\Anaconda\lib\site-packages\scipy\ndimage\_ni_support.py", line 66, in _normalize_sequence
   raise RuntimeError(err)
   RuntimeError: sequence argument must have length equal to input rank

Aucun commentaire:

Enregistrer un commentaire