Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

add legend to colorbar

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

#discrete color scheme
cMap = ListedColormap(['white', 'green', 'blue','red'])

#data
np.random.seed(42)
data = np.random.rand(4, 4)
fig, ax = plt.subplots()
heatmap = ax.pcolor(data, cmap=cMap)

#legend
cbar = plt.colorbar(heatmap)
cbar.ax.set_yticklabels(['0','1','2','>3'])
cbar.set_label('# of contacts', rotation=270)

# put the major ticks at the middle of each cell
ax.set_xticks(np.arange(data.shape[1]) + 0.5, minor=False)
ax.set_yticks(np.arange(data.shape[0]) + 0.5, minor=False)
ax.invert_yaxis()

#labels
column_labels = list('ABCD')
row_labels = list('WXYZ')
ax.set_xticklabels(column_labels, minor=False)
ax.set_yticklabels(row_labels, minor=False)

plt.show()
Comment

PREVIOUS NEXT
Code Example
Python :: python calendar table view 
Python :: tensorflow Dense layer activatity leaklyrelu 
Python :: split a column in pandas 
Python :: how to convert str to int python 
Python :: generate coordinates python 
Python :: docker compose cron 
Python :: how to count all files on linux 
Python :: convert exception to string python 
Python :: how to find greatest number in python 
Python :: Counter() Function 
Python :: textrank python implementation 
Python :: how to make loops in python 
Python :: pytest fixture 
Python :: flask app run 
Python :: sum up list python 
Python :: how to find the last occurrence of a character in a string in python 
Python :: Excel file format cannot be determined, you must specify an engine manually 
Python :: reaction role discord.py 
Python :: when iterating through a pandas dataframe using index, is the index +1 able to be compared 
Python :: how to schedule python script in windows 
Python :: typecasting python 
Python :: set intersection 
Python :: Python NumPy delete Function Example 
Python :: js choice function 
Python :: models django 
Python :: Remove an element from a Python list Using pop() method 
Python :: python how to run code in ssh 
Python :: remove item in dict 
Python :: python get item from set 
Python :: python numpy delete column 
ADD CONTENT
Topic
Content
Source link
Name
1+6 =