Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python/Matplotlib - Colorbar Range and Display Values

When using matplotlib with a contour plot, I'm having trouble getting the colorbar to display as I want. I've read through numerous similar examples, but have still not been able to get what I want.

In the image below, I want two things changed. I want the minimum value and maximum values to be display on the color bar (the max should be 2.0 and the min -0.1). These two values should be at the very edge of the colorbar. Also, I want the colorbar to display the value at every color transition. For example. in the plot below, between 2.1 and 1.8, there is another color transition where the value isn't displayed.

Can anyone please help me? I think I may need to use norm, but it hasn't worked for me so far.

Thanks,

enter image description here

Code:

import numpy as np import matplotlib.pyplot as plt  xi = np.array([0., 0.5, 1.0]) yi = np.array([0., 0.5, 1.0]) zi = np.array([[0., 1.0, 2.0],                [0., 1.0, 2.0],                [-0.1, 1.0, 2.0]])  plt.contour(xi, yi, zi, 15, linewidths=0.5, colors='k') plt.contourf(xi, yi, zi, 15, cmap=plt.cm.jet) plt.colorbar() plt.show() 
like image 725
Scott B Avatar asked Apr 29 '11 00:04

Scott B


People also ask

How do I set the range in Colorbar?

Use the vmin and vmax Parameter to Set the Range of Colorbar in Python. The vmin and vmax parameters can be used to specify the scale for mapping color values. These parameters work with the object, which uses colormaps. It can be used to control the range of the colorbar in matplotlib.

How do you normalize a Colorbar in Python?

norm = normi; #mpl. colors. Normalize(vmin=-80, vmax=20); plt. axis([1, 1000, -400, 400]);


1 Answers

If I understand correctly what you want, I think this should do it:

import numpy as np import matplotlib.pyplot as plt  xi = np.array([0., 0.5, 1.0]) yi = np.array([0., 0.5, 1.0]) zi = np.array([[0., 1.0, 2.0],                [0., 1.0, 2.0],                [-0.1, 1.0, 2.0]])  v = np.linspace(-.1, 2.0, 15, endpoint=True) plt.contour(xi, yi, zi, v, linewidths=0.5, colors='k') plt.contourf(xi, yi, zi, v, cmap=plt.cm.jet) x = plt.colorbar(ticks=v) print x plt.show() 

enter image description here

like image 93
tom10 Avatar answered Sep 19 '22 12:09

tom10