How to calculate histogram intersection? - python

I have two datasets:
data1 = np.array([11.59877469, 12.52525819, 11.30371594, 9.09666023, 9.69178239,
15.50122531, 16.73578474, 11.59361365, 9.33284219, 10.24323511,
12.39877469, 14.22419388, 11.18491025, 9.38917885, 9.56598452,
13.40122531, 13.58126828, 11.66021816, 9.90587666, 10.09661371,
12.69877469, 13.36333954, 11.40602325, 9.04926015, 9.58116652,
14.30122531, 13.41946488, 11.50330911, 9.70846369, 11.57496747,
13.09877469, 15.59128195, 12.38077981, 9.24187201, 9.4187133 ,
14.50122531, 14.00030896, 10.91137571, 10.09368741, 10.59655608,
12.19877469, 12.66493842, 11.151608 , 10.04291564, 10.75044566,
13.90122531, 13.80879138, 12.01213962, 9.77266764, 10.6047969 ,
11.29877469, 11.91469995, 10.49547065, 9.54128679, 10.70684913,
15.80122531, 15.69100383, 11.00648136, 10.26949539, 11.05984311,
13.29877469, 14.36322429, 10.39796445, 9.15126197, 10.19866594,
13.90122531, 13.22035631, 11.47720851, 10.43120555, 11.41316408,
13.09877469, 12.39251915, 11.18971136, 9.9574122 , 10.25744447,
14.20122531, 13.27219366, 11.60801697, 9.72976318, 12.07620948,
13.69877469, 16.08363333, 12.54008941, 9.38917885, 10.24985347,
14.20122531, 13.84544674, 11.29659942, 10.29799654, 10.78807365,
12.39877469, 13.80385357, 11.79124697, 9.85270434, 10.47867621,
15.30122531, 12.68375859, 11.88853282, 10.36460104, 11.52377995,
12.09877469, 12.36280497, 11.03280231, 8.76603826, 11.57464465,
15.70122531, 16.69782975, 11.86483278, 9.75586377, 11.00865558,
14.39877469, 14.31138694, 10.65958136, 10.03571398, 9.75056092,
13.30122531, 13.52878112, 12.46156728, 10.58571404, 12.04584549,
12.69877469, 13.2230095 , 11.41802602, 9.82180264, 9.27339153,
14.50122531, 13.09520827, 10.91617682, 9.59895472, 12.33344676,
13.29877469, 16.05391916, 12.32137696, 9.41767999, 9.83146261,
14.00122531, 14.13174836, 11.67702203, 9.43724456, 11.17869979,
12.19877469, 13.48024677, 10.98060112, 10.22832584, 10.64047961,
14.90122531, 13.34485454, 11.90053559, 10.23379258, 11.59774047,
13.19877469, 12.95059023, 10.88549547, 9.31057157, 11.71497469,
15.40122531, 16.38875513, 12.68748139, 10.10058357, 12.50913252,
14.09877469, 14.37710664, 11.30162088, 10.18782192, 10.10388188,
14.30122531, 13.33032237, 11.20389433, 10.76632313, 12.05343649,
13.09877469, 12.8918117 , 11.89595483, 9.50078288, 10.53745474,
14.70122531, 13.50665794, 11.46071013, 9.62505531, 12.11286483,
14.19877469, 16.84775416, 13.15122724, 10.29252979, 10.48626721,
14.60122531, 14.15322172, 11.65332199, 10.40960057, 12.80367496,
13.99877469, 13.09027045, 10.75948812, 9.74799647, 11.09682545,
14.70122531, 14.32261612, 12.15495084, 11.38675768, 12.20135753])
data2 = np.array([ 8.29102276, 8.81896403, 8.34976878, 6.42352427, 7.60717448,
11.70897724, 12.33303488, 8.46800077, 5.99812922, 7.01110385,
9.19102276, 10.2088251 , 7.81963878, 5.4700672 , 5.9739585 ,
8.70897724, 8.96487767, 7.99967417, 6.66146823, 6.83476828,
7.69102276, 8.39992335, 8.16675914, 6.29511636, 6.39364899,
10.30897724, 8.45040312, 8.19228603, 6.36654906, 8.26193451,
8.59102276, 11.18658667, 8.97980554, 5.79348751, 6.00367267,
9.60897724, 8.72976357, 7.91417073, 6.37615127, 6.71721123,
7.49102276, 8.6868748 , 7.18480092, 6.0359 , 5.92277097,
9.60897724, 8.545837 , 8.47760298, 5.90062301, 6.21032767,
7.39102276, 7.81907928, 7.17999982, 5.40106214, 6.25461858,
10.80897724, 11.00889351, 7.81186342, 5.67230835, 5.96003158,
8.19102276, 8.73112115, 6.52866357, 5.52706949, 4.4671902 ,
8.10897724, 8.19316585, 6.86320746, 6.08603321, 6.16608132,
6.79102276, 7.77483293, 7.52712017, 5.8504898 , 6.12882072,
8.50897724, 8.6267387 , 6.7990035 , 6.30474566, 7.22539441,
8.19102276, 10.96600474, 7.90754278, 5.22765471, 4.40776186,
8.50897724, 8.12679632, 6.83230576, 5.4844976 , 5.91578523,
6.69102276, 8.20840578, 7.03269298, 5.21565194, 5.05562527,
7.30897724, 7.55289343, 7.27693232, 5.92672361, 5.96003158,
6.99102276, 6.87038206, 6.68557263, 4.45240618, 5.20354631,
9.70897724, 10.90586864, 7.84036457, 5.95522475, 6.16543151,
6.89102276, 8.53201258, 5.92952852, 5.11574518, 4.783206 ,
7.70897724, 6.74452625, 6.08106276, 5.64620776, 5.95244058,
6.99102276, 7.40697979, 6.33365116, 4.83522934, 4.65111677,
8.50897724, 7.28871498, 6.29977521, 5.09927389, 6.87966444,
6.99102276, 10.09820922, 7.30360661, 5.13735016, 4.28326363,
8.40897724, 7.5750166 , 6.05496217, 5.00656879, 4.58470269,
5.59102276, 6.14985776, 6.17674211, 4.13858807, 4.790797 ,
6.60897724, 7.14838493, 6.35917805, 5.61290551, 5.48221238,
6.79102276, 6.94369276, 6.13863875, 4.45480673, 5.46078358,
8.90897724, 9.97235342, 7.59315097, 4.81395693, 4.91590048,
6.39102276, 7.53212783, 5.79631951, 4.70922198, 4.02602635,
7.50897724, 7.39109003, 6.52568932, 5.16827895, 5.08529488,
6.09102276, 7.06884081, 7.31560938, 4.59041629, 3.87181395,
7.40897724, 6.81783696, 6.42578256, 4.81875804, 5.69585312,
6.69102276, 10.16457875, 6.46205907, 4.9708389 , 3.51849298,
6.50897724, 7.2665918 , 5.5437311 , 4.37173093, 5.07011288,
5.19102276, 7.60543854, 6.30755057, 4.07198357, 3.76878908,
7.30897724, 6.88420648, 6.77290291, 5.13017559, 6.2691062 ])
and I'd like to plot two histograms:
x1,y1,_ = plt.hist(data1, alpha=0.4, bins=20)
x2,y2,_ = plt.hist(data2, alpha=0.4, bins=20)
the result is the following:
How can I calculate the area of intersecting region?
I tried to find similar bins centers (to find zero-crossing), but I wasn't succeeded, I also tried to find the minimum between two bins with the same index, but it doesn't work for negative data.

Make sure you use the same range and number of bins in both histograms:
>>> rng = min(data1.min(), data2.min()), max(data1.max(), data2.max())
>>> n1, bins1, _ = plt.hist(data1, alpha=0.4, bins=30, range=rng)
>>> n2, bins2, _ = plt.hist(data2, alpha=0.4, bins=30, range=rng)
Then the intersection can be easily calculated:
>>> intersection = np.minimum(n1, n2)
>>> area = intersection.sum()
>>> area
20.0
>>> plt.bar(bins1[:-1], intersection, width=bins1[1]- bins1[0])

You can use:
bin_edges = np.histogram_bin_edges(np.concatenate([data1, data2]))
hist1, _ = np.histogram(data1, bins=bin_edges)
hist2, _ = np.histogram(data2, bins=bin_edges)
np.minimum(hist1, hist2).sum()
Output:
20

Related

curve fitting with scipy

I am trying to fit a curve with curve_fit from SciPy. But it doesn't work as intended, and I don't know why. Here is my code:
xdata = np.asarray(std_ex_90degree[5050:5150,0])
ydata = np.asarray(std_ex_90degree[5050:5150,1])
print(xdata,ydata)
def Gauss(x, A, B):
y = A*np.exp(-1*B*x**2)
return y
popt, covariance = curve_fit(Gauss, xdata, ydata)
fit_A, fit_B = popt
fit_y = Gauss(xdata, fit_A, fit_B)
plt.scatter(xdata, ydata, label='data',s=5)
plt.plot(xdata, fit_y, '-', label='fit')
plt.legend()
As you can see the Gaussian Fit didn't work and I only got a straight line.
Here is the data:
[2834.486 2834.968 2835.45 2835.932 2836.414 2836.896 2837.378 2837.861
2838.343 2838.825 2839.307 2839.789 2840.271 2840.753 2841.235 2841.718
2842.2 2842.682 2843.164 2843.646 2844.128 2844.61 2845.093 2845.575
2846.057 2846.539 2847.021 2847.503 2847.985 2848.468 2848.95 2849.432
2849.914 2850.396 2850.878 2851.36 2851.843 2852.325 2852.807 2853.289
2853.771 2854.253 2854.735 2855.218 2855.699 2856.182 2856.664 2857.146
2857.628 2858.11 2858.592 2859.074 2859.557 2860.039 2860.521 2861.003
2861.485 2861.967 2862.449 2862.932 2863.414 2863.896 2864.378 2864.86
2865.342 2865.824 2866.307 2866.789 2867.271 2867.753 2868.235 2868.717
2869.199 2869.682 2870.164 2870.646 2871.128 2871.61 2872.092 2872.574
2873.056 2873.539 2874.021 2874.503 2874.985 2875.467 2875.949 2876.431
2876.914 2877.396 2877.878 2878.36 2878.842 2879.324 2879.806 2880.289
2880.771 2881.253 2881.735 2882.217]
[0.5027119 0.5155925 0.5296563 0.5450429 0.5619112 0.5804411 0.6008373
0.6233361 0.6482099 0.67577 0.7063611 0.7403504 0.7781109 0.8200049
0.8663718 0.9175249 0.9737514 1.035319 1.102472 1.175419 1.254304
1.339163 1.429889 1.526202 1.627649 1.733603 1.84322 1.955248
2.067605 2.176702 2.276757 2.359875 2.417753 2.445059 2.441798
2.41245 2.362954 2.298523 2.223243 2.14052 2.05336 1.964326
1.87539 1.787885 1.702644 1.620191 1.540921 1.465193 1.393333
1.325607 1.262171 1.203057 1.148185 1.097403 1.050529 1.007382
0.9678 0.9316369 0.8987471 0.8689752 0.8421496 0.8180863 0.7965991
0.7775094 0.76065 0.7458642 0.732995 0.7218768 0.7123291 0.7041584
0.6971676 0.6911709 0.6860058 0.6815417 0.6776828 0.674363 0.6715436
0.6692089 0.6673671 0.6660498 0.6653103 0.6652156 0.6658351 0.6672268
0.6694273 0.6724483 0.676279 0.6808962 0.686272 0.6923797 0.699192
0.7066767 0.7147906 0.7234787 0.7326793 0.7423348 0.7524015 0.7628553
0.7736901 0.7849081]
Inappropriate model. Minor adjustments to model yield a rough fit:
import numpy as np
from matplotlib import pyplot as plt
from scipy.optimize import curve_fit
xdata = np.array((
2834.486, 2834.968, 2835.45 , 2835.932, 2836.414, 2836.896, 2837.378, 2837.861,
2838.343, 2838.825, 2839.307, 2839.789, 2840.271, 2840.753, 2841.235, 2841.718,
2842.2 , 2842.682, 2843.164, 2843.646, 2844.128, 2844.61 , 2845.093, 2845.575,
2846.057, 2846.539, 2847.021, 2847.503, 2847.985, 2848.468, 2848.95 , 2849.432,
2849.914, 2850.396, 2850.878, 2851.36 , 2851.843, 2852.325, 2852.807, 2853.289,
2853.771, 2854.253, 2854.735, 2855.218, 2855.699, 2856.182, 2856.664, 2857.146,
2857.628, 2858.11 , 2858.592, 2859.074, 2859.557, 2860.039, 2860.521, 2861.003,
2861.485, 2861.967, 2862.449, 2862.932, 2863.414, 2863.896, 2864.378, 2864.86 ,
2865.342, 2865.824, 2866.307, 2866.789, 2867.271, 2867.753, 2868.235, 2868.717,
2869.199, 2869.682, 2870.164, 2870.646, 2871.128, 2871.61 , 2872.092, 2872.574,
2873.056, 2873.539, 2874.021, 2874.503, 2874.985, 2875.467, 2875.949, 2876.431,
2876.914, 2877.396, 2877.878, 2878.36 , 2878.842, 2879.324, 2879.806, 2880.289,
2880.771, 2881.253, 2881.735, 2882.217,
))
ydata = np.array((
0.5027119, 0.5155925, 0.5296563, 0.5450429, 0.5619112, 0.5804411, 0.6008373,
0.6233361, 0.6482099, 0.67577 , 0.7063611, 0.7403504, 0.7781109, 0.8200049,
0.8663718, 0.9175249, 0.9737514, 1.035319 , 1.102472 , 1.175419 , 1.254304 ,
1.339163 , 1.429889 , 1.526202 , 1.627649 , 1.733603 , 1.84322 , 1.955248 ,
2.067605 , 2.176702 , 2.276757 , 2.359875 , 2.417753 , 2.445059 , 2.441798 ,
2.41245 , 2.362954 , 2.298523 , 2.223243 , 2.14052 , 2.05336 , 1.964326 ,
1.87539 , 1.787885 , 1.702644 , 1.620191 , 1.540921 , 1.465193 , 1.393333 ,
1.325607 , 1.262171 , 1.203057 , 1.148185 , 1.097403 , 1.050529 , 1.007382 ,
0.9678 , 0.9316369, 0.8987471, 0.8689752, 0.8421496, 0.8180863, 0.7965991,
0.7775094, 0.76065 , 0.7458642, 0.732995 , 0.7218768, 0.7123291, 0.7041584,
0.6971676, 0.6911709, 0.6860058, 0.6815417, 0.6776828, 0.674363 , 0.6715436,
0.6692089, 0.6673671, 0.6660498, 0.6653103, 0.6652156, 0.6658351, 0.6672268,
0.6694273, 0.6724483, 0.676279 , 0.6808962, 0.686272 , 0.6923797, 0.699192 ,
0.7066767, 0.7147906, 0.7234787, 0.7326793, 0.7423348, 0.7524015, 0.7628553,
0.7736901, 0.7849081,
))
def gauss(x: np.ndarray, *args: float) -> np.ndarray:
a, b, c, d = args
return a*np.exp(-b*(x - c)**2) + d
popt, _ = curve_fit(
gauss, xdata, ydata,
p0=(1.7, 0.02, 2851, 0.7),
maxfev=100_000,
)
print(popt)
fit_y = gauss(xdata, *popt)
plt.scatter(xdata, ydata, label='data', s=5)
plt.plot(xdata, fit_y, '-', label='fit')
plt.legend()
plt.show()
[1.68927347e+00 2.10977276e-02 2.85117456e+03 6.81806648e-01]
To do any better, your model needs to change more.

Modify the code to loop over another dataset

I am using haversine_distance function to calculate distance between coordinates in a dataset to a specific coordinate. [start_lat, start_lon = 40.6976637, -74.1197643]
def haversine_distance(lat1, lon1, lat2, lon2):
r = 6371
phi1 = np.radians(lat1)
phi2 = np.radians(lat2)
delta_phi = np.radians(lat2-lat1)
delta_lambda = np.radians(lon2-lon1)
a = np.sin(delta_phi / 2)**2 + np.cos(phi1) * np.cos(phi2) * np.sin(delta_lambda / 2)**2
res = r * (2 * np.arctan2(np.sqrt(a), np.sqrt(1-a)))
return np.round(res, 2)
start_lat, start_lon = 40.6976637, -74.1197643
distances_km = []
for row in pandas_df.itertuples(index=False):
distances_km.append(
haversine_distance(start_lat, start_lon, row.lat, row.lon)
)
pandas_df['Distance'] = distances_km
pandas_df
This successfully creates a column in my data set measuring the distance from given point like this:
Now I want to modify this code so that instead of using [start_lat, start_lon = 40.6976637, -74.1197643] I want to use another dataset containing cities.
How can I modify this existing code such that I create column for every city using its coordinates instead.
So desired output shows different columns with each city name and distance as calculated above.
Any Help is appreciated, new to python!
Cities array as requested in comments
[['Nanaimo' -123.9364 49.1642]
['Prince Rupert' -130.3271 54.3122]
['Vancouver' -123.1386 49.2636]
['Victoria' -123.3673 48.4275]
['Edmonton' -113.4909 53.5445]
['Winnipeg' -97.1392 49.8994]
['Sarnia' -82.4065 42.9746]
['Sarnia' -82.4065 42.9746]
['North York' -79.4112 43.7598]
['Kingston' -76.4812 44.2305]
['St. Catharines' -79.2333 43.1833]
['Thunder Bay' -89.2461 48.3822]
['Gaspé' -64.4833 48.8333]
['Cap-aux-Meules' -61.8607 47.3801]
['Kangiqsujuaq' -71.9667 61.6]
['Montreal' -73.5534 45.5091]
['Quebec City' -71.2074 46.8142]
['Rimouski' -68.524 48.4489]
['Sept-Îles' -66.3833 50.2167]
['Bathurst' -65.6497 47.6186]
['Charlottetown' -63.1399 46.24]
['Corner Brook' -57.9711 48.9411]
['Dartmouth' -63.5714 44.6715]
['Lewisporte' -55.0667 49.2333]
['Port Hawkesbury' -61.3642 45.6153]
['Saint John' -66.0628 45.2796]
["St. John's" -52.7072 47.5675]
['Sydney' -60.1947 46.1381]
['Yarmouth' -66.1175 43.8361]]
The beauty of Python is that you can use the same code to do different things.
To consider different [start_lat, start_lon] values for every column in your data, you can use the same code that you have now. All you need to do is to define start_lat and start_lon as arrays:
# --------------------- Array Initialization ---------------------
import pandas as pd
import numpy as np
np.random.seed(0)
pandas_df = pd.DataFrame(data = {'lat': np.random.rand(100),
'lon': np.random.rand(100)})
start_cities = pd.DataFrame([['Nanaimo' , -123.9364 , 49.1642], ['Prince Rupert' , -130.3271 , 54.3122],
['Vancouver' , -123.1386 , 49.2636], ['Victoria' , -123.3673 , 48.4275],
['Edmonton' , -113.4909 , 53.5445], ['Winnipeg' , -97.1392 , 49.8994],
['Sarnia' , -82.4065 , 42.9746], ['Sarnia' , -82.4065 , 42.9746],
['North York' , -79.4112 , 43.7598], ['Kingston' , -76.4812 , 44.2305],
['St. Catharines' , -79.2333 , 43.1833], ['Thunder Bay' , -89.2461 , 48.3822],
['Gaspé' , -64.4833 , 48.8333], ['Cap-aux-Meules' , -61.8607 , 47.3801],
['Kangiqsujuaq' , -71.9667 , 61.6 ], ['Montreal' , -73.5534 , 45.5091],
['Quebec City' , -71.2074 , 46.8142], ['Rimouski' , -68.524 , 48.4489],
['Sept-Îles' , -66.3833 , 50.2167], ['Bathurst' , -65.6497 , 47.6186],
['Charlottetown' , -63.1399 , 46.24 ], ['Corner Brook' , -57.9711 , 48.9411],
['Dartmouth' , -63.5714 , 44.6715], ['Lewisporte' , -55.0667 , 49.2333],
['Port Hawkesbury' , -61.3642 , 45.6153], ['Saint John' , -66.0628 , 45.2796],
["St. John's" , -52.7072 , 47.5675], ['Sydney' , -60.1947 , 46.1381],
['Yarmouth' , -66.1175 , 43.8361]])
start_cities.columns = 'names', 'start_lat', 'start_lon'
start_lat = start_cities.start_lat
start_lon = start_cities.start_lon
# --------------------- Same code as before (as promised) ---------------------
def haversine_distance(lat1, lon1, lat2, lon2):
r = 6371
phi1 = np.radians(lat1)
phi2 = np.radians(lat2)
delta_phi = np.radians(lat2-lat1)
delta_lambda = np.radians(lon2-lon1)
a = np.sin(delta_phi / 2)**2 + np.cos(phi1) * np.cos(phi2) * np.sin(delta_lambda / 2)**2
res = r * (2 * np.arctan2(np.sqrt(a), np.sqrt(1-a)))
return np.round(res, 2)
distances_km = []
for row in pandas_df.itertuples(index=False):
distances_km.append(
haversine_distance(start_lat, start_lon, row.lat, row.lon))
# --------------------- Store data ---------------------
distances_km = np.array(distances_km)
for ind, name in enumerate(start_cities.names):
pandas_df['distance_km_' + name] = distances_km[:,ind]
# print(pandas_df.keys())
# ["lat" , "lon" ,
# "distance_km_Nanaimo" , "distance_km_Prince Rupert" ,
# "distance_km_Vancouver" , "distance_km_Victoria" ,
# "distance_km_Edmonton" , "distance_km_Winnipeg" ,
# "distance_km_Sarnia" , "distance_km_North York" ,
# "distance_km_Kingston" , "distance_km_St. Catharines" ,
# "distance_km_Thunder Bay" , "distance_km_Gaspé" ,
# "distance_km_Cap-aux-Meules" , "distance_km_Kangiqsujuaq" ,
# "distance_km_Montreal" , "distance_km_Quebec City" ,
# "distance_km_Rimouski" , "distance_km_Sept-Îles" ,
# "distance_km_Bathurst" , "distance_km_Charlottetown" ,
# "distance_km_Corner Brook" , "distance_km_Dartmouth" ,
# "distance_km_Lewisporte" , "distance_km_Port Hawkesbury",
# "distance_km_Saint John" , "distance_km_St. John's" ,
# "distance_km_Sydney" , "distance_km_Yarmouth" ]

Normalize data for colormap

I am plotting color for geopandas shape with 2 array data:
Here's my first array.
newI =
array([ -467, -415, -414, -1001, -246, -147, -523, -327, -583,
-541, -290, -415, -453, -505, -791, -812, -672, -558,
-559, -1055, -327, -703, -419, -499, -273, -574, -802,
-450, -743, -221, -1282, -704, -352, -734, -430, -353,
-515, -1121, -664, -586, -171, -881, -402, -1024, -543,
-527, -384, -775, -931, -1380, -1662, -1069, -952, -435,
-1051, -921, -1211, -794, -547, -313, -511, -993, -430,
-262, -255, -675, -793, -1053, -702, -967, -1016, -230,
-405, -869, -689, -935, -190, -1473, -883, -1233, -240,
-607, -339, -1130, -909, -836, -667, -457, -847, -538,
-606, -457, -800, -322, -1339, -691, -627, -689, -365,
-600, -289, -810, -577, -187, -375, -574, -426, -662,
-695, -1003, -40, -1012, -279, -966, -587, -641, -753,
-461, -563, -604, -1013, -625, -506, -416, -1385, -459,
-760, -347, -308, -555, -325, -1588, -566, -533, -843,
-501, -448, -1022, -654, -602, -1201, -814, -754, -361,
-325, -1141, -725, -256, -601, -379, -496, -1099, -1101,
-598, -442, -773, -295, -1292, -558, -1234, -868, -1135,
-251, -1398, -453, -563, -1306, -693, -560, -512, -935,
-1168, -482, -431, -1199, -1249, -1199, -413, -1018, -194,
-375, -932, -1028, -436, -955, -463, -1303, -676, -554,
-601, -875, -661, -791, -443, -89, -879, -606, -577,
-475, -802, -734, -660, -684, -174, -902, -1241, -1320,
-575, -855, -222, -890, -701, -1082, -531, -693, -1008,
-1357, -433, -379, -192, -343, -477, -230, -938, -675,
-798, -259, -398, -778, -484, -817, -453, -564, -536,
-1599, -968, -547, -845, -1592, -256, -1139, -229, -926,
-474, -392, -990, -295, -558, -465, -497, -395, -468,
-310, -507, -1205, -705, -739, -609, -809, -610, -421,
-1057, -2023, -1105, -618, -466, -1291, -616, -620, -571,
-904, -383, -544, -688, -461, -769, -990, -664, -405,
-419, -852, -435, -298, -782, -758, -371, -813, -421,
-594, -259, -284, -215, -452, -430, -936, -994, -981,
-502, -510, -671, -721, -829, -523, -288, -653, -493,
-983, -1205, -722])
and Here's my second array:
array([-2407, -1992, -3400, -4826, -1544, -820, -3120, -1469, -2869,
-3622, -1738, -2122, -2773, -2939, -3558, -3575, -3082, -2494,
-3591, -5022, -1619, -2608, -3371, -3054, -1596, -2538, -3566,
-2035, -3490, -522, -5362, -3055, -1517, -4107, -2039, -2497,
-2302, -5513, -3876, -4303, -831, -4457, -2027, -5083, -2716,
-2284, -1288, -3781, -4707, -6903, -8592, -5763, -4644, -1999,
-4894, -3190, -6263, -3484, -3090, -1899, -2640, -3940, -2919,
-629, -2018, -4228, -4075, -5249, -2794, -4061, -4089, -1500,
-2434, -3867, -3359, -4070, -1472, -7334, -4367, -5422, -1563,
-3092, -1803, -4664, -4096, -3875, -3061, -1181, -4098, -2850,
-4356, -2239, -3102, -1498, -6458, -3495, -2863, -3568, -1752,
-3422, -1768, -3675, -2061, -919, -1452, -2512, -1924, -3668,
-3931, -4348, -284, -6232, -1065, -4261, -2739, -3392, -3962,
-2369, -2508, -3156, -4759, -3012, -3345, -2566, -7910, -2215,
-3581, -1357, -2155, -2643, -1420, -7449, -3023, -2982, -4913,
-2835, -1748, -4679, -2950, -2951, -5515, -4195, -3568, -1746,
-1437, -5429, -3246, -1556, -2635, -1534, -3553, -4451, -5655,
-2616, -2724, -4445, -1642, -6640, -2869, -5211, -5014, -4909,
-1103, -5658, -2096, -2427, -5719, -3152, -2717, -2544, -4226,
-4813, -2319, -2261, -4844, -5383, -5057, -2981, -5448, -1526,
-1749, -3550, -3736, -1893, -5812, -2686, -5923, -3145, -3569,
-2523, -4586, -2931, -4104, -2301, -666, -4402, -3201, -3171,
-2598, -4279, -3765, -3024, -3085, -468, -3732, -5899, -6464,
-3993, -4583, -1126, -4193, -4214, -3902, -2132, -3712, -4879,
-6907, -1524, -1987, -1444, -2086, -3229, -1316, -4331, -3150,
-4449, -1700, -1486, -3650, -2478, -4166, -2618, -3308, -2458,
-7441, -4452, -2438, -4722, -6949, -1712, -4727, -792, -4193,
-1610, -1951, -3965, -1410, -2958, -2167, -2050, -2035, -2152,
-2236, -3235, -5999, -4024, -3111, -3196, -3881, -2647, -2579,
-6387, -9912, -4677, -2983, -1913, -7547, -3166, -2990, -2183,
-3401, -2080, -3056, -2225, -2546, -4421, -3867, -2975, -1552,
-2090, -3871, -1768, -2032, -3564, -3273, -1579, -4338, -1371,
-3600, -1253, -2083, -1439, -2281, -2045, -4406, -4380, -4129,
-2520, -2529, -2108, -3081, -3561, -2601, -843, -3069, -1852,
-5888, -5730, -3386])
The code to plot those array data is as shown below.
area_gpd = gpd.read_file("....shp")
area_gpd['population'] = newI
plt.rcParams.update({'font.size':32})
west,south,east,north = area.unary_union.bounds
fig,ax = plt.subplots(figsize=(40,40))
cmap = LinearSegmentedColormap.from_list('mycmap', [ 'green','white'])
melbourne_gpd.plot(ax=ax, column='population',legend=False,cmap=cmap,zorder=3)
sm = plt.cm.ScalarMappable(cmap=cmap,\
norm=plt.Normalize(vmin=-9912,
vmax=-284))
It keeps normalizing things so the intensity shows now different.
Is there any function to normalize this data?
I want the map to be darker for those with a larger value. Can anyone give me some recommendations?
Thanks so much
I found a nice solution for the question from a guy on stackoverflow:
import scipy as sp
import matplotlib as mpl
import matplotlib.pyplot as plt
class MidpointNormalize(mpl.colors.Normalize):
def __init__(self, vmin, vmax, midpoint=0, clip=False):
self.midpoint = midpoint
mpl.colors.Normalize.__init__(self, vmin, vmax, clip)
def __call__(self, value, clip=None):
normalized_min = max(0, 1 / 2 * (1 - abs((self.midpoint - self.vmin) / (self.midpoint - self.vmax))))
normalized_max = min(1, 1 / 2 * (1 + abs((self.vmax - self.midpoint) / (self.midpoint - self.vmin))))
normalized_mid = 0.5
x, y = [self.vmin, self.midpoint, self.vmax], [normalized_min, normalized_mid, normalized_max]
return sp.ma.masked_array(sp.interp(value, x, y))
vals = sp.array([[-5., 0], [5, 10]])
vmin = -1225
vmax = 669
norm = MidpointNormalize(vmin=vmin, vmax=vmax, midpoint=0)
It will nicely complete the job for your color map.
Here is the link to the place I found the solution : Colorplot that distinguishes between positive and negative values

plt.tricontourf(x,y,z) creating color values outside of data bounds

I am attempting to make compressor and turbine maps colored by efficiency. I have achieved this, but the tricontourf I am attempting leads to level colors outside of where my data even exists. I need to make sure the contour ends at the bounds of my data. Is there a way to achieve this?
My code:
import numpy as np
import matplotlib.pyplot as plt
alphaMap = np.array([0.000, 90.000])
NcMap = np.array([0.300, 0.400, 0.500, 0.600, 0.700, 0.750, 0.800, 0.850, 0.900, 0.950, 1.000, 1.050, 1.100, 1.150])
RlineMap = np.array([1.000, 1.200, 1.400, 1.600, 1.800, 2.000, 2.200, 2.400, 2.600, 2.800, 3.000])
WCmap = np.array([[[17.907, 19.339, 20.749, 22.136, 23.498, 24.833, 26.141, 27.420, 28.669, 29.887, 31.011],
[24.951, 26.742, 28.485, 30.177, 31.815, 33.397, 34.921, 36.385, 37.788, 39.128, 40.405],
[32.682, 34.715, 36.662, 38.520, 40.286, 41.958, 43.533, 45.011, 46.390, 47.669, 48.848],
[40.927, 43.115, 45.168, 47.083, 48.858, 50.492, 51.983, 53.331, 54.539, 55.607, 56.537],
[49.850, 52.122, 54.195, 56.068, 57.741, 59.215, 60.494, 61.580, 62.479, 63.197, 63.739],
[54.798, 57.066, 59.099, 60.897, 62.463, 63.800, 64.913, 65.810, 66.497, 66.983, 67.278],
[60.051, 62.252, 64.185, 65.851, 67.255, 68.405, 69.307, 69.973, 70.413, 70.638, 70.675],
[65.313, 67.427, 69.262, 70.824, 72.118, 73.153, 73.938, 74.484, 74.803, 74.907, 74.907],
[70.995, 72.902, 74.542, 75.920, 77.043, 77.920, 78.560, 78.974, 79.174, 79.198, 79.198],
[77.441, 78.904, 80.155, 81.199, 82.042, 82.690, 83.151, 83.434, 83.545, 83.548, 83.548],
[84.344, 85.211, 85.952, 86.572, 87.074, 87.460, 87.735, 87.903, 87.967, 87.968, 87.968],
[89.305, 89.687, 90.025, 90.320, 90.572, 90.783, 90.953, 91.083, 91.174, 91.227, 91.243],
[93.626, 93.712, 93.793, 93.868, 93.939, 94.004, 94.064, 94.120, 94.170, 94.216, 94.257],
[95.978, 95.989, 96.000, 96.012, 96.022, 96.033, 96.044, 96.054, 96.064, 96.074, 96.084]],
[[17.907, 19.339, 20.749, 22.136, 23.498, 24.833, 26.141, 27.420, 28.669, 29.887, 31.011],
[24.951, 26.742, 28.485, 30.177, 31.815, 33.397, 34.921, 36.385, 37.788, 39.128, 40.405],
[32.682, 34.715, 36.662, 38.520, 40.286, 41.958, 43.533, 45.011, 46.390, 47.669, 48.848],
[40.927, 43.115, 45.168, 47.083, 48.858, 50.492, 51.983, 53.331, 54.539, 55.607, 56.537],
[49.850, 52.122, 54.195, 56.068, 57.741, 59.215, 60.494, 61.580, 62.479, 63.197, 63.739],
[54.798, 57.066, 59.099, 60.897, 62.463, 63.800, 64.913, 65.810, 66.497, 66.983, 67.278],
[60.051, 62.252, 64.185, 65.851, 67.255, 68.405, 69.307, 69.973, 70.413, 70.638, 70.675],
[65.313, 67.427, 69.262, 70.824, 72.118, 73.153, 73.938, 74.484, 74.803, 74.907, 74.907],
[70.995, 72.902, 74.542, 75.920, 77.043, 77.920, 78.560, 78.974, 79.174, 79.198, 79.198],
[77.441, 78.904, 80.155, 81.199, 82.042, 82.690, 83.151, 83.434, 83.545, 83.548, 83.548],
[84.344, 85.211, 85.952, 86.572, 87.074, 87.460, 87.735, 87.903, 87.967, 87.968, 87.968],
[89.305, 89.687, 90.025, 90.320, 90.572, 90.783, 90.953, 91.083, 91.174, 91.227, 91.243],
[93.626, 93.712, 93.793, 93.868, 93.939, 94.004, 94.064, 94.120, 94.170, 94.216, 94.257],
[96.084, 96.074, 96.064, 96.054, 96.044, 96.033, 96.022, 96.012, 96.000, 95.989, 95.978]]])
effMap = np.array([[[.8070, .8291, .8461, .8566, .8586, .8497, .8170, .7410, .6022, .3674, .0000],
[.8230, .8454, .8628, .8741, .8775, .8708, .8419, .7732, .6477, .4372, .0916],
[.8411, .8631, .8805, .8921, .8966, .8918, .8671, .8065, .6959, .5124, .2168],
[.8565, .8783, .8957, .9077, .9131, .9099, .8883, .8338, .7340, .5696, .3083],
[.8662, .8879, .9055, .9179, .9239, .9219, .9024, .8520, .7600, .6096, .3739],
[.8699, .8917, .9093, .9218, .9281, .9265, .9080, .8598, .7721, .6297, .4089],
[.8743, .8957, .9130, .9253, .9316, .9304, .9131, .8678, .7858, .6538, .4519],
[.8836, .9026, .9179, .9287, .9342, .9331, .9183, .8804, .8128, .7065, .5485],
[.8943, .9103, .9230, .9319, .9362, .9351, .9231, .8930, .8406, .7602, .6442],
[.9060, .9169, .9253, .9310, .9334, .9321, .9236, .9036, .8703, .8211, .7529],
[.9170, .9224, .9264, .9288, .9293, .9280, .9231, .9127, .8962, .8730, .8423],
[.9159, .9171, .9176, .9177, .9171, .9159, .9136, .9097, .9042, .8968, .8876],
[.9061, .9059, .9055, .9052, .9047, .9042, .9036, .9028, .9018, .9007, .8994],
[.8962, .8964, .8965, .8966, .8967, .8968, .8969, .8970, .8971, .8972, .8973]],
[[.8070, .8291, .8461, .8566, .8586, .8497, .8170, .7410, .6022, .3674, .0714],
[.8230, .8454, .8628, .8741, .8775, .8708, .8419, .7732, .6477, .4372, .0916],
[.8411, .8631, .8805, .8921, .8966, .8918, .8671, .8065, .6959, .5124, .2168],
[.8565, .8783, .8957, .9077, .9131, .9099, .8883, .8338, .7340, .5696, .3083],
[.8662, .8879, .9055, .9179, .9239, .9219, .9024, .8520, .7600, .6096, .3739],
[.8699, .8917, .9093, .9218, .9281, .9265, .9080, .8598, .7721, .6297, .4089],
[.8743, .8957, .9130, .9253, .9316, .9304, .9131, .8678, .7858, .6538, .4519],
[.8836, .9026, .9179, .9287, .9342, .9331, .9183, .8804, .8128, .7065, .5485],
[.8943, .9103, .9230, .9319, .9362, .9351, .9231, .8930, .8406, .7602, .6442],
[.9060, .9169, .9253, .9310, .9334, .9321, .9236, .9036, .8703, .8211, .7529],
[.9170, .9224, .9264, .9288, .9293, .9280, .9231, .9127, .8962, .8730, .8423],
[.9159, .9171, .9176, .9177, .9171, .9159, .9136, .9097, .9042, .8968, .8876],
[.9061, .9059, .9055, .9052, .9047, .9042, .9036, .9028, .9018, .9007, .8994],
[.8962, .8964, .8965, .8966, .8967, .8968, .8969, .8970, .8971, .8972, .8973]]])
PRmap = np.array([[[1.0678, 1.0649, 1.0613, 1.0571, 1.0522, 1.0468, 1.0402, 1.0322, 1.0227, 1.0117, 1.0000],
[1.1239, 1.1186, 1.1122, 1.1047, 1.0962, 1.0865, 1.0751, 1.0611, 1.0445, 1.0257, 1.0045],
[1.1994, 1.1910, 1.1809, 1.1691, 1.1558, 1.1409, 1.1233, 1.1020, 1.0771, 1.0488, 1.0173],
[1.2981, 1.2855, 1.2706, 1.2533, 1.2339, 1.2122, 1.1869, 1.1563, 1.1210, 1.0811, 1.0370],
[1.4289, 1.4111, 1.3899, 1.3655, 1.3380, 1.3076, 1.2720, 1.2295, 1.1804, 1.1254, 1.0654],
[1.5118, 1.4909, 1.4661, 1.4375, 1.4052, 1.3695, 1.3278, 1.2779, 1.2205, 1.1565, 1.0868],
[1.6070, 1.5827, 1.5538, 1.5205, 1.4831, 1.4417, 1.3934, 1.3358, 1.2697, 1.1962, 1.1165],
[1.7160, 1.6881, 1.6555, 1.6183, 1.5767, 1.5312, 1.4785, 1.4160, 1.3448, 1.2660, 1.1808],
[1.8402, 1.8086, 1.7724, 1.7318, 1.6869, 1.6381, 1.5824, 1.5170, 1.4430, 1.3615, 1.2736],
[1.9930, 1.9587, 1.9206, 1.8788, 1.8336, 1.7852, 1.7309, 1.6685, 1.5988, 1.5225, 1.4405],
[2.1593, 2.1257, 2.0899, 2.0518, 2.0117, 1.9695, 1.9235, 1.8724, 1.8163, 1.7557, 1.6909],
[2.2764, 2.2510, 2.2248, 2.1978, 2.1701, 2.1416, 2.1118, 2.0801, 2.0464, 2.0108, 1.9735],
[2.3771, 2.3664, 2.3557, 2.3448, 2.3339, 2.3229, 2.3118, 2.3004, 2.2887, 2.2768, 2.2646],
[2.4559, 2.4538, 2.4516, 2.4495, 2.4473, 2.4452, 2.443, 2.4409, 2.4387, 2.4365, 2.4343]],
[[1.0678, 1.0649, 1.0613, 1.0571, 1.0522, 1.0468, 1.0402, 1.0322, 1.0227, 1.0117, 1.0000],
[1.1239, 1.1186, 1.1122, 1.1047, 1.0962, 1.0865, 1.0751, 1.0611, 1.0445, 1.0257, 1.0045],
[1.1994, 1.1910, 1.1809, 1.1691, 1.1558, 1.1409, 1.1233, 1.1020, 1.0771, 1.0488, 1.0173],
[1.2981, 1.2855, 1.2706, 1.2533, 1.2339, 1.2122, 1.1869, 1.1563, 1.1210, 1.0811, 1.0370],
[1.4289, 1.4111, 1.3899, 1.3655, 1.3380, 1.3076, 1.2720, 1.2295, 1.1804, 1.1254, 1.0654],
[1.5118, 1.4909, 1.4661, 1.4375, 1.4052, 1.3695, 1.3278, 1.2779, 1.2205, 1.1565, 1.0868],
[1.6070, 1.5827, 1.5538, 1.5205, 1.4831, 1.4417, 1.3934, 1.3358, 1.2697, 1.1962, 1.1165],
[1.7160, 1.6881, 1.6555, 1.6183, 1.5767, 1.5312, 1.4785, 1.4160, 1.3448, 1.2660, 1.1808],
[1.8402, 1.8086, 1.7724, 1.7318, 1.6869, 1.6381, 1.5824, 1.5170, 1.4430, 1.3615, 1.2736],
[1.9930, 1.9587, 1.9206, 1.8788, 1.8336, 1.7852, 1.7309, 1.6685, 1.5988, 1.5225, 1.4405],
[2.1593, 2.1257, 2.0899, 2.0518, 2.0117, 1.9695, 1.9235, 1.8724, 1.8163, 1.7557, 1.6909],
[2.2764, 2.2510, 2.2248, 2.1978, 2.1701, 2.1416, 2.1118, 2.0801, 2.0464, 2.0108, 1.9735],
[2.3771, 2.3664, 2.3557, 2.3448, 2.3339, 2.3229, 2.3118, 2.3004, 2.2887, 2.2768, 2.2646],
[2.4343, 2.4365, 2.4387, 2.4409, 2.4430, 2.4452, 2.4473, 2.4495, 2.4516, 2.4538, 2.4559]]])
label = []
for x in NcMap:
label.append(x*100)
for i in range(0,14):
plt.annotate('{0}%'.format(round(label[i],2)),xy = ((flowmax[i],PRmax[i])), textcoords='offset points', xytext=(0,6), ha = 'center', color = 'b')
plt.xlim(0,1)
plt.ylim(1,8)
plt.ylabel(r'$PR_{off}$', fontsize=16)
plt.xlabel(r'$\.m_{c,off} [kg/s]$', fontsize=16)
x = WCmap[0,:14,:]
x = x.flatten().tolist()
y = PRmap[0,:14,:]
y = y.flatten().tolist()
z = effMap[0,:14,:]
z = z.flatten().tolist()
plt.tricontourf(x,y,z, cmap = 'jet')
cbar = plt.colorbar()
cbar.set_label(r'$\eta_{off}$', fontsize=16)
plt.show()
Compressor Map Plot

Plot two graphs on top of each other

I'm using matplotlib and I wish to plot a contourf on top of a pcolormesh, displaying both at the same time as described. Here is the code:
"""space for module inclusion"""
fig, ax = plt.subplots()
ax.pcolormesh(*arguments here*)
ax.contour(*arguments here*)
plt.show()
However, when I try to display the data, only contourf is shown (overrides pcolormesh). Any Ideas on this one?
EDIT_1:
So basically the values I'm using are the following:
X:
[ 63.37480164 63.37519836 63.37559891 63.37599945 63.37639999
63.37680054 63.37720108 63.37760162 63.37799835 63.3783989
63.37879944 63.37919998 63.37960052 63.38000107 63.38040161
63.38079834 63.38119888 63.38159943 63.38199997 63.38240051
63.38280106 63.3832016 63.38359833 63.38399887 63.38439941
63.38479996 63.3852005 63.38560104 63.38600159 63.38639832
63.38679886 63.3871994 63.38759995 63.38800049 63.38840103
63.38880157 63.3891983 63.38959885 63.38999939 63.39039993
63.39080048 63.39120102 63.39160156 63.39199829 63.39239883
63.39279938 63.39319992 63.39360046 63.39400101 63.39440155
63.39479828 63.39519882 63.39559937 63.39599991 63.39640045
63.39680099 63.39720154 63.39759827 63.39799881 63.39839935
63.3987999 63.39920044 63.39960098 63.40000153 63.40039825
63.4007988 63.40119934 63.40159988 63.40200043 63.40240097
63.40280151 63.40319824 63.40359879 63.40399933 63.40439987
63.40480042 63.40520096 63.4056015 63.40599823 63.40639877
63.40679932 63.40719986 63.4076004 63.40800095 63.40840149
63.40879822 63.40919876 63.4095993 63.40999985 63.41040039
63.41080093 63.41120148 63.41159821 63.41199875 63.41239929
63.41279984 63.41320038 63.41360092 63.41400146 63.41439819
63.41479874 63.41519928 63.41559982 63.41600037 63.41640091
63.41680145 63.41719818 63.41759872 63.41799927 63.41839981
63.41880035 63.4192009 63.41960144 63.41999817 63.42039871
63.42079926 63.4211998 63.42160034 63.42200089 63.42240143
63.42279816 63.4231987 63.42359924 63.42399979 63.42440033
63.42480087 63.42520142 63.42559814 63.42599869 63.42639923
63.42679977 63.42720032 63.42760086 63.4280014 63.42839813
63.42879868 63.42919922 63.42959976 63.43000031 63.43040085
63.43080139 63.43119812 63.43159866 63.43199921 63.43239975
63.43280029 63.43320084 63.43360138 63.43399811 63.43439865
63.43479919 63.43519974 63.43560028 63.43600082 63.43640137
63.4367981 63.43719864 63.43759918 63.43799973 63.43840027
63.43880081 63.43920135 63.4396019 63.43999863 63.44039917
63.44079971 63.44120026 63.4416008 63.44200134 63.44240189
63.44279861 63.44319916 63.4435997 63.44400024 63.44440079
63.44480133 63.44520187 63.4455986 63.44599915 63.44639969
63.44680023 63.44720078 63.44760132 63.44800186 63.44839859
63.44879913 63.44919968 63.44960022 63.45000076 63.45040131
63.45080185 63.45119858 63.45159912 63.45199966 63.45240021
63.45280075 63.45320129 63.45360184 63.45399857 63.45439911
63.45479965 63.4552002 63.45560074 63.45600128 63.45640182
63.45679855 63.4571991 63.45759964 63.45800018 63.45840073
63.45880127 63.45920181 63.45959854 63.45999908 63.46039963
63.46080017 63.46120071 63.46160126 63.4620018 63.46239853
63.46279907 63.46319962 63.46360016 63.4640007 63.46440125
63.46480179 63.46519852 63.46559906 63.4659996 63.46640015
63.46680069 63.46720123 63.46760178 63.4679985 63.46839905
63.46879959 63.46920013 63.46960068 63.47000122 63.47040176
63.47079849 63.47119904 63.47159958 63.47200012 63.47240067
63.47280121 63.47320175 63.47359848 63.47399902 63.47439957
63.47480011 63.47520065 63.4756012 63.47600174 63.47639847
63.47679901 63.47719955 63.4776001 63.47800064 63.47840118
63.47880173 63.47919846 63.479599 63.47999954 63.48040009
63.48080063 63.48120117 63.48160172 63.48199844 63.48239899
63.48279953 63.48320007 63.48360062 63.48400116 63.4844017
63.48479843 63.48519897 63.48559952 63.48600006 63.4864006
63.48680115 63.48720169 63.48759842 63.48799896 63.48839951
63.48880005 63.48920059 63.48960114 63.49000168 63.49039841
63.49079895 63.49119949 63.49160004 63.49200058 63.49240112
63.49280167 63.49319839 63.49359894 63.49399948 63.49440002
63.49480057 63.49520111 63.49560165 63.49599838 63.49639893
63.49679947 63.49720001 63.49760056 63.4980011 63.49840164
63.49879837 63.49919891 63.49959946 63.5 63.50040054
63.50080109 63.50120163 63.50159836 63.5019989 63.50239944
63.50279999 63.50320053 63.50360107 63.50400162 63.50439835
63.50479889 63.50519943 63.50559998 63.50600052 63.50640106
63.50680161 63.50719833 63.50759888 63.50799942 63.50839996
63.50880051 63.50920105 63.50960159 63.50999832 63.51039886
63.51079941 63.51119995 63.51160049 63.51200104 63.51240158
63.51279831 63.51319885 63.5135994 63.51399994 63.51440048
63.51480103 63.51520157 63.5155983 63.51599884 63.51639938
63.51679993 63.51720047 63.51760101 63.51800156 63.51839828
63.51879883 63.51919937 63.51959991 63.52000046 63.520401
63.52080154 63.52119827 63.52159882 63.52199936 63.5223999
63.52280045 63.52320099 63.52360153 63.52399826 63.5243988
63.52479935 63.52519989 63.52560043 63.52600098 63.52640152
63.52679825 63.52719879 63.52759933 63.52799988 63.52840042
63.52880096 63.52920151 63.52959824 63.52999878 63.53039932
63.53079987 63.53120041 63.53160095 63.5320015 63.53239822
63.53279877 63.53319931 63.53359985 63.5340004 63.53440094
63.53480148 63.53519821 63.53559875 63.5359993 63.53639984
63.53680038 63.53720093 63.53760147 63.5379982 63.53839874
63.53879929 63.53919983 63.53960037 63.54000092 63.54040146
63.54079819 63.54119873 63.54159927 63.54199982 63.54240036
63.5428009 63.54320145 63.54359818 63.54399872 63.54439926
63.5447998 63.54520035 63.54560089 63.54600143 63.54639816
63.54679871 63.54719925 63.54759979 63.54800034 63.54840088
63.54880142 63.54919815 63.54959869 63.54999924 63.55039978
63.55080032 63.55120087 63.55160141 63.55199814 63.55239868
63.55279922 63.55319977 63.55360031 63.55400085 63.5544014
63.55479813 63.55519867 63.55559921 63.55599976 63.5564003
63.55680084 63.55720139 63.55759811 63.55799866 63.5583992
63.55879974 63.55920029 63.55960083 63.56000137 63.5603981
63.56079865 63.56119919 63.56159973 63.56200027 63.56240082
63.56280136 63.5632019 63.56359863 63.56399918 63.56439972
63.56480026 63.56520081 63.56560135 63.56600189 63.56639862
63.56679916 63.56719971 63.56760025 63.56800079 63.56840134
63.56880188 63.56919861 63.56959915 63.56999969 63.57040024
63.57080078 63.57120132 63.57160187 63.5719986 63.57239914
63.57279968 63.57320023 63.57360077 63.57400131 63.57440186
63.57479858 63.57519913 63.57559967 63.57600021 63.57640076
63.5768013 63.57720184 63.57759857 63.57799911 63.57839966
63.5788002 63.57920074 63.57960129 63.58000183 63.58039856
63.5807991 63.58119965 63.58160019 63.58200073 63.58240128
63.58280182 63.58319855 63.58359909 63.58399963 63.58440018
63.58480072 63.58520126 63.58560181 63.58599854 63.58639908
63.58679962 63.58720016 63.58760071 63.58800125 63.58840179
63.58879852 63.58919907 63.58959961 63.59000015 63.5904007
63.59080124 63.59120178 63.59159851 63.59199905 63.5923996
63.59280014 63.59320068 63.59360123 63.59400177 63.5943985
63.59479904 63.59519958 63.59560013 63.59600067 63.59640121
63.59680176 63.59719849 63.59759903 63.59799957 63.59840012
63.59880066 63.5992012 63.59960175 63.59999847 63.60039902
63.60079956 63.6012001 63.60160065 63.60200119 63.60240173
63.60279846 63.60319901 63.60359955 63.60400009 63.60440063
63.60480118 63.60520172 63.60559845 63.60599899 63.60639954
63.60680008 63.60720062 63.60760117 63.60800171 63.60839844
63.60879898 63.60919952 63.60960007 63.61000061 63.61040115
63.6108017 63.61119843 63.61159897 63.61199951 63.61240005
63.6128006 63.61320114 63.61360168 63.61399841 63.61439896
63.6147995 63.61520004 63.61560059 63.61600113 63.61640167
63.6167984 63.61719894 63.61759949 63.61800003 63.61840057
63.61880112 63.61920166 63.61959839 63.61999893 63.62039948
63.62080002 63.62120056 63.6216011 63.62200165 63.62239838
63.62279892 63.62319946 63.62360001 63.62400055 63.62440109
63.62480164 63.62519836 63.62559891 63.62599945 63.62639999
63.62680054 63.62720108 63.62760162 63.62799835 63.6283989
63.62879944 63.62919998 63.62960052 63.63000107 63.63040161
63.63079834 63.63119888 63.63159943 63.63199997 63.63240051
63.63280106 63.6332016 63.63359833 63.63399887 63.63439941
63.63479996 63.6352005 63.63560104 63.63600159 63.63639832
63.63679886 63.6371994 63.63759995 63.63800049 63.63840103
63.63880157 63.6391983 63.63959885 63.63999939 63.64039993
63.64080048 63.64120102 63.64160156 63.64199829 63.64239883
63.64279938 63.64319992 63.64360046 63.64400101 63.64440155
63.64479828 63.64519882 63.64559937 63.64599991 63.64640045
63.64680099 63.64720154 63.64759827 63.64799881 63.64839935
63.6487999 63.64920044 63.64960098 63.65000153 63.65039825
63.6507988 63.65119934 63.65159988 63.65200043 63.65240097
63.65280151 63.65319824 63.65359879 63.65399933 63.65439987
63.65480042 63.65520096 63.6556015 63.65599823 63.65639877
63.65679932 63.65719986 63.6576004 63.65800095 63.65840149
63.65879822 63.65919876 63.6595993 63.65999985 63.66040039
63.66080093 63.66120148 63.66159821 63.66199875 63.66239929
63.66279984]
Y:
[[ 2.49405336 2.49405479 2.49405622 ..., 2.49506283 2.49506426
2.49506545]
[ 2.50891113 2.50891256 2.50891399 ..., 2.50994205 2.50994349
2.50994492]
[ 2.52241206 2.52241349 2.52241492 ..., 2.52346492 2.52346635
2.52346778]
...,
[ 3.86142349 3.8614254 3.86142731 ..., 3.86282992 3.86283183
3.86283374]
[ 3.87396121 3.87396312 3.87396502 ..., 3.87538362 3.87538576
3.87538767]
[ 3.88761282 3.88761473 3.88761663 ..., 3.8890295 3.88903141
3.88903356]]
Z:
[[ 1.17835083e+03 1.18324207e+03 1.19204639e+03 ..., -5.34249163e+00
5.41835642e+00 3.46183872e+00]
[ 1.25111658e+03 1.24381042e+03 1.25598743e+03 ..., 1.37508184e-01
2.57289410e+00 -3.10967302e+00]
[ 1.31315002e+03 1.30969629e+03 1.30394031e+03 ..., 3.06716180e+00
7.64734685e-01 -9.59618759e+00]
...,
[ 3.51441078e+01 3.44107513e+01 3.44107513e+01 ..., -5.19034863e+00
-1.89025664e+00 -4.45699453e+00]
[ 2.77138577e+01 2.72105122e+01 2.67071648e+01 ..., -1.48025024e+00
5.33136606e-01 -9.76903498e-01]
[ 2.52477798e+01 2.67249184e+01 2.82020588e+01 ..., 1.36409312e-01
-2.32875556e-01 -2.07929993e+00]]
and the code is the following:
fig,ax = plt.subplots()
ax.pcolormesh(x,y,z, cmap='gist_rainbow', linewidth=0, zorder=1)
#plt.set_xlabel('Time [s]')
#plt.set_ylabel('R [m]; ECE')
#plt.view_init(90,-90)
#plt.set_axis_off()
ax.contour(z,zorder=2)
plt.show()
which outputs: http://www.image-upload.net/di/4T7EQ1AJ/Capture.png
another code:
fig,ax = plt.subplots()
#ax.pcolormesh(x,y,z, cmap='gist_rainbow', linewidth=0, zorder=1)
#plt.set_xlabel('Time [s]')
#plt.set_ylabel('R [m]; ECE')
#plt.view_init(90,-90)
#plt.set_axis_off()
print x
print "\n\n"
print y
print "\n\n"
print z
ax.contour(z,zorder=2)
plt.show()
which outputs: http://www.image-upload.net/di/4T7EQ1AJ/Capture.png
and finally, this code
fig,ax = plt.subplots()
ax.pcolormesh(x,y,z, cmap='gist_rainbow', linewidth=0, zorder=1)
#plt.set_xlabel('Time [s]')
#plt.set_ylabel('R [m]; ECE')
#plt.view_init(90,-90)
#plt.set_axis_off()
print x
print "\n\n"
print y
print "\n\n"
print z
#ax.contour(z,zorder=2)
plt.show()
which outputs: http://www.image-upload.net/di/JEKL/Capture.png
What I want is this contour http://www.image-upload.net/di/4T7EQ1AJ/Capture.png on top of http://www.image-upload.net/di/JEKL/Capture.png without the messy white background! :)
Contourf is a filled contour. So it is covering up everything underneath it. There are two options then:
1) You could try setting an alpha in contourf... i.e. alpha=0.25. This will make the filled contour partially see through.
2) Better option: use contour instead of contourf, which will then just plot contour lines.
You just need to use zorder which controls what order artists are rendered (which elements with high zorder 'above' elements with lower zorder)
fig, ax = plt.subplots()
ax.pcolormesh(..., zorder=1)
ax.contour(..., zorder=2)
plt.show()

Categories

Resources