Colormap data from file - pcolormesh problem - alrm31015 - May-19-2019
Hello!
I want to make a colormap of some data from a file. The file is the same type as essai0 but longer (around 280 data points). The first 2 columns are the points locations (Latitude and Longitude) and the 3rd and 4th columns are the 2 sets of data measured at those locations.
Here are a few lines of the file essai0.txt (I can't attach it for a reason) :
Output: 44.74104950 -0.579805180 81.30 4.28
44.74116118 -0.579861860 78.27 3.51
44.74124210 -0.579962550 84.78 3.82
44.74137597 -0.580031900 89.26 4.59
44.74158193 -0.580160010 92.86 0.00
44.74161419 -0.580185050 93.25 0.00
44.74173365 -0.580261140 81.91 53.5
44.74186340 -0.580293890 73.33 7.77
44.74198296 -0.580356860 86.29 6.21
44.74212337 -0.580434390 84.97 6.00
44.74223479 -0.580489040 83.99 5.36
44.74235165 -0.580544960 87.05 0.00
44.74247756 -0.580614870 73.10 4.76
44.74249296 -0.580633930 73.68 4.90
44.74252739 -0.580669520 67.61 4.51
44.74260257 -0.580804250 0.00 10.4
44.74268116 -0.580936440 69.62 4.60
44.74275689 -0.581071310 82.75 4.29
44.74285107 -0.581215710 78.22 8.05
44.74292680 -0.581346490 86.00 3.92
44.74299962 -0.581484080 87.70 5.74
44.74307632 -0.581618940 86.69 4.84
44.74314913 -0.581744280 86.31 7.16
44.74322911 -0.581886690 69.60 6.40
44.74332423 -0.582023960 81.31 4.13
44.74342116 -0.582166330 44.78 4.98
44.74349634 -0.582309960 73.80 3.81
44.74357153 -0.582452320 57.59 5.96
44.74365215 -0.582581970 74.39 4.90
44.74370741 -0.582692550 77.39 0.00
44.74353226 -0.582890600 49.72 7.40
44.74341281 -0.582642070 59.08 6.52
44.74330434 -0.582449410 74.65 2.61
44.74318351 -0.582239410 30.12 4.27
44.74306680 -0.582031340 45.25 1.83
44.74293637 -0.581830980 82.35 3.04
44.74283339 -0.581642170 84.10 1.88
44.74271119 -0.581428320 83.77 3.48
44.74259448 -0.581231810 70.90 3.57
44.74248875 -0.581035310 56.10 5.81
44.74239996 -0.580874700 51.16 0.76
44.74236539 -0.580821500 57.64 2.55
44.74204718 -0.580673540 56.43 8.61
44.74187423 -0.580587260 82.62 4.83
44.74173075 -0.580497390 58.78 5.76
44.74155268 -0.581446470 70.67 5.28
44.74141334 -0.580316510 72.57 2.25
44.74127427 -0.580245120 26.62 4.83
44.74112162 -0.580176110 57.38 3.88
44.74092997 -0.580076170 87.52 3.20
I originally thought of using plt.imshow , but I saw that this requires to use np.meshgrid in my case, and I don't really know how to input a Z (the data) that is not a function of the Latitude and Longitude. Therefore I decided to use ax.pcolormesh which syntax seemed more simple.
My code so far is as follows (I am using Python 2.7):
import numpy as np
import matplotlib.pyplot as plt
Lat,Long,data0,data1=np.loadtxt('essai0.txt',unpack=True)
cmap=plt.get_cmap('RdYlBu')
fig, (ax0,ax1)=plt.subplots(nrows=2)
im0= ax0.pcolormesh(Long, Lat, data0, cmap=cmap)
fig.colorbar(im0, ax=ax0)
ax0.set_xlim(-0.584, -0,579)
ax0.set_ylim(44.740, 44.744)
ax0.set_xlabel('Longitude')
ax0.set_ylabel('Latitude')
ax0.set_title('data0')
im1= ax0.pcolormesh(Long, Lat, data1, cmap=cmap)
fig.colorbar(im1, ax=ax1)
ax0.set_xlim(-0.583, -0,579)
ax0.set_ylim(44.740, 44.744)
ax0.set_xlabel('Longitude')
ax0.set_ylabel('Latitude')
ax0.set_title('data1')
fig.tight_layout()
plt.show() I get the following error :Error: Traceback (most recent call last):
File "<ipython-input-2-b5abc51ccab3>", line 1, in <module>
runfile('C:/Users/Amelie/Desktop/essai-carte0.py', wdir='C:/Users/Amelie/Desktop')
File "C:\Python27\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 601, in runfile
execfile(filename, namespace)
File "C:\Python27\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 66, in execfile
exec(compile(scripttext, filename, 'exec'), glob, loc)
File "C:/Users/Amelie/Desktop/essai-carte0.py", line 29, in <module>
File "C:\Python27\lib\site-packages\matplotlib\axes\_axes.py", line 5090, in pcolormesh
X, Y, C = self._pcolorargs('pcolormesh', *args, allmatch=allmatch)
File "C:\Python27\lib\site-packages\matplotlib\axes\_axes.py", line 4693, in _pcolorargs
numRows, numCols = C.shape
ValueError: need more than 1 value to unpack
Could you please help to understand how to put my data in the correct format to be unpacked? Or explain another way to use pcolormesh or imshow for example in my case?
The original file I am trying to map contains 13 sets of data, with around 280 rows (I haven't tested the wholes file on pythn yet).
Thanks in advance,
Best Regards.
|