I have a csv file including lan, lon, z values. I've been trying to make contourf based on bellow code :
import pandas as pd import numpy as np import matplotlib.pyplot as plt from scipy.interpolate import griddata import geojsoncontour import utm import matplotlib.cm as cm from scipy.spatial import ConvexHull import alphashape from descartes import PolygonPatch from mask import alpha_shape import math import matplotlib.ticker as ticker data_url = 'http://spatialcloud.ir/files/data_1.csv' df = pd.read_csv(data_url) df.head() x,y = utm.to_latlon(df.lat, df.lon, 39, 'N') z = df.z plt.scatter(y,x) def plot_contour(x,y,z,resolution = 50,contour_method='linear'): resolution = str(resolution)+'j' X,Y = np.mgrid[min(x):max(x):complex(resolution), min(y):max(y):complex(resolution)] points = [[a,b] for a,b in zip(x,y)] Z = griddata(points, z, (X, Y), method=contour_method) return X,Y,Z X,Y,Z = plot_contour(y,x,z,resolution = 50,contour_method='linear') fig, ax = plt.subplots(figsize=(13,8)) cpf = ax.contourf(X,Y,Z,cmap="rainbow") fig.colorbar(cpf) plt.show()
The result is like the below image : But I need to make contourf based on my points boundary like this : what I want from contourf I mean I need to make contourf just inside the black polygon which is my points boundray and all colors outside of black polygon get removed. What should I do???