Discussion:
[Matplotlib-users] Color Bar Limits
John Leeman
2015-04-02 12:47:33 UTC
Permalink
Hi all,

I’m plotting some scatter points colored by a third variable, but want to use a limited subset of a colormap. In the example below, the color axis data ranges from 0-4, but I want to not use the red portion of the bar. Doing the first part is just accomplished by setting the vmin/vmax. But when I plot a color bar I don’t want to show the colors and values for anything below zero. Other than just white-boxing that part of the bar I’m not sure how to do it. I tried a suggestion of setting the limit properties of the bar axis attribute, but that results in the bar getting shrunk and shifted (a very weird behavior). Any ideas?

Thank you,

John Leeman

import numpy as np
import matplotlib.pyplot as plt

x = np.arange(100)
y = np.random.rand(100)
z = 4 * np.random.rand(100)

color_map = plt.get_cmap('rainbow_r')

fig = plt.figure(figsize=(12,9))
ax1 = plt.subplot(111)
sc = ax1.scatter(x, y, c=z, s=50, cmap=color_map, vmin=-1, vmax=4)

position=fig.add_axes([0.37, 0.16, 0.5, 0.02])
cb = fig.colorbar(sc, cax=position, orientation='horizontal', drawedges=False)
cb.set_label('Z-Colors’, fontsize=14)

# I tried this after talking with Ben Root, but it
# results in some odd behavior
# cb.ax.set_xlim(0,4)

plt.show()
Jody Klymak
2015-04-02 16:32:42 UTC
Permalink
Hi John,

I got this off stack exchange, apologies to the original contributor...

Cheers, Jody


import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import LinearSegmentedColormap

x = np.arange(100)
y = np.random.rand(100)
z = 4 * np.random.rand(100)

cmap = plt.get_cmap('rainbow_r’)
start=0.2
stop = 1.
colors = cmap(np.linspace(start, stop, cmap.N))
# Create a new colormap from those colors
color_map = LinearSegmentedColormap.from_list('Upper Half', colors)

fig = plt.figure(figsize=(12,9))
ax1 = plt.subplot(111)
sc = ax1.scatter(x, y, c=z, s=50, cmap=color_map, vmin=0, vmax=4)

position=fig.add_axes([0.37, 0.16, 0.5, 0.02])
cb = fig.colorbar(sc, cax=position, orientation='horizontal', drawedges=False)
cb.set_label('Z-Colors', fontsize=14)

# I tried this after talking with Ben Root, but it
# results in some odd behavior
# cb.ax.set_xlim(0,4)

plt.show()
Post by John Leeman
Hi all,
I’m plotting some scatter points colored by a third variable, but want to use a limited subset of a colormap. In the example below, the color axis data ranges from 0-4, but I want to not use the red portion of the bar. Doing the first part is just accomplished by setting the vmin/vmax. But when I plot a color bar I don’t want to show the colors and values for anything below zero. Other than just white-boxing that part of the bar I’m not sure how to do it. I tried a suggestion of setting the limit properties of the bar axis attribute, but that results in the bar getting shrunk and shifted (a very weird behavior). Any ideas?
Thank you,
John Leeman
import numpy as np
import matplotlib.pyplot as plt
x = np.arange(100)
y = np.random.rand(100)
z = 4 * np.random.rand(100)
color_map = plt.get_cmap('rainbow_r')
fig = plt.figure(figsize=(12,9))
ax1 = plt.subplot(111)
sc = ax1.scatter(x, y, c=z, s=50, cmap=color_map, vmin=-1, vmax=4)
position=fig.add_axes([0.37, 0.16, 0.5, 0.02])
cb = fig.colorbar(sc, cax=position, orientation='horizontal', drawedges=False)
cb.set_label('Z-Colors’, fontsize=14)
# I tried this after talking with Ben Root, but it
# results in some odd behavior
# cb.ax.set_xlim(0,4)
plt.show()
<Color_Bar.png>
------------------------------------------------------------------------------
Dive into the World of Parallel Programming The Go Parallel Website, sponsored
by Intel and developed in partnership with Slashdot Media, is your hub for all
things parallel software development, from weekly thought leadership blogs to
news, videos, case studies, tutorials and more. Take a look and join the
conversation now. http://goparallel.sourceforge.net/_______________________________________________
Matplotlib-users mailing list
https://lists.sourceforge.net/lists/listinfo/matplotlib-users
--
Jody Klymak
http://web.uvic.ca/~jklymak/
Benjamin Root
2015-04-02 16:50:53 UTC
Permalink
No, that's not what he is asking for. John wants the norm to go from -1 to
4, but he wants the colorbar to display only the 0 to 4 portion. Your
approach (setting vmin=0) would change the normalization and change the
colors.

The axes limits do not appear to be scaled by the values. They are set to
(0, 1). So, the kludgy way would seem to be to set the xlimits to be (0.2,
1) (taking out a fifth of the colorbar, but the frame is still there...

Ben Root
Post by Jody Klymak
Hi John,
I got this off stack exchange, apologies to the original contributor...
Cheers, Jody
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import LinearSegmentedColormap
x = np.arange(100)
y = np.random.rand(100)
z = 4 * np.random.rand(100)
cmap = plt.get_cmap('rainbow_r’)
start=0.2
stop = 1.
colors = cmap(np.linspace(start, stop, cmap.N))
# Create a new colormap from those colors
color_map = LinearSegmentedColormap.from_list('Upper Half', colors)
fig = plt.figure(figsize=(12,9))
ax1 = plt.subplot(111)
sc = ax1.scatter(x, y, c=z, s=50, cmap=color_map, vmin=0, vmax=4)
position=fig.add_axes([0.37, 0.16, 0.5, 0.02])
cb = fig.colorbar(sc, cax=position, orientation='horizontal',
drawedges=False)
cb.set_label('Z-Colors', fontsize=14)
# I tried this after talking with Ben Root, but it
# results in some odd behavior
# cb.ax.set_xlim(0,4)
plt.show()
Hi all,
I’m plotting some scatter points colored by a third variable, but want to
use a limited subset of a colormap. In the example below, the color axis
data ranges from 0-4, but I want to not use the red portion of the bar.
Doing the first part is just accomplished by setting the vmin/vmax. But
when I plot a color bar I don’t want to show the colors and values for
anything below zero. Other than just white-boxing that part of the bar I’m
not sure how to do it. I tried a suggestion of setting the limit properties
of the bar axis attribute, but that results in the bar getting shrunk and
shifted (a very weird behavior). Any ideas?
Thank you,
John Leeman
import numpy as np
import matplotlib.pyplot as plt
x = np.arange(100)
y = np.random.rand(100)
z = 4 * np.random.rand(100)
color_map = plt.get_cmap('rainbow_r')
fig = plt.figure(figsize=(12,9))
ax1 = plt.subplot(111)
sc = ax1.scatter(x, y, c=z, s=50, cmap=color_map, vmin=-1, vmax=4)
position=fig.add_axes([0.37, 0.16, 0.5, 0.02])
cb = fig.colorbar(sc, cax=position, orientation='horizontal',
drawedges=False)
cb.set_label('Z-Colors’, fontsize=14)
# I tried this after talking with Ben Root, but it
# results in some odd behavior
# cb.ax.set_xlim(0,4)
plt.show()
<Color_Bar.png>
------------------------------------------------------------------------------
Dive into the World of Parallel Programming The Go Parallel Website, sponsored
by Intel and developed in partnership with Slashdot Media, is your hub for all
things parallel software development, from weekly thought leadership blogs to
news, videos, case studies, tutorials and more. Take a look and join the
conversation now.
http://goparallel.sourceforge.net/_______________________________________________
Matplotlib-users mailing list
https://lists.sourceforge.net/lists/listinfo/matplotlib-users
--
Jody Klymak
http://web.uvic.ca/~jklymak/
------------------------------------------------------------------------------
Dive into the World of Parallel Programming The Go Parallel Website, sponsored
by Intel and developed in partnership with Slashdot Media, is your hub for all
things parallel software development, from weekly thought leadership blogs to
news, videos, case studies, tutorials and more. Take a look and join the
conversation now. http://goparallel.sourceforge.net/
_______________________________________________
Matplotlib-users mailing list
https://lists.sourceforge.net/lists/listinfo/matplotlib-users
Jody Klymak
2015-04-02 16:56:17 UTC
Permalink
No, that's not what he is asking for. John wants the norm to go from -1 to 4, but he wants the colorbar to display only the 0 to 4 portion. Your approach (setting vmin=0) would change the normalization and change the colors.
Hmm, well his values go from 0 to 4, and he wants his colorbar to go from 0 to 4, but just over the last 4/5ths of the colormap. I think I gave him what he wants. But I guess he can decide!

Cheers, Jody
The axes limits do not appear to be scaled by the values. They are set to (0, 1). So, the kludgy way would seem to be to set the xlimits to be (0.2, 1) (taking out a fifth of the colorbar, but the frame is still there...
Ben Root
Hi John,
I got this off stack exchange, apologies to the original contributor...
Cheers, Jody
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import LinearSegmentedColormap
x = np.arange(100)
y = np.random.rand(100)
z = 4 * np.random.rand(100)
cmap = plt.get_cmap('rainbow_r’)
start=0.2
stop = 1.
colors = cmap(np.linspace(start, stop, cmap.N))
# Create a new colormap from those colors
color_map = LinearSegmentedColormap.from_list('Upper Half', colors)
fig = plt.figure(figsize=(12,9))
ax1 = plt.subplot(111)
sc = ax1.scatter(x, y, c=z, s=50, cmap=color_map, vmin=0, vmax=4)
position=fig.add_axes([0.37, 0.16, 0.5, 0.02])
cb = fig.colorbar(sc, cax=position, orientation='horizontal', drawedges=False)
cb.set_label('Z-Colors', fontsize=14)
# I tried this after talking with Ben Root, but it
# results in some odd behavior
# cb.ax.set_xlim(0,4)
plt.show()
Post by John Leeman
Hi all,
I’m plotting some scatter points colored by a third variable, but want to use a limited subset of a colormap. In the example below, the color axis data ranges from 0-4, but I want to not use the red portion of the bar. Doing the first part is just accomplished by setting the vmin/vmax. But when I plot a color bar I don’t want to show the colors and values for anything below zero. Other than just white-boxing that part of the bar I’m not sure how to do it. I tried a suggestion of setting the limit properties of the bar axis attribute, but that results in the bar getting shrunk and shifted (a very weird behavior). Any ideas?
Thank you,
John Leeman
import numpy as np
import matplotlib.pyplot as plt
x = np.arange(100)
y = np.random.rand(100)
z = 4 * np.random.rand(100)
color_map = plt.get_cmap('rainbow_r')
fig = plt.figure(figsize=(12,9))
ax1 = plt.subplot(111)
sc = ax1.scatter(x, y, c=z, s=50, cmap=color_map, vmin=-1, vmax=4)
position=fig.add_axes([0.37, 0.16, 0.5, 0.02])
cb = fig.colorbar(sc, cax=position, orientation='horizontal', drawedges=False)
cb.set_label('Z-Colors’, fontsize=14)
# I tried this after talking with Ben Root, but it
# results in some odd behavior
# cb.ax.set_xlim(0,4)
plt.show()
<Color_Bar.png>
------------------------------------------------------------------------------
Dive into the World of Parallel Programming The Go Parallel Website, sponsored
by Intel and developed in partnership with Slashdot Media, is your hub for all
things parallel software development, from weekly thought leadership blogs to
news, videos, case studies, tutorials and more. Take a look and join the
conversation now. http://goparallel.sourceforge.net/_______________________________________________ <http://goparallel.sourceforge.net/_______________________________________________>
Matplotlib-users mailing list
https://lists.sourceforge.net/lists/listinfo/matplotlib-users <https://lists.sourceforge.net/lists/listinfo/matplotlib-users>
--
Jody Klymak
http://web.uvic.ca/~jklymak/ <http://web.uvic.ca/~jklymak/>
------------------------------------------------------------------------------
Dive into the World of Parallel Programming The Go Parallel Website, sponsored
by Intel and developed in partnership with Slashdot Media, is your hub for all
things parallel software development, from weekly thought leadership blogs to
news, videos, case studies, tutorials and more. Take a look and join the
conversation now. http://goparallel.sourceforge.net/ <http://goparallel.sourceforge.net/>
_______________________________________________
Matplotlib-users mailing list
https://lists.sourceforge.net/lists/listinfo/matplotlib-users <https://lists.sourceforge.net/lists/listinfo/matplotlib-users>
--
Jody Klymak
http://web.uvic.ca/~jklymak/
Benjamin Root
2015-04-02 17:02:44 UTC
Permalink
::Looks again::

Ok, I see what you did here:

cmap = plt.get_cmap('rainbow_r’)
start=0.2
stop = 1.
colors = cmap(np.linspace(start, stop, cmap.N))
# Create a new colormap from those colors
color_map = LinearSegmentedColormap.from_list('Upper Half', colors)

I missed this part the first time through, noticing only the change to the
vmin. Yeah, I think that would work just fine. Sorry for the confusion.

Cheers!
Ben Root
Post by Benjamin Root
No, that's not what he is asking for. John wants the norm to go from -1 to
4, but he wants the colorbar to display only the 0 to 4 portion. Your
approach (setting vmin=0) would change the normalization and change the
colors.
Hmm, well his values go from 0 to 4, and he wants his colorbar to go from
0 to 4, but just over the last 4/5ths of the colormap. I think I gave him
what he wants. But I guess he can decide!
Cheers, Jody
The axes limits do not appear to be scaled by the values. They are set to
(0, 1). So, the kludgy way would seem to be to set the xlimits to be (0.2,
1) (taking out a fifth of the colorbar, but the frame is still there...
Ben Root
Post by Jody Klymak
Hi John,
I got this off stack exchange, apologies to the original contributor...
Cheers, Jody
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import LinearSegmentedColormap
x = np.arange(100)
y = np.random.rand(100)
z = 4 * np.random.rand(100)
cmap = plt.get_cmap('rainbow_r’)
start=0.2
stop = 1.
colors = cmap(np.linspace(start, stop, cmap.N))
# Create a new colormap from those colors
color_map = LinearSegmentedColormap.from_list('Upper Half', colors)
fig = plt.figure(figsize=(12,9))
ax1 = plt.subplot(111)
sc = ax1.scatter(x, y, c=z, s=50, cmap=color_map, vmin=0, vmax=4)
position=fig.add_axes([0.37, 0.16, 0.5, 0.02])
cb = fig.colorbar(sc, cax=position, orientation='horizontal',
drawedges=False)
cb.set_label('Z-Colors', fontsize=14)
# I tried this after talking with Ben Root, but it
# results in some odd behavior
# cb.ax.set_xlim(0,4)
plt.show()
Hi all,
I’m plotting some scatter points colored by a third variable, but want to
use a limited subset of a colormap. In the example below, the color axis
data ranges from 0-4, but I want to not use the red portion of the bar.
Doing the first part is just accomplished by setting the vmin/vmax. But
when I plot a color bar I don’t want to show the colors and values for
anything below zero. Other than just white-boxing that part of the bar I’m
not sure how to do it. I tried a suggestion of setting the limit properties
of the bar axis attribute, but that results in the bar getting shrunk and
shifted (a very weird behavior). Any ideas?
Thank you,
John Leeman
import numpy as np
import matplotlib.pyplot as plt
x = np.arange(100)
y = np.random.rand(100)
z = 4 * np.random.rand(100)
color_map = plt.get_cmap('rainbow_r')
fig = plt.figure(figsize=(12,9))
ax1 = plt.subplot(111)
sc = ax1.scatter(x, y, c=z, s=50, cmap=color_map, vmin=-1, vmax=4)
position=fig.add_axes([0.37, 0.16, 0.5, 0.02])
cb = fig.colorbar(sc, cax=position, orientation='horizontal',
drawedges=False)
cb.set_label('Z-Colors’, fontsize=14)
# I tried this after talking with Ben Root, but it
# results in some odd behavior
# cb.ax.set_xlim(0,4)
plt.show()
<Color_Bar.png>
------------------------------------------------------------------------------
Dive into the World of Parallel Programming The Go Parallel Website, sponsored
by Intel and developed in partnership with Slashdot Media, is your hub for all
things parallel software development, from weekly thought leadership blogs to
news, videos, case studies, tutorials and more. Take a look and join the
conversation now.
http://goparallel.sourceforge.net/_______________________________________________
Matplotlib-users mailing list
https://lists.sourceforge.net/lists/listinfo/matplotlib-users
--
Jody Klymak
http://web.uvic.ca/~jklymak/
------------------------------------------------------------------------------
Dive into the World of Parallel Programming The Go Parallel Website, sponsored
by Intel and developed in partnership with Slashdot Media, is your hub for all
things parallel software development, from weekly thought leadership blogs to
news, videos, case studies, tutorials and more. Take a look and join the
conversation now. http://goparallel.sourceforge.net/
_______________________________________________
Matplotlib-users mailing list
https://lists.sourceforge.net/lists/listinfo/matplotlib-users
--
Jody Klymak
http://web.uvic.ca/~jklymak/
John Leeman
2015-04-02 17:45:33 UTC
Permalink
Jody and Ben,

That does the business! I had hunted for awhile, but didn’t find that solution. Thank you for your help!

Cheers,

John Leeman
Post by Jody Klymak
cmap = plt.get_cmap('rainbow_r’)
start=0.2
stop = 1.
colors = cmap(np.linspace(start, stop, cmap.N))
# Create a new colormap from those colors
color_map = LinearSegmentedColormap.from_list('Upper Half', colors)
I missed this part the first time through, noticing only the change to the vmin. Yeah, I think that would work just fine. Sorry for the confusion.
Cheers!
Ben Root
No, that's not what he is asking for. John wants the norm to go from -1 to 4, but he wants the colorbar to display only the 0 to 4 portion. Your approach (setting vmin=0) would change the normalization and change the colors.
Hmm, well his values go from 0 to 4, and he wants his colorbar to go from 0 to 4, but just over the last 4/5ths of the colormap. I think I gave him what he wants. But I guess he can decide!
Cheers, Jody
The axes limits do not appear to be scaled by the values. They are set to (0, 1). So, the kludgy way would seem to be to set the xlimits to be (0.2, 1) (taking out a fifth of the colorbar, but the frame is still there...
Ben Root
Hi John,
I got this off stack exchange, apologies to the original contributor...
Cheers, Jody
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import LinearSegmentedColormap
x = np.arange(100)
y = np.random.rand(100)
z = 4 * np.random.rand(100)
cmap = plt.get_cmap('rainbow_r’)
start=0.2
stop = 1.
colors = cmap(np.linspace(start, stop, cmap.N))
# Create a new colormap from those colors
color_map = LinearSegmentedColormap.from_list('Upper Half', colors)
fig = plt.figure(figsize=(12,9))
ax1 = plt.subplot(111)
sc = ax1.scatter(x, y, c=z, s=50, cmap=color_map, vmin=0, vmax=4)
position=fig.add_axes([0.37, 0.16, 0.5, 0.02])
cb = fig.colorbar(sc, cax=position, orientation='horizontal', drawedges=False)
cb.set_label('Z-Colors', fontsize=14)
# I tried this after talking with Ben Root, but it
# results in some odd behavior
# cb.ax.set_xlim(0,4)
plt.show()
Post by John Leeman
Hi all,
I’m plotting some scatter points colored by a third variable, but want to use a limited subset of a colormap. In the example below, the color axis data ranges from 0-4, but I want to not use the red portion of the bar. Doing the first part is just accomplished by setting the vmin/vmax. But when I plot a color bar I don’t want to show the colors and values for anything below zero. Other than just white-boxing that part of the bar I’m not sure how to do it. I tried a suggestion of setting the limit properties of the bar axis attribute, but that results in the bar getting shrunk and shifted (a very weird behavior). Any ideas?
Thank you,
John Leeman
import numpy as np
import matplotlib.pyplot as plt
x = np.arange(100)
y = np.random.rand(100)
z = 4 * np.random.rand(100)
color_map = plt.get_cmap('rainbow_r')
fig = plt.figure(figsize=(12,9))
ax1 = plt.subplot(111)
sc = ax1.scatter(x, y, c=z, s=50, cmap=color_map, vmin=-1, vmax=4)
position=fig.add_axes([0.37, 0.16, 0.5, 0.02])
cb = fig.colorbar(sc, cax=position, orientation='horizontal', drawedges=False)
cb.set_label('Z-Colors’, fontsize=14)
# I tried this after talking with Ben Root, but it
# results in some odd behavior
# cb.ax.set_xlim(0,4)
plt.show()
<Color_Bar.png>
------------------------------------------------------------------------------
Dive into the World of Parallel Programming The Go Parallel Website, sponsored
by Intel and developed in partnership with Slashdot Media, is your hub for all
things parallel software development, from weekly thought leadership blogs to
news, videos, case studies, tutorials and more. Take a look and join the
conversation now. http://goparallel.sourceforge.net/_______________________________________________ <http://goparallel.sourceforge.net/_______________________________________________>
Matplotlib-users mailing list
https://lists.sourceforge.net/lists/listinfo/matplotlib-users <https://lists.sourceforge.net/lists/listinfo/matplotlib-users>
--
Jody Klymak
http://web.uvic.ca/~jklymak/ <http://web.uvic.ca/~jklymak/>
------------------------------------------------------------------------------
Dive into the World of Parallel Programming The Go Parallel Website, sponsored
by Intel and developed in partnership with Slashdot Media, is your hub for all
things parallel software development, from weekly thought leadership blogs to
news, videos, case studies, tutorials and more. Take a look and join the
conversation now. http://goparallel.sourceforge.net/ <http://goparallel.sourceforge.net/>
_______________________________________________
Matplotlib-users mailing list
https://lists.sourceforge.net/lists/listinfo/matplotlib-users <https://lists.sourceforge.net/lists/listinfo/matplotlib-users>
--
Jody Klymak
http://web.uvic.ca/~jklymak/ <http://web.uvic.ca/~jklymak/>
Loading...