0

I am using this program to project my ammonia concentration over India region, but when I run this code:

from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt
import numpy as np

def read_datafile(file_name):
data = np.genfromtxt(file_name, delimiter=',', names=['lat', 'lon', 'nh'])
return data

data = read_datafile('E:/algo/data/combining/FINAL_DATA.csv')

map = Basemap(llcrnrlon=66,llcrnrlat=6,urcrnrlon=98.5,urcrnrlat=38, resolution='i', projection='cyl')
map.drawcoastlines()
map.drawlsmask()
map.drawcountries()
parallels = np.arange(6,38,5)
meridians = np.arange(66,99,5)
map.drawparallels(parallels,labels=[1,0,0,0],fontsize=10)
map.drawmeridians(meridians,labels=[0,0,0,1],fontsize=10)

lons,lats= np.meshgrid(data['lon'][1:],data['lat'][1:],sparse = 'True')
x,y = map(lons,lats)
map.pcolormesh(x,y,data['nh'][1:])
plt.show()

It shows me the error:

ValueError: not enough values to unpack (expected 2, got 1)

in the map.pcolor(x,y,z) And I used data['lon'][1:] because the 1st value was nan maybe due to header. So, any way to remove that error, please!!! The csv file contains:

,lat,lon,Unnamed: 0,nh
0,7.75,71.75,0.5,8501464385113510.0
1,7.75,74.25,2.5,8019719264331602.0
2,7.75,75.25,4.5,7708972186355438.0
3,7.75,78.75,6.5,1.5269509187007006e+16
4,7.75,87.75,8.5,1.5235106744690036e+16
5,7.75,88.25,10.0,8944079502837338.0
6,7.75,91.25,12.0,1.1589890596655764e+16
7,7.75,91.75,14.5,1.0453168566752552e+16  
8,7.75,92.75,16.0,1.0493223728181644e+16
9,7.75,93.25,18.5,1.0345603893411792e+16
10,7.75,95.25,21.0,1.141171929073354e+16
11,7.75,96.75,22.5,7782035033549430.0
12,8.25,70.25,24.0,8201072171587072.0
13,8.25,70.75,25.5,7602494056851770.0
14,8.25,73.25,28.0,9214770255302838.0
15,8.25,73.75,30.0,7005492206478936.0
16,8.25,74.25,31.0,8266837257825698.0
17,8.25,76.25,32.5,8149348547108306.0
18,8.25,78.25,34.5,7761299283688882.0
19,8.25,79.25,36.5,8122365507049480.0
20,8.25,84.25,38.5,7505510890250006.0
21,8.25,86.75,40.0,9349574410796046.0
22,8.25,87.25,41.0,9759035121454402.0
  • 1
    See [mcve]. The code is not runnable, hence it's not possible to say where the error comes from. – ImportanceOfBeingErnest Jul 09 '18 at 01:10
  • I ran this code.. How is this not runnable! – Satya Prakash Dash Jul 09 '18 at 06:18
  • 1
    @SatyaPrakashDash It is not runnable because we don't have the file `'E:/algo/data/combining/FINAL_DATA.csv'`. Also, is this question about `pcolormesh()` (like you say in the title of the question) or about `pcolor()` (which you use in your example code)? – Thomas Kühn Jul 09 '18 at 06:38
  • It is a .csv file containing latitude,longitude and ammonia concentration values like: – Satya Prakash Dash Jul 09 '18 at 06:45
  • 1
    I know that. The point is that we cannot run your code and reproduce the error. Have you actually followed the link that @ImportanceOfBeingErnest has provided? It explains very well how you can cook down your problem into a minimal runnable example. – Thomas Kühn Jul 09 '18 at 06:48
  • thanks for pointitng out...I gave you some data of my csv file and changed it to pcolormesh() @ThomasKühn – Satya Prakash Dash Jul 09 '18 at 07:42
  • @ImportanceOfBeingErnest The answer you link doesn't quite answer this question. The data in the provided example csv file is not on a regular grid. I already have an answer ready -- you beat me by a few minutes :) – Thomas Kühn Jul 09 '18 at 09:18
  • @ThomasKühn Looks like you're right. In any case this is one of the most common questions asked, so I think in general it would be better to answer existing questions on that topic rather then allowing for yet another one with its own answer. I changed the duplicate target, but maybe you find an even better one or can provide your answer on an existing question if the approach differs. – ImportanceOfBeingErnest Jul 09 '18 at 09:25
  • @ImportanceOfBeingErnest ok, point taken. – Thomas Kühn Jul 09 '18 at 09:27
  • @SatyaPrakashDash in addition to the linked duplicate, note also that your use of `np.genfromtext()` is wrong. You have to fix your csv file such that the header contains a valid string for each column (no empty strings) and then use `names=True` instead of what you have. It appears that you cannot select only certain columns with `genfromtext`. See [here](https://stackoverflow.com/a/50884286/2454357) for more info. – Thomas Kühn Jul 09 '18 at 09:30
  • I used data['lon'][1:] because the 1st value was nan maybe due to header. And I think I can remove header using skip_header = 1 Actually the problem is when I do a pcolormesh() plot then the z value should not be a nx1 matrix which I am getting here.That i couldnt able to make it and hence the error @ThomasKühn – Satya Prakash Dash Jul 09 '18 at 10:11

0 Answers0