Manipulations sur des courbes de niveau

by Joseph Razik, last modified on 2019-10-18
lignes_de_niveaux
In [1]:
%pylab inline
Populating the interactive namespace from numpy and matplotlib
In [2]:
import csv
In [3]:
csvfile = open('results_and_cart.csv', 'r')
spamreader = csv.reader(csvfile, delimiter=';')
In [4]:
X = list()
Y = list()
Z = list()
for row in spamreader:
    X.append(row[2])
    Y.append(row[1])
    Z.append(row[4])
print(X, Y, Z)
# Un point n'a pas de valeur, on l'enleve
X.pop(3)
Y.pop(3)
Z.pop(3)
['5.92398', '5.912557', '5.930175', '', '5.931817', '5.915823', '5.911687', '5.902029', '5.892169', '5.886', '5.894633', '5.881573', '5.901615', '5.910198', '5.9125616667', '5.92139', '5.926833', '5.91695', '5.91347', '5.91285', '5.911132', '5.9028333333', '5.912195', '5.920488', '5.9249233333', '5.92729', '5.928222', '5.94568', '5.9409266667', '5.93929', '5.958725', '5.9628066667', '5.9543', '5.9579', '5.9698866667', '5.975805', '5.988633', '5.9940916667', '6.011076', '6.006637', '5.959012', '5.977893'] ['43.12257', '43.12117', '43.11961', '', '43.11586', '43.11456', '43.11353', '43.11321', '43.11351', '43.11563', '43.10686', '43.10221', '43.10746', '43.10574', '43.10978', '43.1092833333', '43.10916', '43.10371', '43.10021', '43.09319', '43.08941', '43.0851666667', '43.0864083333', '43.089', '43.0929033333', '43.08261', '43.0998', '43.10062', '43.09607', '43.08802', '43.08071', '43.08689', '43.09618', '43.10303', '43.10246', '43.09268', '43.10155', '43.088945', '43.0938', '43.08361', '43.06977', '43.0779'] ['4186', '3691', '2986', '3152', '3447', '3580', '3470', '3393', '3366', '2711', '3100', '3346', '3313', '3081', '2924', '3204', '3372', '2725', '3569', '3034', '3681', '3727', '3519', '3319', '3561', '3323', '2942', '1781', '2227', '2950', '3073', '3046', '3088', '3213', '2891', '3163', '2477', '2966', '2898', '2852', '3074', '3371']
Out[4]:
'3152'
In [5]:
x = array([float(v) for v in X])
y = array([float(v) for v in Y])
z = array([float(v) for v in Z])
print(x.min(), x.max())
print(y.min(), y.max())
print(z.min(), z.max())
5.881573 6.011076
43.06977 43.12257
1781.0 4186.0
In [6]:
x_min = 5.88
x_max = 6.02
delta_x = (x_max - x_min) / 100
gx = np.arange(x_min, x_max, delta_x)
y_min = 43.06
y_max = 43.13
delta_y = (y_max - y_min) / 100
gy = np.arange(y_min, y_max, delta_y)
X, Y = np.meshgrid(gx, gy)
In [7]:
fig = figure(figsize=(24,12))
Z = griddata(x, y, z, X, Y, interp='nn')

from matplotlib.ticker import MaxNLocator

levels = MaxNLocator(nbins=15).tick_values(z.min(), z.max())

contour(X, Y, Z, levels=levels, colors='black', linewidth=2)
contourf(X, Y, Z, levels=levels)
#,             cmap=cmap)
colorbar()

scatter(x, y)
Out[7]:
<matplotlib.collections.PathCollection at 0x7fb21649fef0>
In [8]:
fig.savefig('resultats_contour.png', format='png')
In [9]:
X
Out[9]:
array([[ 5.88  ,  5.8814,  5.8828, ...,  6.0158,  6.0172,  6.0186],
       [ 5.88  ,  5.8814,  5.8828, ...,  6.0158,  6.0172,  6.0186],
       [ 5.88  ,  5.8814,  5.8828, ...,  6.0158,  6.0172,  6.0186],
       ..., 
       [ 5.88  ,  5.8814,  5.8828, ...,  6.0158,  6.0172,  6.0186],
       [ 5.88  ,  5.8814,  5.8828, ...,  6.0158,  6.0172,  6.0186],
       [ 5.88  ,  5.8814,  5.8828, ...,  6.0158,  6.0172,  6.0186]])
In [10]:
Z
Out[10]:
masked_array(data =
 [[-- -- -- ..., -- -- --]
 [-- -- -- ..., -- -- --]
 [-- -- -- ..., -- -- --]
 ..., 
 [-- -- -- ..., -- -- --]
 [-- -- -- ..., -- -- --]
 [-- -- -- ..., -- -- --]],
             mask =
 [[ True  True  True ...,  True  True  True]
 [ True  True  True ...,  True  True  True]
 [ True  True  True ...,  True  True  True]
 ..., 
 [ True  True  True ...,  True  True  True]
 [ True  True  True ...,  True  True  True]
 [ True  True  True ...,  True  True  True]],
       fill_value = 1e+20)
In [11]:
Z[20,:]
Out[11]:
masked_array(data = [-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
 -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
 3239.129284580858 3215.422155607985 3192.654512940078 3171.145705607557
 3151.44816434993 3134.602887125695 3122.245247926261 3111.466212840688
 3102.542183871429 3096.5877896707657 3094.856116980025 3098.922326302681
 3110.7906953955326 3131.286757661708 3153.1188889587966 3174.951020255864
 3196.783151552785 3204.40627611275 3167.7861007736014 3116.081609503823
 3057.7959976095626 -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
 -- -- -- -- -- -- -- -- -- -- -- -- -- -- --],
             mask = [ True  True  True  True  True  True  True  True  True  True  True  True
  True  True  True  True  True  True  True  True  True  True  True  True
  True  True  True  True  True  True  True  True  True  True  True  True
  True  True  True  True  True  True  True  True  True  True False False
 False False False False False False False False False False False False
 False False False False False False False  True  True  True  True  True
  True  True  True  True  True  True  True  True  True  True  True  True
  True  True  True  True  True  True  True  True  True  True  True  True
  True  True  True  True],
       fill_value = 1e+20)