Drawing shaded area depending on distance with latitude and altitude coordinate


I

Isaac Won

I have tried to make a plot of points with longitude and latitude coordinate, and draw shaded area with distance from one point. So, I thought that I could uae contourf function from matplotlibrary. My code is:
import haversine
import numpy as np
import matplotlib.pyplot as plt
with open(filin, 'r') as f:
arrays = [map(float, line.split()) for line in f]
newa = [[x[1],-x[2]] for x in arrays]

lat = np.zeros(275)
lon = np.zeros(275)
for c in range(0,275):
lat[c] = newa[c][0]
lon[c] = newa[c][1]

with open(filin, 'r') as f:
arrays = [map(float, line.split()) for line in f]
newa = [[x[1],-x[2]] for x in arrays]

lat = np.zeros(275)
lon = np.zeros(275)
for c in range(0,275):
lat[c] = newa[c][0]
lon[c] = newa[c][1]


dis = np.zeros(275)

for c in range(0,275):
dis[c] = haversine.distance(newa[0],[lat[c],lon[c]])

dis1 = [[]]*1

for c in range(0,275):
dis1[0].append(dis[c])


cs = plt.contourf(lon,lat,dis1)
cb = plt.colorbar(cs)

plt.plot(-lon[0],lat[0],'ro')
plt.plot(-lon[275],lat[275],'ko')
plt.plot(-lon[1:275],lat[1:275],'bo')
plt.xlabel('Longitude(West)')
plt.ylabel('Latitude(North)')
plt.gca().invert_xaxis()
plt.show()

My idea in this code was that I could made a shaded contour by distance from a certain point which was noted as newa[0] in the code. I calculated distances between newa[0] and other points by haversine module which calculate distances with longitudes and latitudes of two points. However, whenever I ran this code, I got the error related to X, Y or Z in contourf such as:
TypeError: Length of x must be number of columns in z, and length of y must be number of rows.

IF I use meshgrid for X and Y, I also get:
TypeError: Inputs x and y must be 1D or 2D.

I just need to draw shaded contour with distance from one point on the top of the plot of each point.

If you give any idea or hint, I will really apprecite. Thank you, Isaac
 
Ad

Advertisements

M

Mark Lawrence

I have tried to make a plot of points with longitude and latitude coordinate, and draw shaded area with distance from one point. So, I thought that I could uae contourf function from matplotlibrary. My code is:
import haversine
import numpy as np
import matplotlib.pyplot as plt
with open(filin, 'r') as f:
arrays = [map(float, line.split()) for line in f]
newa = [[x[1],-x[2]] for x in arrays]

lat = np.zeros(275)
lon = np.zeros(275)
for c in range(0,275):
lat[c] = newa[c][0]
lon[c] = newa[c][1]

with open(filin, 'r') as f:
arrays = [map(float, line.split()) for line in f]
newa = [[x[1],-x[2]] for x in arrays]

lat = np.zeros(275)
lon = np.zeros(275)
for c in range(0,275):
lat[c] = newa[c][0]
lon[c] = newa[c][1]


dis = np.zeros(275)

for c in range(0,275):
dis[c] = haversine.distance(newa[0],[lat[c],lon[c]])

dis1 = [[]]*1

for c in range(0,275):
dis1[0].append(dis[c])


cs = plt.contourf(lon,lat,dis1)
cb = plt.colorbar(cs)

plt.plot(-lon[0],lat[0],'ro')
plt.plot(-lon[275],lat[275],'ko')
plt.plot(-lon[1:275],lat[1:275],'bo')
plt.xlabel('Longitude(West)')
plt.ylabel('Latitude(North)')
plt.gca().invert_xaxis()
plt.show()

My idea in this code was that I could made a shaded contour by distance from a certain point which was noted as newa[0] in the code. I calculated distances between newa[0] and other points by haversine module which calculate distances with longitudes and latitudes of two points. However, whenever I ran this code, I got the error related to X, Y or Z in contourf such as:
TypeError: Length of x must be number of columns in z, and length of y must be number of rows.

IF I use meshgrid for X and Y, I also get:
TypeError: Inputs x and y must be 1D or 2D.

I just need to draw shaded contour with distance from one point on the top of the plot of each point.

If you give any idea or hint, I will really apprecite. Thank you, Isaac

Sorry I can't help directly but can point you here
https://lists.sourceforge.net/lists/listinfo/matplotlib-users or perhaps
stackoverflow.
 
Ad

Advertisements

D

Dave Angel

Re: Drawing shaded area depending on distance with latitude and altitude
coordinate


dis1 = [[]]*1

for c in range(0,275):
dis1[0].append(dis[c])

So dis1 has 1 row in it. But contourf is expecting many rows,
matching the length of lat. I'm guessing you have to fill in the
others.

cs = plt.contourf(lon,lat,dis1)

TypeError: Length of x must be number of columns in z, and
length of y must be number of rows.

That's only a tiny part of the error message. Please post the whole
traceback. I made a guess here knowing nothing about these libraries
you're using.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top