0

I am trying to set the min/max values of a contour but for some reason, this code only modifies the color but not the legend itself. What might I be doing wrong?

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd

x = [0,0.05,0.1]
y = [2,1.75,1.5,1.25,1,0.75,0.5,0.25]

z = [[28.2, 34.4, 27. ],
   [27.3, 32.3, 32. ],
   [27.9, 30.3, 27.8],
   [36.3, 27.3, 28.2],
   [31. , 32.9, 30.6],
   [33.4, 30.7, 28. ],
   [30.9, 32.1, 28.7],
   [29.9, 26.5, 25.1]]

X, Y = np.meshgrid(x, y)
    
plt.figure()                           
plt.contourf(X, Y, z, 100, cmap='viridis')
plt.clim(0,30)
plt.xticks(x) 
plt.yticks(y) 
plt.colorbar();

ax = plt.gca() #you first need to get the axis handle
ax.set_aspect(1/8) #sets the height to width ratio 

This results in the following image where the contour color range has the right limits but the legend is still using the range from the z-array

enter image description here

wiggles8x0
  • 17
  • 6

1 Answers1

0

This turns out to accomplish what I wanted to do

plt.contourf(X, Y, z, levels = np.linspace(0,30,10), cmap='viridis')

I only wish that it would assign the max value color to the value in z which exceeds the upper limit (30 in this case) instead of just leaving that area in the contour blank as seen below

enter image description here

wiggles8x0
  • 17
  • 6