14

I'm trying to make a bar plot with a color bar, each bar's hight is one variable (y) and each bar should have a color depending on another variable (c).

What I've got to is this (simple example):

data_x = [0,1,2,3]
data_hight = [60,60,80,100]
data_color = [1000,500,1000,900]


data_color = [x / max(data_color) for x in data_color]
fig, ax = plt.subplots(figsize=(15, 4))

my_cmap = plt.cm.get_cmap('GnBu')
colors = my_cmap(data_color)
rects = ax.bar(data_x, data_hight, color=colors)

CS = plt.contourf([data_x, data_color],cmap=my_cmap)

cbar = plt.colorbar(CS, cmap=my_cmap)
cbar.set_label('Color', rotation=270,labelpad=25)

plt.xticks(data_x)    
plt.ylabel("Y")

plt.show()

The main problem is that the histogram colors are fine but the color bar is in a diferent scale. besides that I can see a blue line at y=0, it shouldn't be there.

Any help will be a preciated. Thanks!

1
  • Maybe you could include an image illustrating what you mean? Also it remains unclear what you display and autolabel functions do. You should really try here to come up with a minimal example, that is stand-alone and focussed to the programming problem, not to your larger set of functions. Commented Jul 6, 2018 at 7:00

2 Answers 2

28

You are creating a contourf plot inside your bar plot. That makes no sense.

Instead you would need to create a mappable without any visual representation to supply to the colorbar. This would be a ScalarMappable.

import matplotlib.pyplot as plt
from matplotlib.cm import ScalarMappable

data_x = [0,1,2,3]
data_hight = [60,60,80,100]
data_color = [1000.,500.,1000.,900.]


data_color = [x / max(data_color) for x in data_color]
fig, ax = plt.subplots(figsize=(15, 4))

my_cmap = plt.cm.get_cmap('GnBu')
colors = my_cmap(data_color)
rects = ax.bar(data_x, data_hight, color=colors)

sm = ScalarMappable(cmap=my_cmap, norm=plt.Normalize(0,max(data_color)))
sm.set_array([])

cbar = plt.colorbar(sm)
cbar.set_label('Color', rotation=270,labelpad=25)

plt.xticks(data_x)    
plt.ylabel("Y")

plt.show()

enter image description here

Sign up to request clarification or add additional context in comments.

3 Comments

How do you include the colorbar as a legend (i.e horizontal on the top right of the figure)?
How can you do this from a dataframe, the case of multiple bar charts?
ValueError: Unable to determine Axes to steal space for Colorbar
0

For matplotlib versions greater than ~3.6, to avoid the "ValueError: Unable to determine Axes to steal space for Colorbar" error, you need to specify the axes for the colorbar, which can be done via plt.gca():

import matplotlib.pyplot as plt
from matplotlib.cm import ScalarMappable

data_x = [0,1,2,3]
data_hight = [60,60,80,100]
data_color = [1000.,500.,1000.,900.]


data_color = [x / max(data_color) for x in data_color]
fig, ax = plt.subplots(figsize=(15, 4))

my_cmap = plt.get_cmap('GnBu') # plt.cm.get_cmap is deprecated as of v3.7
colors = my_cmap(data_color)
rects = ax.bar(data_x, data_hight, color=colors)

sm = ScalarMappable(cmap=my_cmap, norm=plt.Normalize(0,max(data_color)))
sm.set_array([])

cbar = plt.colorbar(sm, ax = plt.gca()) # Supply the current axes to the colorbar
cbar.set_label('Color', rotation=270,labelpad=25)

plt.xticks(data_x)    
plt.ylabel("Y")

plt.show()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.