Colorbar and contour plot seem to not match data; individual/custom scale to emphasize data of interest

12 views Asked by At

I am trying to create a contour plot that shows the turbulence intensity of a measurement plane. However, towards the bottom the values are very high (about 45%) while the values are about 2-10% in the majority of the plane. Due to the scale of the colourbar, it is impossible to see any variation in the range of interest (2-10%). Example Matlab

I want the colourbar to go from 2 to 5% with a large colour gradient and the gradient should decrease quite rapidly, so that the colour change between 30-45% is very small.

I have tried adding custom normalization to have an individual scale for the colorbar. However, it does not seem to match the contour plot colours. I.e. it appears that the value is closer to 10-15% than 2-10%. Based on the data, the colour should be mostly dark purple.

Contour bar and Contour Plot

I have added the ContourMatrix to the end of the question. Interested in the far right column which contains turbulence intensity.

ContourMatrix = np.loadtxt(data_str.splitlines())
print(ContourMatrix)
# Assuming ContourMatrix is already created in Python
x = ContourMatrix[:, 0]
y = ContourMatrix[:, 1]
values_ti = ContourMatrix[:, 4]  # Adjusted variable name

# Define grid for interpolation
X, Y = np.meshgrid(np.linspace(min(x), max(x), 100), np.linspace(0.4, -0.4, 100))  # Adjusted limits for Y-axis
Z = griddata((x, y), values_ti, (X, Y), method='linear')


# Custom normalization
class MidpointLogNormalize(Normalize):
    def __init__(self, vmin=None, vmax=None, midpoint=None, clip=False):
        self.midpoint = midpoint
        Normalize.__init__(self, vmin, vmax, clip)

    def __call__(self, value, clip=None):
        # Use np.where to perform array-wise comparison
        return np.where(value <= self.midpoint,
                        np.log10(1 + value) / np.log10(1 + self.midpoint) * 0.5,
                        (value - self.midpoint) / (self.vmax - self.midpoint) * 0.5 + 0.5)


# Create filled contour plot
plt.contourf(X, Y, Z, 100, cmap='plasma', alpha=1, norm=MidpointLogNormalize(vmin=2, vmax=45, midpoint=4))

# Add colorbar with custom ticks and labels
cbar = plt.colorbar(ScalarMappable(norm=MidpointLogNormalize(vmin=2, vmax=45, midpoint=4), cmap='plasma'))
cbar_ticks = [2, 5, 10, 20, 30, 40, 45]
cbar.set_ticks(cbar_ticks)
cbar.set_ticklabels([str(tick) for tick in cbar_ticks])

# Add markers
plt.scatter(x, y, s=1, c='k', marker='+', alpha=1.0)

# Plot circle
radius = 0.3
circle = Circle((0, 0), radius, edgecolor='k', linewidth=1, fill=False)
plt.gca().add_patch(circle)

# Plot dot at the center of the circle
plt.plot(0, 0, 'ko', markersize=4)

Data for ContourMatrix: data_str = """ -0.4 0.4 0.630885915 -1.471062283 3.0963 -0.3 0.4 0.632192906 -1.266942184 2.992 -0.2 0.4 0.637319087 -0.466358223 2.8618 -0.1 0.4 0.633302477 -1.093654463 2.8855 0 0.4 0.635165409 -0.802709989 2.9681 0.1 0.4 0.634795151 -0.860535194 2.9951 0.2 0.4 0.636487265 -0.596268496 2.8705 0.3 0.4 0.627690599 -1.970092336 3.1598 0.4 0.4 0.623768592 -2.582613947 3.1787 -0.4 0.35 0.629567053 -1.677036206 3.224 -0.3 0.35 0.634046212 -0.977501226 3.1507 -0.2 0.35 0.635387453 -0.768032245 2.999 -0.1 0.35 0.626372803 -2.17589993 3.1514 0 0.35 0.632440342 -1.228298777 3.1271 0.1 0.35 0.627662835 -1.974428378 3.304 0.2 0.35 0.634352021 -0.929741268 2.9069 0.3 0.35 0.626872068 -2.097926885 3.2108 0.4 0.35 0.628126802 -1.901968169 3.287 -0.4 0.3 0.63704614 -0.508985896 3.071 -0.3 0.3 0.637584642 -0.424885031 3.0268 -0.2 0.3 0.63705201 -0.508069211 2.9359 -0.1 0.3 0.634751724 -0.867317492 3.0556 0 0.3 0.635102241 -0.812575265 3.0564 0.1 0.3 0.635030214 -0.823824107 3.125 0.2 0.3 0.631624304 -1.355743927 3.1241 0.3 0.3 0.63144034 -1.384474593 3.1036 0.4 0.3 0.633676606 -1.035224549 2.9999 -0.4 0.25 0.642653445 0.366737869 3.0636 -0.3 0.25 0.638091526 -0.345722157 3.2556 -0.2 0.25 0.634442654 -0.915586637 3.2961 -0.1 0.25 0.63651472 -0.591980644 3.0586 0 0.25 0.634593256 -0.892066336 3.2577 0.1 0.25 0.630942017 -1.46230042 3.3458 0.2 0.25 0.637138037 -0.494633845 3.0924 0.3 0.25 0.633445041 -1.071389479 3.1995 0.4 0.25 0.637793273 -0.392302056 3.0744 -0.4 0.2 0.641006945 0.109594874 3.6334 -0.3 0.2 0.638107924 -0.343161232 3.209 -0.2 0.2 0.640731073 0.066510523 3.0375 -0.1 0.2 0.639403357 -0.140846357 3.1376 0 0.2 0.635579333 -0.738065225 3.073 0.1 0.2 0.635120948 -0.809653699 3.2691 0.2 0.2 0.636402262 -0.609543813 3.2768 0.3 0.2 0.633995949 -0.98535112 3.1529 0.4 0.2 0.641001197 0.10869714 3.1434 -0.4 0.15 0.643655899 0.523296721 3.2212 -0.3 0.15 0.639846469 -0.071642991 3.1498 -0.2 0.15 0.642219509 0.298967685 2.8616 -0.1 0.15 0.638379964 -0.300675205 2.97 0 0.15 0.634018808 -0.981781085 2.9237 0.1 0.15 0.634784929 -0.862131724 3.1708 0.2 0.15 0.639326577 -0.152837499 3.0186 0.3 0.15 0.638843954 -0.228211397 3.1993 0.4 0.15 0.642762827 0.383820648 3.2515 -0.4 0.1 0.645503745 0.811885063 3.2578 -0.3 0.1 0.641111485 0.125921448 3.2283 -0.2 0.1 0.640866267 0.087624488 3.1917 -0.1 0.1 0.638843474 -0.228286284 3.1175 0 0.1 0.637066145 -0.505861526 3.0232 0.1 0.1 0.636763758 -0.553087061 3.2674 0.2 0.1 0.638299075 -0.313308136 3.1311 0.3 0.1 0.640458313 0.023911952 3.3096 0.4 0.1 0.646422105 0.955310404 3.1992 -0.4 0.05 0.648097041 1.216894312 3.149 -0.3 0.05 0.642854056 0.39806841 3.1531 -0.2 0.05 0.64008754 -0.033993603 3.1353 -0.1 0.05 0.640298822 -0.000996523 3.1371 0 0.05 0.637865228 -0.381064411 3.0532 0.1 0.05 0.636527439 -0.589994266 3.2595 0.2 0.05 0.642321423 0.314884234 3.0926 0.3 0.05 0.641557652 0.195601813 3.3811 0.4 0.05 0.64632485 0.940121506 3.2858 -0.4 0 0.648701893 1.31135742 3.4873 -0.3 0 0.645172667 0.760178748 3.1737 -0.2 0 0.646496325 0.966901726 3.2042 -0.1 0 0.642805802 0.390532394 3.7 0 0 0.640305203 0 3.5371 0.1 0 0.641828781 0.23794554 3.1187 0.2 0 0.644035831 0.58263274 3.2823 0.3 0 0.644523637 0.658816118 3.437 0.4 0 0.649479391 1.432783526 3.2857 -0.4 -0.025 0.651434721 1.738158314 2.8956 -0.3 -0.025 0.645531302 0.816188792 2.9571 -0.2 -0.025 0.64465278 0.678985142 2.8816 -0.1 -0.025 0.643467255 0.493835097 3.1362 0 -0.025 0.642717614 0.376759466 2.9246 0.1 -0.025 0.645599724 0.82687462 3.3345 0.2 -0.025 0.644311903 0.625748376 3.0815 0.3 -0.025 0.646510502 0.969115762 3.3188 0.4 -0.025 0.645712188 0.844438674 3.103 -0.4 -0.05 0.652890176 1.965464694 2.8384 -0.3 -0.05 0.645166278 0.759180867 2.9127 -0.2 -0.05 0.64509292 0.747724199 2.9008 -0.1 -0.05 0.641216363 0.142300897 3.1307 0 -0.05 0.642555096 0.351378201 2.9185 0.1 -0.05 0.642409123 0.328580848 3.288 0.2 -0.05 0.646020485 0.892587135 3.0798 0.3 -0.05 0.647227201 1.081046675 3.2599 0.4 -0.05 0.646681766 0.995863059 3.0768 -0.4 -0.075 0.653491214 2.05933219 2.9673 -0.3 -0.075 0.645853687 0.866537326 2.9752 -0.2 -0.075 0.644676352 0.682666447 2.9541 -0.1 -0.075 0.642392409 0.325970448 3.2064 0 -0.075 0.64103929 0.114646392 3.0371 0.1 -0.075 0.642892503 0.404072852 3.4156 0.2 -0.075 0.645344222 0.786971454 3.1753 0.3 -0.075 0.647349544 1.100153623 3.2898 0.4 -0.075 0.646217701 0.923387377 3.1919 -0.4 -0.1 0.65109581 1.685228708 2.7311 -0.3 -0.1 0.643833728 0.551069266 2.532 -0.2 -0.1 0.642516969 0.345423624 2.4883 -0.1 -0.1 0.640527986 0.034793283 2.5604 0 -0.1 0.642617612 0.361141739 2.4991 0.1 -0.1 0.642064934 0.274826867 3.0925 0.2 -0.1 0.641717351 0.220542944 2.7287 0.3 -0.1 0.644166175 0.602989301 2.7916 0.4 -0.1 0.644805156 0.702782503 2.8243 -0.4 -0.125 0.651617997 1.766781432 2.9317 -0.3 -0.125 0.645298265 0.779794123 2.6988 -0.2 -0.125 0.642863927 0.399610016 2.5821 -0.1 -0.125 0.639138105 -0.182272123 2.6409 0 -0.125 0.64295935 0.41451276 2.6595 0.1 -0.125 0.642381621 0.324285662 3.092 0.2 -0.125 0.643560726 0.508432914 2.8325 0.3 -0.125 0.646791034 1.01292804 2.9457 0.4 -0.125 0.645864374 0.868206414 2.923 -0.4 -0.15 0.654270406 2.181022807 3.6171 -0.3 -0.15 0.64799804 1.20143282 3.5756 -0.2 -0.15 0.646509956 0.969030501 3.4272 -0.1 -0.15 0.643243579 0.458902317 3.5406 0 -0.15 0.65281968 1.954454979 3.3512 0.1 -0.15 0.640140435 -0.0257328 3.6819 0.2 -0.15 0.645085667 0.746591382 3.6905 0.3 -0.15 0.650328773 1.565436276 3.6901 0.4 -0.15 0.649195316 1.388418008 3.5596 -0.4 -0.175 0.655300121 2.341839117 4.2073 -0.3 -0.175 0.647200563 1.076886467 4.3684 -0.2 -0.175 0.64477966 0.698800651 4.0358 -0.1 -0.175 0.642188306 0.294094602 4.2177 0 -0.175 0.653991563 2.137474371 4.0795 0.1 -0.175 0.644210515 0.60991412 4.3667 0.2 -0.175 0.646580249 0.980008584 4.3606 0.3 -0.175 0.647871382 1.18165192 4.2068 0.4 -0.175 0.648227373 1.237249048 3.9966 -0.4 -0.2 0.652093788 1.841088438 4.9194 -0.3 -0.2 0.645421888 0.799100984 5.1412 -0.2 -0.2 0.642443335 0.333923846 4.6046 -0.1 -0.2 0.638571367 -0.270782761 4.8022 0 -0.2 0.648910418 1.343923923 4.845 0.1 -0.2 0.640028458 -0.043220745 5.0867 0.2 -0.2 0.644494523 0.654269212 4.2685 0.3 -0.2 0.646647546 0.990518756 4.5856 0.4 -0.2 0.648012554 1.203699525 4.2723 -0.4 -0.22 0.648346691 1.255883598 7.81 -0.3 -0.22 0.638923992 -0.215711338 7.8466 -0.2 -0.22 0.639116917 -0.185581234 6.9051 -0.1 -0.22 0.634005844 -0.983805752 7.0971 0 -0.22 0.645566299 0.821654457 6.7087 0.1 -0.22 0.642833067 0.394790427 6.7006 0.2 -0.22 0.642137584 0.286173026 5.7116 0.3 -0.22 0.642468238 0.337813057 6.7719 0.4 -0.22 0.649589404 1.44996499 5.8421 -0.4 -0.25 0.643171696 0.447676043 11.2665 -0.3 -0.25 0.624205236 -2.51442075 12.1483 -0.2 -0.25 0.6346287 -0.886530912 10.6704 -0.1 -0.25 0.626109511 -2.217019641 11.2351 0 -0.25 0.625954062 -2.241297039 11.4022 0.1 -0.25 0.629556014 -1.678760176 11.4139 0.2 -0.25 0.626682077 -2.127598832 10.8941 0.3 -0.25 0.634274094 -0.941911648 10.7257 0.4 -0.25 0.651700565 1.779676548 8.5993 -0.4 -0.275 0.602505108 -5.903449698 18.5405 -0.3 -0.275 0.589242852 -7.974689365 18.0186 -0.2 -0.275 0.608347763 -4.990969881 14.8303 -0.1 -0.275 0.59564995 -6.974057565 16.8206 0 -0.275 0.598556557 -6.520116609 16.9782 0.1 -0.275 0.606933271 -5.21187898 15.7363 0.2 -0.275 0.606268046 -5.315770747 15.1497 0.3 -0.275 0.60715145 -5.17780478 15.557 0.4 -0.275 0.619611158 -3.231903354 15.4479 -0.4 -0.325 0.44772037 -30.07703703 32.1154 -0.3 -0.325 0.491538187 -23.23376654 25.8088 -0.2 -0.325 0.515934883 -19.4235998 26.5352 -0.1 -0.325 0.513768064 -19.76200391 25.5117 0 -0.325 0.520923708 -18.64446749 24.2903 0.1 -0.325 0.512633792 -19.93914933 25.3012 0.2 -0.325 0.506195131 -20.94471071 25.2274 0.3 -0.325 0.5064044 -20.91202801 25.1006 0.4 -0.325 0.456628767 -28.6857635 32.6067 -0.4 -0.35 0.373249601 -41.70754835 40.21 -0.3 -0.35 0.428915854 -33.01384217 31.4222 -0.2 -0.35 0.445401768 -30.43914593 31.1645 -0.1 -0.35 0.470258689 -26.55710335 28.733 0 -0.35 0.45752468 -28.54584386 28.7522 0.1 -0.35 0.438996425 -31.43950372 30.345 0.2 -0.35 0.444158804 -30.63326649 29.8911 0.3 -0.35 0.452448615 -29.33860094 28.9558 0.4 -0.35 0.386020555 -39.7130379 40.0669 -0.4 -0.385 0.351766549 -45.06267518 46.3737 -0.3 -0.385 0.35470431 -44.60386882 41.5641 -0.2 -0.385 0.367019392 -42.68055448 41.8978 -0.1 -0.385 0.400259234 -37.48930485 37.3694 0 -0.385 0.392168629 -38.7528592 37.8883 0.1 -0.385 0.366377483 -42.7808049 39.9096 0.2 -0.385 0.376851806 -41.14497204 37.3136 0.3 -0.385 0.390408584 -39.02773515 36.1592 0.4 -0.385 0.377445037 -41.05232395 43.9629

"""

0

There are 0 answers