Hi I have a dataset consisting of long, lat and value. This dataset fat for each coordinate of the world a separate row and is therefore a Gridded Data dataset. Now I want to import this from a CSV file into my project and plot it in Basemap (and display it as a Contourf). I have managed the import, but the display of the data as a Contourf still poses problems. For example, I get the error message:
ValueError: zero-size array to reduction operation maximum which has no identity
my code:
from traceback import print_tb
import numpy as np
from pykrige.ok import OrdinaryKriging
from pykrige.kriging_tools import write_asc_grid
import pykrige.kriging_tools as kt
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap
from matplotlib.patches import Path, PathPatch
import pandas as pd
from numpy import linspace
def load_data():
df = pd.read_csv(r"File", usecols=['Value'])
return(df)
def get_data(df):
return {
"lons": df['Longitude'],
"lats": df['Latitude'],
"values": df['Value']
}
def generate_grid(df, basemap, delta=1):
grid["lon"] = linspace(0, basemap.urcrnrx, df.shape[1])
grid["lat"] = linspace(0, basemap.urcrnry, df.shape[1])
grid = {
'lon': np.arange(-180, 180, delta),
'lat': np.arange(-89.9,89.9, delta) # dont extrapolate towards the poles
}
grid["x"], grid["y"] = np.meshgrid(grid["lon"], grid["lat"])
grid["x"], grid["y"] = basemap(grid["x"], grid["y"])
return grid
def prepare_map_plot():
figure, axes = plt.subplots(figsize=(10,10))
basemap = Basemap(projection='robin', lon_0=0, lat_0=0, resolution='l',area_thresh=1000000,ax=axes)
basemap.drawcoastlines()
basemap.drawparallels(np.arange(-90.,120.,30.))
basemap.drawmeridians(np.arange(0.,420.,60.))
return figure, axes, basemap
def plot_mesh_data(df, grid, basemap):
colormesh = basemap.contourf(grid["x"], grid["y"],df, 32, cmap='RdBu_r', ) #plot the data on the map. plt.cm.RdYlBu_r
color_bar = basemap.colorbar(colormesh,location='bottom',pad="10%")
df = load_data()
# print(df['Latitude'].shape)
figure, axes, basemap = prepare_map_plot()
grid = generate_grid(df, basemap, 90)
plot_mesh_data(df, grid,basemap)
# plt.show()
But more importantly: how do I manage to display the data, do I have to interpolate it again?