Related
I use scipy.interpolate.griddata to interpolate my data for contour plot. The data have different scale on x and y axes:
from scipy.interpolate import griddata
xx = [0.0493, 0.0458, 0.0425, 0.0394, 0.0365, 0.0337, 0.0311, 0.0286, 0.0262, 0.024, 0.0219, 0.0198, 0.0179, 0.016, 0.0143, 0.0126, 0.0109, 0.0094, 0.0079, 0.0064, 0.005, 0.0037, 0.0024, 0.0012, 0.0, 0.0663, 0.0637, 0.0613, 0.059, 0.0567, 0.0546, 0.0525, 0.0506, 0.0487, 0.0469, 0.0451, 0.0434, 0.0418, 0.0402, 0.0387, 0.0373, 0.0359, 0.0345, 0.0332, 0.0319, 0.0307, 0.0295, 0.0283, 0.0272, 0.0261, 0.0792, 0.0774, 0.0756, 0.0739, 0.0722, 0.0706, 0.0691, 0.0676, 0.0661, 0.0647, 0.0633, 0.062, 0.0607, 0.0594, 0.0582, 0.057, 0.0559, 0.0547, 0.0536, 0.0526, 0.0515, 0.0505, 0.0495, 0.0486, 0.0477, 0.0919, 0.0905, 0.0891, 0.0878, 0.0865, 0.0852, 0.084, 0.0828, 0.0816, 0.0805, 0.0794, 0.0783, 0.0772, 0.0762, 0.0752, 0.0742, 0.0732, 0.0723, 0.0714, 0.0705, 0.0696, 0.0688, 0.0679, 0.0671, 0.0663, 0.1044, 0.1033, 0.1022, 0.1011, 0.1, 0.099, 0.098, 0.097, 0.096, 0.0951, 0.0942, 0.0933, 0.0924, 0.0915, 0.0907, 0.0898, 0.089, 0.0882, 0.0874, 0.0867, 0.0859, 0.0852, 0.0845, 0.0837, 0.083, 0.1168, 0.1159, 0.1149, 0.114, 0.1132, 0.1123, 0.1114, 0.1106, 0.1098, 0.109, 0.1082, 0.1074, 0.1066, 0.1059, 0.1052, 0.1045, 0.1037, 0.1031, 0.1024, 0.1017, 0.1011, 0.1004, 0.0998, 0.0992, 0.0985, 0.1291, 0.1283, 0.1275, 0.1267, 0.126, 0.1252, 0.1245, 0.1238, 0.123, 0.1223, 0.1217, 0.121, 0.1203, 0.1197, 0.119, 0.1184, 0.1178, 0.1172, 0.1166, 0.116, 0.1154, 0.1148, 0.1143, 0.1137, 0.1132]
yy = [0.6137, 0.8211, 1.0277, 1.2338, 1.4393, 1.6444, 1.8489, 2.053, 2.2567, 2.4601, 2.6631, 2.8658, 3.0682, 3.2703, 3.4722, 3.6738, 3.8752, 4.0763, 4.2773, 4.4781, 4.6787, 4.8791, 5.0794, 5.2795, 5.4795, 0.3217, 0.5059, 0.694, 0.8859, 1.0812, 1.2799, 1.4816, 1.6861, 1.8934, 2.1033, 2.3155, 2.5301, 2.7467, 2.9655, 3.1861, 3.4086, 3.6328, 3.8586, 4.0861, 4.315, 4.5453, 4.777, 5.01, 5.2442, 5.4795, 0.2447, 0.4154, 0.5919, 0.7737, 0.9606, 1.1524, 1.3488, 1.5496, 1.7547, 1.9638, 2.1767, 2.3932, 2.6133, 2.8367, 3.0633, 3.293, 3.5257, 3.7611, 3.9993, 4.2401, 4.4833, 4.729, 4.977, 5.2272, 5.4795, 0.1814, 0.3467, 0.5184, 0.696, 0.8795, 1.0685, 1.2629, 1.4624, 1.6668, 1.876, 2.0897, 2.3079, 2.5302, 2.7567, 2.987, 3.2212, 3.4591, 3.7004, 3.9452, 4.1933, 4.4446, 4.6989, 4.9563, 5.2165, 5.4795, 0.1202, 0.2837, 0.4538, 0.6303, 0.8128, 1.0012, 1.1953, 1.3949, 1.5998, 1.8099, 2.0249, 2.2448, 2.4693, 2.6983, 2.9318, 3.1694, 3.4112, 3.657, 3.9066, 4.16, 4.417, 4.6776, 4.9416, 5.2089, 5.4795, 0.0598, 0.2232, 0.3932, 0.5697, 0.7525, 0.9413, 1.136, 1.3365, 1.5425, 1.754, 1.9706, 2.1924, 2.4191, 2.6506, 2.8867, 3.1275, 3.3726, 3.6221, 3.8757, 4.1334, 4.3951, 4.6606, 4.93, 5.203, 5.4795, 0.0, 0.1638, 0.3344, 0.5115, 0.695, 0.8848, 1.0806, 1.2823, 1.4897, 1.7028, 1.9212, 2.145, 2.3739, 2.6078, 2.8466, 3.0903, 3.3385, 3.5914, 3.8486, 4.1102, 4.376, 4.646, 4.9199, 5.1978, 5.4795]
vv = [0.4829, 0.5196, 0.5541, 0.5866, 0.6173, 0.6463, 0.6738, 0.6998, 0.7246, 0.7481, 0.7706, 0.7919, 0.8123, 0.8318, 0.8504, 0.8683, 0.8854, 0.9017, 0.9175, 0.9326, 0.9471, 0.9611, 0.9745, 0.9875, 1.0, 0.4229, 0.4512, 0.4782, 0.5041, 0.5288, 0.5525, 0.5752, 0.597, 0.618, 0.6381, 0.6575, 0.6761, 0.6941, 0.7114, 0.7282, 0.7443, 0.7599, 0.775, 0.7895, 0.8036, 0.8173, 0.8305, 0.8433, 0.8557, 0.8678, 0.4044, 0.4259, 0.4467, 0.4668, 0.4862, 0.505, 0.5231, 0.5407, 0.5578, 0.5743, 0.5903, 0.6059, 0.621, 0.6356, 0.6498, 0.6637, 0.6771, 0.6902, 0.703, 0.7154, 0.7274, 0.7392, 0.7507, 0.7618, 0.7727, 0.3883, 0.4056, 0.4225, 0.4388, 0.4548, 0.4703, 0.4854, 0.5001, 0.5144, 0.5283, 0.5419, 0.5552, 0.5681, 0.5808, 0.5931, 0.6051, 0.6169, 0.6284, 0.6396, 0.6506, 0.6613, 0.6718, 0.6821, 0.6921, 0.7019, 0.3725, 0.3871, 0.4014, 0.4153, 0.4289, 0.4422, 0.4551, 0.4678, 0.4802, 0.4924, 0.5042, 0.5159, 0.5272, 0.5384, 0.5493, 0.56, 0.5704, 0.5807, 0.5907, 0.6006, 0.6102, 0.6197, 0.629, 0.6381, 0.6471, 0.3569, 0.3697, 0.3821, 0.3943, 0.4063, 0.418, 0.4295, 0.4407, 0.4518, 0.4626, 0.4732, 0.4836, 0.4938, 0.5038, 0.5137, 0.5233, 0.5328, 0.5421, 0.5513, 0.5603, 0.5691, 0.5778, 0.5863, 0.5947, 0.6029, 0.3415, 0.3529, 0.3641, 0.375, 0.3858, 0.3964, 0.4068, 0.417, 0.427, 0.4368, 0.4465, 0.456, 0.4654, 0.4745, 0.4836, 0.4925, 0.5012, 0.5098, 0.5182, 0.5265, 0.5347, 0.5428, 0.5507, 0.5585, 0.5662]
N=500
data_points = (xx, yy)
grid_points = (np.linspace(min(xx), max(xx), N), np.linspace(min(yy), max(yy), N))
vi = griddata(data_points, vv,
(grid_points[0][None, :], grid_points[1][:, None]), method='cubic')
plot(xx,yy, '.k')
contour(grid_points[0], grid_points[1], vi)
But I got ugly sharped contours:
However, if I scale for example the y axis, like this yy = [v/50. for v in yy], I got the smoothed plot:
How to get the smoothed contours with original axes scales?
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
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
I have a data set of points, logR, logT, and logX, where X is a function of R and T. It's only a data set, I have no defined function for X. The data is listed in a table, where logR corresponds to columns and logT corresponds to logT. I am attempting to use an interpolation function to evaluate this grid at two inputs of logR and logT. I found my situation most related to this post:
How to pass arrays into Scipy Interpolate RectBivariateSpline?
But I could not arrange things so my function could be evaluated at my inputs. Here are my attempts:
import numpy as np
from scipy.interpolate import RectBivariateSpline, interp2d
op_r = np.array([1e-8, 3.1622e-8, 1e-7, 3.1622e-7, 1e-6, 3.1622e-6, 1e-5,
3.1622e-5, 1e-4, 3.1622e-4, 1e-3, 3.1622e-3, 1e-2, 3.1622e-2, 0.1, .31622, 1,
3.1622, 10])
op_T = np.array([17782.794, 19952.623, 22387.211, 25118.864, 28183.829,
31622.777, 35481.339, 39810.717, 44668.359, 50118.723, 56234.133, 63095.734,
79432.823, 89125.094])
log_op_val = np.array([[-0.598, -0.593, -0.583, -0.568, -0.539, -0.477,
-0.353, -0.142, 0.168, 0.558, 0.990, 1.443, 1.915, 2.407, 2.866, 3.239, 3.517,
3.725, 3.896], [-0.597, -0.592, -0.580, -0.561, -0.532, -0.474, -0.362,
-0.165, 0.138, 0.539, 1.001, 1.476, 1.942, 2.426, 2.912, 3.352, 3.702, 3.968,
4.175], [-0.588, -0.588, -0.578, -0.555, -0.520, -0.462, -0.357, -0.171,
0.124, 0.529, 1.009, 1.507, 2.001, 2.487, 2.979, 3.453, 3.856, 4.176, 4.422],
[-0.545, -0.559, -0.563, -0.546, -0.506, -0.442, -0.338, -0.159, 0.132, 0.538,
1.015, 1.525, 2.051, 2.565, 3.072, 3.563, 3.996, 4.356, 4.634], [-0.520,
-0.521, -0.519, -0.509, -0.475, -0.409, -0.301, -0.122, 0.167, 0.571, 1.052,
1.570, 2.106, 2.642, 3.176, 3.684, 4.136, 4.517, 4.822], [-0.518, -0.514,
-0.504, -0.478, -0.425, -0.344, -0.232, -0.056, 0.226, 0.629, 1.111, 1.631,
2.169, 2.719, 3.276, 3.804, 4.275, 4.672, 4.990], [-0.517, -0.513, -0.504,
-0.479, -0.417, -0.297, -0.129, 0.074, 0.353, 0.734, 1.202, 1.715, 2.250,
2.800, 3.364, 3.907, 4.394, 4.802, 5.127], [-0.518, -0.514, -0.505, -0.484,
-0.429, -0.311, -0.104, 0.185, 0.521, 0.894, 1.329, 1.818, 2.341, 2.883,
3.441, 3.986, 4.481, 4.894, 5.218], [-0.517, -0.514, -0.507, -0.490, -0.443,
-0.337, -0.142, 0.169, 0.588, 1.039, 1.480, 1.936, 2.431, 2.955, 3.496, 4.031,
4.521, 4.934, 5.253], [-0.516, -0.513, -0.507, -0.492, -0.453, -0.361, -0.184,
0.103, 0.510, 1.009, 1.531, 2.022, 2.502, 3.002, 3.519, 4.035, 4.513, 4.920,
5.235], [-0.515, -0.511, -0.506, -0.493, -0.460, -0.381, -0.225, 0.036, 0.409,
0.877, 1.415, 1.973, 2.502, 3.005, 3.505, 4.002, 4.468, 4.868, 5.183],
[-0.515, -0.511, -0.503, -0.490, -0.462, -0.394, -0.257, -0.022, 0.321, 0.759,
1.269, 1.827, 2.403, 2.949, 3.458, 3.948, 4.405, 4.802, 5.113], [-0.516,
-0.512, -0.502, -0.487, -0.460, -0.400, -0.279, -0.066, 0.254, 0.672, 1.164,
1.701, 2.278, 2.851, 3.388, 3.889, 4.347, 4.741, 5.047], [-0.517, -0.512,
-0.503, -0.485, -0.454, -0.397, -0.287, -0.092, 0.211, 0.620, 1.101, 1.628,
2.190, 2.762, 3.322, 3.841, 4.305, 4.695, 4.989], [-0.516, -0.512, -0.503,
-0.484, -0.449, -0.388, -0.283, -0.099, 0.192, 0.596, 1.071, 1.596, 2.148,
2.714, 3.281, 3.811, 4.280, 4.661, 4.937]])
T_1a = 22100.
R_a = rho_ta /(((T_1a)/(1e6))**3)
gri_chi_a = RectBivariateSpline(op_r, op_T, op_val)
chi_a = RectBivariateSpline(R_a, T_1a)
print chi_a
And this is the error I get:
Traceback (most recent call last):
File "model.py", line 279, in <module>
gri_chi_a = RectBivariateSpline(op_r, op_T, op_val)
"/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/scipy/interpolate/fitpack2.py", line 882, in __init__
raise TypeError('x dimension of z must have same number of '
TypeError: x dimension of z must have same number of elements as x
It is the same as if I use the interp2d function. Any help would be appreciated.
Copy-n-pasting your arrays I get:
In [391]: op_r.shape
Out[391]: (19,)
In [393]: op_T.shape
Out[393]: (14,)
In [395]: log_op_val.shape
Out[395]: (15, 19)
15 does not equal 14 or 19!
I'm trying to predict time-series data, but by offsetting the result by date_offset-timepoints before training and prediction. The reason for doing this is to try and predict date_offset-timepoints into the future with the present data. See http://glowingpython.blogspot.co.za/2015/01/forecasting-beer-consumption-with.html for an example.
So in summary:
data = [1,2,3,4,5] should predict result = [2,3,4,5,6] if date_offset = 1
The results on the plot below show the red line being shifted by date_offset, and not predicting date_offset into the future. No matter how big I make date_offset, it keeps shifting and not predicting the last result I have, i.e. result = 5 (which is already know). In fact, the red line should not shift at all, just loose accuracy the bigger date_offset becomes. What am I doing wrong?
See example code and resulting image below:
from sklearn import linear_model
import matplotlib.pyplot as plt
import numpy as np
date_offset = 1
data = np.array([9330.0, 9470.0, 9550.0, 9620.0, 9600.0, 9585.0, 9600.0, 9600.0, 9430.0, 9460.0, 9450.0, 9650.0, 9620.0, 9650.0, 9500.0, 9400.0, 9165.0, 9100.0, 8755.0, 8850.0, 8990.0, 9150.0, 9195.0, 9175.0, 9250.0, 9200.0, 9350.0, 9280.0, 9370.0, 9470.0, 9445.0, 9440.0, 9280.0, 9325.0, 9170.0, 9270.0, 9200.0, 9450.0, 9510.0, 9371.0, 9499.0, 9499.0, 9400.0, 9500.0, 9550.0, 9670.0, 9700.0, 9760.0, 9767.4599999999991, 9652.0, 9520.0, 9600.0, 9610.0, 9700.0, 9825.0, 9900.0, 9950.0, 9801.0, 9770.0, 9545.0, 9630.0, 9710.0, 9700.0, 9700.0, 9600.0, 9615.0, 9575.0, 9500.0, 9600.0, 9480.0, 9565.0, 9510.0, 9475.0, 9600.0, 9400.0, 9400.0, 9400.0, 9300.0, 9430.0, 9410.0, 9380.0, 9320.0, 9000.0, 9100.0, 9000.0, 9200.0, 9210.0, 9251.0, 9460.0, 9400.0, 9600.0, 9621.0, 9440.0, 9490.0, 9675.0, 9850.0, 9680.0, 10100.0, 9900.0, 10100.0, 9949.0, 10040.0, 10050.0, 10200.0, 10400.0, 10350.0, 10200.0, 10175.0, 10001.0, 10110.0, 10400.0, 10401.0, 10300.0, 10548.0, 10515.0, 10475.0, 10200.0, 10481.0, 10500.0, 10540.0, 10559.0, 10300.0, 10400.0, 10202.0, 10330.0, 10450.0, 10540.0, 10540.0, 10650.0, 10450.0, 10550.0, 10501.0, 10206.0, 10250.0, 10345.0, 10225.0, 10330.0, 10506.0, 11401.0, 11245.0, 11360.0, 11549.0, 11415.0, 11450.0, 11460.0, 11600.0, 11530.0, 11450.0, 11402.0, 11299.0])
data = data[np.newaxis].T
results = np.array([9470.0, 9545.0, 9635.0, 9640.0, 9600.0, 9622.0, 9555.0, 9429.0, 9495.0, 9489.0, 9630.0, 9612.0, 9630.0, 9501.0, 9372.0, 9165.0, 9024.0, 8780.0, 8800.0, 8937.0, 9051.0, 9100.0, 9166.0, 9220.0, 9214.0, 9240.0, 9254.0, 9400.0, 9450.0, 9470.0, 9445.0, 9301.0, 9316.0, 9170.0, 9270.0, 9251.0, 9422.0, 9466.0, 9373.0, 9440.0, 9415.0, 9410.0, 9500.0, 9520.0, 9620.0, 9705.0, 9760.0, 9765.0, 9651.0, 9520.0, 9600.0, 9610.0, 9700.0, 9805.0, 9900.0, 9950.0, 9800.0, 9765.0, 9602.0, 9630.0, 9790.0, 9710.0, 9800.0, 9649.0, 9580.0, 9780.0, 9560.0, 9501.0, 9511.0, 9530.0, 9498.0, 9475.0, 9595.0, 9500.0, 9460.0, 9400.0, 9310.0, 9382.0, 9375.0, 9385.0, 9320.0, 9100.0, 8990.0, 9045.0, 9129.0, 9201.0, 9251.0, 9424.0, 9440.0, 9500.0, 9621.0, 9490.0, 9512.0, 9599.0, 9819.0, 9684.0, 10025.0, 9984.0, 10110.0, 9950.0, 10048.0, 10095.0, 10200.0, 10338.0, 10315.0, 10200.0, 10166.0, 10095.0, 10110.0, 10400.0, 10445.0, 10360.0, 10548.0, 10510.0, 10480.0, 10180.0, 10488.0, 10520.0, 10510.0, 10565.0, 10450.0, 10400.0, 10240.0, 10338.0, 10410.0, 10540.0, 10481.0, 10521.0, 10530.0, 10325.0, 10510.0, 10446.0, 10249.0, 10236.0, 10211.0, 10340.0, 10394.0, 11370.0, 11250.0, 11306.0, 11368.0, 11415.0, 11400.0, 11452.0, 11509.0, 11500.0, 11455.0, 11400.0, 11300.0, 11369.0])
# Date offset to predict next i-days results
data = data[:-date_offset]
results = results[date_offset:]
train_data = data[:-50]
train_results = results[:-50]
test_data = data[-50:]
test_results = results[-50:]
regressor = linear_model.BayesianRidge(normalize=True)
regressor.fit(train_data, train_results)
plt.figure(figsize=(8,6))
plt.plot(regressor.predict(test_data), '--', color='#EB3737', linewidth=2, label='Prediction')
plt.plot(test_results, label='True', color='green', linewidth=2)
plt.legend(loc='best')
plt.show()
First of all, the model is not really bad. For instance, when the real value is 10450, it predict 10350, which is really close. And, obviously, the farther in time the predicted point is, the less accurate its predictions, as the variance is growing and sometimes even bias is also growing. You cannot expect the opposite.
Secondly, it is a linear model, so it cannot be absolutely exact when the predicted variable is not linear by nature.
Thirdly, one have to choose a predicted variable with care. For instance, in this case you might try to predict not the value at time T, but the change in value at time T (i.e. C[T]=V[T]-V[T-1]) or the moving average of the last K values. Here you might (or, on the contrary, might not) find out that you are trying to model the so called "random walk" which is hard to predict exactly by its random nature.
And lastly, you might consider other models, like ARIMA, which are better suited for predicting time series.
Adding back the organize_data step:
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from sklearn import linear_model
def organize_data(to_forecast, window, horizon):
"""
Input:
to_forecast, univariate time series organized as numpy array
window, number of items to use in the forecast window
horizon, horizon of the forecast
Output:
X, a matrix where each row contains a forecast window
y, the target values for each row of X
"""
shape = to_forecast.shape[:-1] + \
(to_forecast.shape[-1] - window + 1, window)
strides = to_forecast.strides + (to_forecast.strides[-1],)
X = np.lib.stride_tricks.as_strided(to_forecast,
shape=shape,
strides=strides)
y = np.array([X[i+horizon][-1] for i in range(len(X)-horizon)])
return X[:-horizon], y
data = np.array([9330.0, 9470.0, 9550.0, 9620.0, 9600.0, 9585.0, 9600.0, 9600.0, 9430.0, 9460.0, 9450.0, 9650.0, 9620.0, 9650.0, 9500.0, 9400.0, 9165.0, 9100.0, 8755.0, 8850.0, 8990.0, 9150.0, 9195.0, 9175.0, 9250.0, 9200.0, 9350.0, 9280.0, 9370.0, 9470.0, 9445.0, 9440.0, 9280.0, 9325.0, 9170.0, 9270.0, 9200.0, 9450.0, 9510.0, 9371.0, 9499.0, 9499.0, 9400.0, 9500.0, 9550.0, 9670.0, 9700.0, 9760.0, 9767.4599999999991, 9652.0, 9520.0, 9600.0, 9610.0, 9700.0, 9825.0, 9900.0, 9950.0, 9801.0, 9770.0, 9545.0, 9630.0, 9710.0, 9700.0, 9700.0, 9600.0, 9615.0, 9575.0, 9500.0, 9600.0, 9480.0, 9565.0, 9510.0, 9475.0, 9600.0, 9400.0, 9400.0, 9400.0, 9300.0, 9430.0, 9410.0, 9380.0, 9320.0, 9000.0, 9100.0, 9000.0, 9200.0, 9210.0, 9251.0, 9460.0, 9400.0, 9600.0, 9621.0, 9440.0, 9490.0, 9675.0, 9850.0, 9680.0, 10100.0, 9900.0, 10100.0, 9949.0, 10040.0, 10050.0, 10200.0, 10400.0, 10350.0, 10200.0, 10175.0, 10001.0, 10110.0, 10400.0, 10401.0, 10300.0, 10548.0, 10515.0, 10475.0, 10200.0, 10481.0, 10500.0, 10540.0, 10559.0, 10300.0, 10400.0, 10202.0, 10330.0, 10450.0, 10540.0, 10540.0, 10650.0, 10450.0, 10550.0, 10501.0, 10206.0, 10250.0, 10345.0, 10225.0, 10330.0, 10506.0, 11401.0, 11245.0, 11360.0, 11549.0, 11415.0, 11450.0, 11460.0, 11600.0, 11530.0, 11450.0, 11402.0, 11299.0])
train_window = 50
k = 5 # number of previous observations to use
h = 2 # forecast horizon
X,y = organize_data(data, k, h)
train_data = X[:train_window]
train_results = y[:train_window]
test_data = X[train_window:]
test_results = y[train_window:]
regressor = linear_model.BayesianRidge(normalize=True)
regressor.fit(train_data, train_results)
plt.figure(figsize=(8,6))
plt.plot(regressor.predict(X), '--', color='#EB3737', linewidth=2, label='Prediction')
plt.plot(y, label='True', color='green', linewidth=2)
plt.legend(loc='best')
plt.show()