Unable to visualise NetCDF DEM using basemap in jupyter - jupyter

I am trying to visualise the DEM tiles which are in a netCDF format using python, but the following code is resulting in just the country boundaries and grids and not any data plotted.
from netCDF4 import Dataset
import netCDF4
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap
nc = 'C:/Users/S05E135.SRTMGL1_NC.nc'
fh = Dataset(nc, mode='r')
fh.variables.keys()
lons = fh.variables['lon'][:]
lats = fh.variables['lat'][:]
height= fh.variables['SRTMGL1_DEM'][:]
height_units = fh.variables['SRTMGL1_DEM'].units
fh.close()
# Plotting the data
# Get some parameters for the Stereographic Projection
lon_0 = lons.mean()
lat_0 = lats.mean()
m = Basemap(width=4000000,height=3500000,
resolution='l',projection='stere',\
lat_ts=40,lat_0=lat_0,lon_0=lon_0)
# use meshgrid to create 2D arrays
lon, lat = np.meshgrid(lons, lats)
xi, yi = m(lon, lat)
# Plot Data
cs = m.pcolor(xi,yi,np.squeeze(height))
# Add Grid Lines
m.drawparallels(np.arange(-80., 81., 10.), labels=[1,0,0,0], fontsize=10)
m.drawmeridians(np.arange(-180., 181., 10.), labels=[0,0,0,1], fontsize=10)
# Add Coastlines, States, and Country Boundaries
m.drawcoastlines()
m.drawstates()
m.drawcountries()
# Add Colorbar
cbar = m.colorbar(cs, location='bottom', pad="10%")
cbar.set_label(height_units)
# Add Title
plt.title('dem')
plt.show()
Image of the output coming from the code

Related

Imported GeoTIFF file returns only NAN values

I have been trying to import a GEOTIFF file that shows irradiance data. I am able to plot (see Plot here) the file so I am sure the file has data; however; the raster array contains only nan values. Is there any way to resolve this? I am quite new to python so perhaps I've missed something obvious, any help would be appreciated :)
This is the code I'm using:
from osgeo import gdal
import numpy as np
import matplotlib.pyplot as plt
import gc
gc.enable()
#import
ds = gdal.Open('DNI_Spain.tif')
gt = ds.GetGeoTransform()
proj = ds.GetProjection()
band = ds.GetRasterBand(1)
array = band.ReadAsArray()
max_value = np.max(array)
min_value = np.min(array)
median_value = np.median(array)
plt.figure()
plt.imshow(array)

Plotting a boxplot without using 'toPandas' and in a one graph

I want to plot 'boxplot' of two different columns od a dataframe. I use the following code in pyspark python3:
c1 = df.where(F.col('c1') > 0.0).select(F.col('c1')).toPandas()
c2 = df.where(F.col('c1') > 0.0).select(F.col('c2')).toPandas()
fig, (ax1, ax2) = plt.subplots(nrows=2)
c1.boxplot(ax = ax1)
c2.boxplot(ax = ax2)
Is there any way to plot them
without using '.toPandas'?
in one picture like the following figure?

Image Color is not proper when converted to PIL

I am converting tensor to pil image, but color is distorted. I have tried converting it to BGR but result is still same.
trsn = transforms.ToPILImage()
temp = (target + torch.mean(target)) *torch.std(target)
res = trsn(temp.to('cpu').detach().squeeze())
np_img=np.array(res)
bgr = cv2.cvtColor(np_img,cv2.COLOR_RGB2BGR)
pil_im = Image.fromarray(bgr)
print(res)
# plt.imshow(res)
plt.imshow(res,label="Epoch "+str(i))
plt.show()
# im=Image.fromarray(np.uint8(target.to('cpu').detach().numpy()*255))
pil_im.save('f.jpeg')
I have a function that converts tensor to numpy and can show image on matplotlib.
Here is the function
def imcnvt(image):
x = image.to("cpu").clone().detach().numpy().squeeze()
x = x.transpose(1,2,0)
x = x*np.array((0.5,0.5,0.5)) + np.array((0.5,0.5,0.5))
return torch.Tensor(x)
Here is the pic I get
and here is the pic I want with PIL

Store integers from a text file into variable arrays

I need to plot some coordinate of x, y in MATLAB using some text file.
and I get a problem with reading it using a for loop.
I can figure it in Python but I need help in converting it in MATLAB.
This is some code in Python
file = open("6.txt", "r")
x = []
y = []
z = []
for i in file.readlines()[::]:
if i[0] == "(":
jam = i.strip('()').split(",")
x.append(float(jam[0]))
y.append(float(jam[1]))
jam = i.strip('()\n').split(",")
z.append(float(jam[2]))
'''
but in Matlab I initially start with this code
fileID = fopen('1.txt', 'r+');
formatSpec = '%s';
for i = fscanf(fileID, '%s')[::]
in Python result is
x = [1.154545 1.265648 ..... 1.56849]
Y = [1.0 1.5655 1.61662 ..... 1.0]
You can try something like:
content=readmatrix('1.txt') %read the entire content as a matrix
x=content(:1) %extract the first column
y=content(:2) %extract the second column
plot(x,y) %plot the data
I cannot test the above code right now, this is why I commented every line that I wrote. But the algorithm remains.

How to add a scatter plot over a map of London using python

I am trying to plot this scatter over a map of London, but it is not correct, here is my code, the maps have different sizes and I want one map but I got 2.
This is my dataframe (dfrt) to get the scatter:
commonName id lat lon placeType url additionalProperties2 category key sourceSystemKey value modified
0 River Street , Clerkenwell BikePoints_1 51.529163 -0.109970 BikePoint /Place/BikePoints_1 {'$type': 'Tfl.Api.Presentation.Entities.Addit... Description TerminalName BikePoints 001023 2019-08-22T11:17:04.327Z
1 Phillimore Gardens, Kensington BikePoints_2 51.499606 -0.197574 BikePoint /Place/BikePoints_2 {'$type': 'Tfl.Api.Presentation.Entities.Addit... Description TerminalName BikePoints 001018 2019-08-22T11:10:02.34Z
2 Christopher Street, Liverpool Street BikePoints_3 51.521283 -0.084605 BikePoint /Place/BikePoints_3 {'$type': 'Tfl.Api.Presentation.Entities.Addit... Description TerminalName BikePoints 001012 2019-08-22T11:12:02.7Z
3 St. Chad's Street, King's Cross BikePoints_4 51.530059 -0.120973 BikePoint /Place/BikePoints_4 {'$type': 'Tfl.Api.Presentation.Entities.Addit... Description TerminalName BikePoints 001013 2019-08-22T11:08:02.047Z
4 Sedding Street, Sloane Square BikePoints_5 51.493130 -0.156876 BikePoint /Place/BikePoints_5 {'$type': 'Tfl.Api.Presentation.Entities.Addit... Description TerminalName
And this what I tried to get the plots:
fig = plt.figure(figsize=(20, 10))
m = Basemap(projection='lcc', resolution='h',
lat_0=51.53, lon_0=0.08,
width=1E6, height=1.2E6)
m.shadedrelief()
m.drawcoastlines(color='gray')
m.drawcountries(color='gray')
a4_dims = (20, 10)
fig, ax = plt.subplots(figsize = a4_dims)
ax.scatter(dfrt['lat'], dfrt['lon'])
ax.set_title('Latitud y Longitud de la ubicaciĆ³n de BikePoints')
ax.set_xlabel('Latitud')
ax.set_ylabel('Longitud')
Could you please, help me?
You create figure/axes twice and end up plotting 2 figures. Here is the relevant code based on yours:
# create figure and axes
fig = plt.figure(figsize=(20, 10))
ax1 = plt.gca()
m = Basemap(projection='lcc', resolution='h', \
lat_0=51.53, lon_0=0.08,
width=1E6, height=1.2E6, ax=ax1)
m.shadedrelief()
m.drawcoastlines(color='gray')
m.drawcountries(color='gray')
# a4_dims = (20, 10)
# fig, ax = plt.subplots(figsize = a4_dims)
# uncomment to do scatter plot
#ax1.scatter(dfrt['lat'], dfrt['lon'])
ax1.set_title('Latitud y Longitud de la ubicaciĆ³n de BikePoints')
ax1.set_xlabel('Latitud')
ax1.set_ylabel('Longitud')
plt.show()
and the (unfinish) plot:
Edit 1
For the data plot on the map, here is the code that use simple data to plot:
# sample data
lons = np.array([-5.371475, -1.569707, -0.892185])
lats = np.array([51.211262, 52.819886, 55.122479])
x,y = m(lons, lats)
# any of these code will plot dots on the map
#ax1.plot(*m(lons, lats), 'ro') # OK
#ax1.plot(x, y, 'ro') # OK
ax1.scatter(x, y, 25) # OK
For data from pandas' dataframe, try to get lons and lats as follows:
lons = list(dfrt['lon'])
lats = list(dfrt['lat'])
then do as shown above.