0

I collect data about things like water temperature, air temperature, humidity etc. in a csv-file, and I want to plot that data on a map. However, I run into issues when trying to plot it, since latitudes, longitudes and data lists are one-dimentional. Therefore I cannot use pcolormesh over the map. I've tried googling how to do this, but I haven't been able to find a way that works for me.

The problem is not with basemap, but with finding a way to plot data where, e.g. a low temperature results in a blue color at the recorded coordinates, while a high temperature results in a red color so I get a map similar to this. Temperature plot of USA

Ideally I'd want a function that looks like plot(latitudes, longitudes, data) where all three variables are just lists. I don't think this exists though.

Data in the csv-file is represented like this:

Date,Time,Latitude,Longitude,WaterTemp,AirTemp,Humidity
211222,14042300,60.438760,5.310859,5.2,2.4,0.21

So how would I convert the data to be able to do this?

I've tried to use pcolormesh but reshaped the data lists, but I haven't gotten that to work. The meshgrid function doesn't fix the problem either. Every way I've tried to plot the data has resulted in different errors, like "shapes don't match" or "data must be two-dimentional". I can use map.scatter(lats, lons, latlon=True). However that doesn't let me set each scatter point with a value, just the coordinates.

1 Answer 1

0

You can try interpolation to create a regular grid based on your data.

import scipy as sp
import matplotlib.pyplot as plt

lat = np.random.randint(-40, 40, 1000)
lon = np.random.randint(-40, 40, 1000)
temp = np.random.randint(0, 32, 1000)

spline_2d = sp.interpolate.SmoothBivariateSpline(lat, lon, temp)

lat_min = np.min(lat)
lat_max = np.max(lat)

lon_min = np.min(lon)
lon_max = np.max(lon)

grid_points = 100

lat_grid = np.arange(lat_min, lat_max, (lat_max - lat_min)/grid_points)
lon_grid = np.arange(lon_min, lon_max, (lon_max - lon_min)/grid_points)

lat_grid, lon_grid = np.meshgrid(lat_grid, lon_grid, indexing = 'ij')

interp_points = spline_2d.ev(lat_grid.ravel(), lon_grid.ravel()).reshape((grid_points, grid_points))

plt.figure()
plt.pcolormesh(lat_grid, lon_grid, interp_points)
Sign up to request clarification or add additional context in comments.

9 Comments

Hmm apparently scipy doesn't have attribute 'interpolate'. Traceback (most recent call last): File "c:\Users\johan\Documents\Programming\Python\Exercises\Python\matplotlib exercises\plot.py", line 9, in <module> spline_2d = sp.interpolate.SmoothBivariateSpline(lat, lon, temp) AttributeError: module 'scipy' has no attribute 'interpolate'
Try from scipy.interpolate import SmoothBivariateSpline and then drop the sp.interpolate. from the spline_2d line.
I get the error/warning C:\Users\johan\AppData\Roaming\Python\Python310\site-packages\scipy\interpolate\_fitpack2.py:1176: UserWarning: The required storage space exceeds the available storage space: nxest or nyest too small, or s too small. The weighted least-squares spline corresponds to the current set of knots. warnings.warn(message) What does this error mean?
Unsure, but you can try adjusting some of the parameters in the BivariateSpline and see if that fixes the warning. Alternatively, it shouldn't prevent you from performing the interpolation. If the output plot seems reasonable, you can consider just ignoring the warning.
It seemingly work. But If I for example say lat = [i for i in range(100)] lon = [i for i in range(100)] temp = [5 for i in range(100)] it doesn't produce the same color for each pixel even though the temp is the same, which is what I want.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.