I am looking to change the color of the ticklabels
in my heatmap based on some condition. For example below, I would like to change the colors of ticklabels
containing A
as red
and B
as green
.
I tried making a list ['red', 'green', 'red', 'green']
and pass it along the color option but no success so far. Any help would be great.
import numpy as np
import matplotlib.pyplot as plt
alpha = ['ABC', 'BDF', 'ADF', 'BCF']
data = np.random.random((4,4))
fig = plt.figure()
ax = fig.add_subplot(111)
cax = ax.matshow(data, interpolation='nearest')
fig.colorbar(cax)
ax.set_xticklabels(['']+alpha)
ax.set_yticklabels(['']+alpha)
plt.show()
You can set the text colour using set_color
on the label text objects:
for l in ax.xaxis.get_ticklabels()+ax.yaxis.get_ticklabels():
if 'A' in l.get_text():
l.set_color('r')
elif 'B' in l.get_text():
l.set_color('g')
Of course, this doesn't address what happens if both A
and B
are present in the label (ie should ABC
be red or green)?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.