Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

An example of how to associate a color to each bar and plot a color bar

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

data_x = [0,1,2,3]
data_hight = [60,60,80,100]
data_color = [200.,600.,0.,750.]

data_color_normalized = [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_normalized)

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.title('How to plot a bar chart with a colorbar with matplotlib ?')

plt.savefig("bar_chart_with_colorbar_01.png", bbox_inches='tight')

plt.show()
Comment

PREVIOUS NEXT
Code Example
Python :: how to get maximum value of number in python 
Python :: how to sort in python 
Python :: any python type hint 
Python :: fetch last record from django model 
Python :: Python Try Except Else Clause 
Python :: what is not equals in python 
Python :: shibang for python file in linux 
Python :: Prints all integers of a list 
Python :: append two dfs 
Python :: True Positive, True Negative, False Positive, False Negative in scikit learn 
Python :: python array linspace 
Python :: scikit decision tree 
Python :: pandas array of dataframes 
Python :: recorrer lista desde el final python 
Python :: django error displaying images page not found 
Python :: check how many days old file is python 
Python :: pandas and operator 
Python :: drf serializer unique together 
Python :: python get focused window 
Python :: how to add path to python in windows 
Python :: powershell bulk rename and add extra string to filename 
Python :: DJANGO model instance get by variable 
Python :: python dataframe add row 
Python :: How to go up in a path in python 
Python :: count unique values in python 
Python :: python string [::-1] 
Python :: How to change application icon of pygame 
Python :: how to store something in python 
Python :: datetime time set seconds 
Python :: python selenium check if browser is open 
ADD CONTENT
Topic
Content
Source link
Name
4+2 =