I have a dictionary containing keys as labels and values as colors for a plot. Using an example from:Change color of specific ticks at plot with matplotlib . How do I change the label colors based on the keys in the dictionary. Note, the labels can be repeated multiple times on the x-axis and they can be strings e.g labels = 'A', 'B', 'C', 'D'.
Here is the little modification of the code:
import matplotlib.pyplot as plt
fig, ax = plt.subplots(figsize=(5,4))
ax.plot([1,2,3])
colors = {0.00 :'grey',0.25:'yellow',0.50:'brown',2.00:'red'}
for k, v in colors.items():
ax.get_xticklabels()[k].set_color(v)
plt.show()
I expected each label on the plot to be associated with a color but I am getting a plain plot with this error: TypeError: list indices must be integers or slices, not float. I am trying to adapt it to string labels specifically.