Mohammed Li
Mohammed Li

Reputation: 901

pyplot depicted range of colorbar

I have a problem creating a contourf-plot in python and setting the limits of the colorbar.

I have set them in so far that the color scaling is correct, but only the part that is used is depicted.

My code looks like this:

import numpy as np
import matplotlib.pyplot as plt

data = np.loadtxt("./somefancyasciifile")
xAxisScale = np.linspace(0.0, 80, len(data[0,:]))
yAxisScale = np.linspace(0.0, 80, len(data[0,:]))

fig = plt.figure(figsize = (16, 9), dpi = 100)
ax = fig.add_subplot(111 )
image = ax.contourf(xAxisScale, yAxisScale, data, 51,vmax = 1.0, vmin = 0.0)
colorbar = fig.colorbar(image, ticks = [0.0, 0.5, 1.0])

plt.show()

The default colorscale is correctly adjusted such that deep blue is 0.0 and red is 1.0. My minimum value is something like 0.45 and my maximum value is a little below 1.0. Even though the colors are correctly adjusted as I want them to, the colorbar itself only ranges from my minimum value to my maximum value. However I want the bar to show the entire range from 0.0 to 1.0 with corresponding colors.

Upvotes: 0

Views: 375

Answers (1)

tmdavison
tmdavison

Reputation: 69116

This is because you are setting N = 51, that overrides the vmin and vmax, to just contour 51 levels based on the input array. An alternative would be to set the levels to the whole range from 0 to 1: using the levels keyword:

import numpy as np
import matplotlib.pyplot as plt

data = np.random.rand(10,10)*0.6+0.4 # data ranging from 0.4 to 1

xAxisScale = np.linspace(0.0, 80, len(data[0,:]))
yAxisScale = np.linspace(0.0, 80, len(data[0,:]))

fig = plt.figure(figsize = (16, 9), dpi = 100)
ax = fig.add_subplot(111 )
image = ax.contourf(xAxisScale, yAxisScale, data,levels=np.arange(0,1.01,0.01),vmin = 0.0,vmax=1.0)
colorbar = fig.colorbar(image, ticks = [0.0, 0.5, 1.0])

plt.show()

enter image description here

Upvotes: 1

Related Questions