Discussion:
[Matplotlib-users] how to reverse the colorbar and its label at the same time?
Chao YUE
2012-11-10 15:07:35 UTC
Permalink
Paul Hobson
2012-11-10 15:41:03 UTC
Permalink
Dear all,
Is there a way to reverse the colorbar label, the default is small value at the bottom and big value at the top, yet I would like the big value at the bottom and small value at the top.
all code in pylab mode.
import numpy as np
import matplotlib as mat
a = np.arange(100).reshape(10,10)
contourf(a,levels=np.arange(0,101,10))
colorbar()
in the above figure, colorbar label shows 0 at the bottom and 100 at the top.
Yet I want the 0 at the top and the 100 at the bottom, with the same sequence of colors in the colorbar.
a = np.arange(100).reshape(10,10)
contourf(a,levels=np.arange(0,101,10),cmap=mat.cm.jet_r)
cbar = colorbar()
cbar.set_ticks(np.arange(0,101,10))
cbar.set_ticklabels(np.arange(100,-1,-10))
Chao,

I think it's as simple as:

import numpy as np
import matplotlib.pyplot as plt

a = np.arange(100).reshape(10,10)
fig, ax1 = plt.subplots()
CS = ax1.contourf(a,levels=np.arange(0,101,10))
cbar = plt.colorbar(CS)
cbar.ax.invert_yaxis()

Does that produce the desired results?
-p
Damon McDougall
2012-11-10 15:51:05 UTC
Permalink
Dear all,
Is there a way to reverse the colorbar label, the default is small value at the bottom and big value at the top, yet I would like the big value at the bottom and small value at the top.
all code in pylab mode.
import numpy as np
import matplotlib as mat
a = np.arange(100).reshape(10,10)
contourf(a,levels=np.arange(0,101,10))
colorbar()
in the above figure, colorbar label shows 0 at the bottom and 100 at the top.
Yet I want the 0 at the top and the 100 at the bottom, with the same sequence of colors in the colorbar.
a = np.arange(100).reshape(10,10)
contourf(a,levels=np.arange(0,101,10),cmap=mat.cm.jet_r)
cbar = colorbar()
cbar.set_ticks(np.arange(0,101,10))
cbar.set_ticklabels(np.arange(100,-1,-10))
Chao,
import numpy as np
import matplotlib.pyplot as plt
a = np.arange(100).reshape(10,10)
fig, ax1 = plt.subplots()
CS = ax1.contourf(a,levels=np.arange(0,101,10))
cbar = plt.colorbar(CS)
cbar.ax.invert_yaxis()
Does that produce the desired results?
-p
Or, you could plot -a instead of a.
--
Damon McDougall
http://www.damon-is-a-geek.com
Institute for Computational Engineering Sciences
201 E. 24th St.
Stop C0200
The University of Texas at Austin
Austin, TX 78712-1229
Chao YUE
2012-11-10 16:37:19 UTC
Permalink
unknown
1970-01-01 00:00:00 UTC
Permalink
--047d7b621c74908be804ce26ae51
Content-Type: text/plain; charset=ISO-8859-1

Thanks, I think cbar.ax.invert_yaxis() is what I am looking for.

Chao

On Sat, Nov 10, 2012 at 4:51 PM, Damon McDougall
Dear all,
Is there a way to reverse the colorbar label, the default is small
value at the bottom and big value at the top, yet I would like the big
value at the bottom and small value at the top.
all code in pylab mode.
import numpy as np
import matplotlib as mat
a = np.arange(100).reshape(10,10)
contourf(a,levels=np.arange(0,101,10))
colorbar()
in the above figure, colorbar label shows 0 at the bottom and 100 at
the top.
Yet I want the 0 at the top and the 100 at the bottom, with the same
sequence of colors in the colorbar.
One way is to reverse the cmap, and then reverse the colorbar labels at
a = np.arange(100).reshape(10,10)
contourf(a,levels=np.arange(0,101,10),cmap=mat.cm.jet_r)
cbar = colorbar()
cbar.set_ticks(np.arange(0,101,10))
cbar.set_ticklabels(np.arange(100,-1,-10))
Chao,
import numpy as np
import matplotlib.pyplot as plt
a = np.arange(100).reshape(10,10)
fig, ax1 = plt.subplots()
CS = ax1.contourf(a,levels=np.arange(0,101,10))
cbar = plt.colorbar(CS)
cbar.ax.invert_yaxis()
Does that produce the desired results?
-p
Or, you could plot -a instead of a.
--
Damon McDougall
http://www.damon-is-a-geek.com
Institute for Computational Engineering Sciences
201 E. 24th St.
Stop C0200
The University of Texas at Austin
Austin, TX 78712-1229
--
***********************************************************************************
Chao YUE
Laboratoire des Sciences du Climat et de l'Environnement (LSCE-IPSL)
UMR 1572 CEA-CNRS-UVSQ
Batiment 712 - Pe 119
91191 GIF Sur YVETTE Cedex
Tel: (33) 01 69 08 29 02; Fax:01.69.08.77.16
************************************************************************************

--047d7b621c74908be804ce26ae51
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

Thanks, I think cbar.ax.invert_yaxis() is what I am looking for. <br><br>Chao<br><br><div class="gmail_quote">On Sat, Nov 10, 2012 at 4:51 PM, Damon McDougall <span dir="ltr">&lt;<a href="mailto:***@gmail.com" target="_blank">***@gmail.com</a>&gt;</span> wrote:<br> <blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="HOEnZb"><div class="h5">On Sat, Nov 10, 2012 at 9:41 AM, Paul Hobson &lt;<a href="mailto:***@gmail.com">***@gmail.com</a>&gt; wrote:<br>

&gt; On Sat, Nov 10, 2012 at 7:07 AM, Chao YUE &lt;<a href="mailto:***@gmail.com">***@gmail.com</a>&gt; wrote:<br>
&gt;&gt;<br>
&gt;&gt; Dear all,<br>
&gt;&gt;<br>
&gt;&gt; Is there a way to reverse the colorbar label, the default is small value at the bottom and big value at the top, yet I would like the big value at the bottom and small value at the top.<br>
&gt;&gt;<br>
&gt;&gt; all code in pylab mode.<br>
&gt;&gt;<br>
&gt;&gt; import numpy as np<br>
&gt;&gt; import matplotlib as mat<br>
&gt;&gt;<br>
&gt;&gt; a = np.arange(100).reshape(10,10)<br>
&gt;&gt; contourf(a,levels=np.arange(0,101,10))<br>
&gt;&gt; colorbar()<br>
&gt;&gt;<br>
&gt;&gt; in the above figure, colorbar label shows 0 at the bottom and 100 at the top.<br>
&gt;&gt; Yet I want the 0 at the top and the 100 at the bottom, with the same sequence of colors in the colorbar.<br>
&gt;&gt;<br>
&gt;&gt; One way is to reverse the cmap, and then reverse the colorbar labels at the same time:<br>
&gt;&gt; a = np.arange(100).reshape(10,10)<br>
&gt;&gt; contourf(a,levels=np.arange(0,101,10),cmap=mat.cm.jet_r)<br>
&gt;&gt; cbar = colorbar()<br>
&gt;&gt; cbar.set_ticks(np.arange(0,101,10))<br>
&gt;&gt; cbar.set_ticklabels(np.arange(100,-1,-10))<br>
&gt;<br>
&gt; Chao,<br>
&gt;<br>
&gt; I think it&#39;s as simple as:<br>
&gt;<br>
&gt; import numpy as np<br>
&gt; import matplotlib.pyplot as plt<br>
&gt;<br>
&gt; a = np.arange(100).reshape(10,10)<br>
&gt; fig, ax1 = plt.subplots()<br>
&gt; CS = ax1.contourf(a,levels=np.arange(0,101,10))<br>
&gt; cbar = plt.colorbar(CS)<br>
&gt; cbar.ax.invert_yaxis()<br>
&gt;<br>
&gt; Does that produce the desired results?<br>
&gt; -p<br>
<br>
</div></div>Or, you could plot -a instead of a.<br>
<span class="HOEnZb"><font color="#888888"><br>
--<br>
Damon McDougall<br>
<a href="http://www.damon-is-a-geek.com" target="_blank">http://www.damon-is-a-geek.com</a><br>
Institute for Computational Engineering Sciences<br>
201 E. 24th St.<br>
Stop C0200<br>
The University of Texas at Austin<br>
Austin, TX <a href="tel:78712-1229" value="+48787121229">78712-1229</a><br>
</font></span></blockquote></div><br><br clear="all"><br>-- <br><div>***********************************************************************************</div>
<div>Chao YUE<br>Laboratoire des Sciences du Climat et de l&#39;Environnement (LSCE-IPSL)<br>UMR 1572 CEA-CNRS-UVSQ<br>Batiment 712 - Pe 119<br>91191 GIF Sur YVETTE Cedex</div>
<div>Tel: (33) 01 69 08 29 02; Fax:01.69.08.77.16<br></div>

<div>************************************************************************************</div><br>

--047d7b621c74908be804ce26ae51--
Loading...