Я пытаюсь добавить дополнительную галочку на ось X моей matplotlib, если ее там еще нет. Я хочу удалить метку галочки, если ее еще нет, а затем покрасить галочку.
В настоящее время мой код делает все, кроме раскрашивает галочку, если мне нужно добавить галочку, потому что ее нет.
Другими словами, он окрасит галочку в красный цвет, если галочка уже присутствует, но этого не произойдет, если я создам новую галочку.
Моя логика неправильно?
Это код, который я написал:
for i, d in enumerate(ds):
plt.imshow(2Darray, interpolation='none', extent=[left, right, bottom, top], aspect='auto' )
xticks = list(plt.xticks()[0]) #get x-axis ticks
if d not in xticks: #if d is not in the x-axis ticks
plt.xticks(xticks + [d]) #add d to x-axis ticks
new_xticks = list(plt.xticks()[0]) #get new x-axis ticks
index_d = new_xticks.index(d) #find index where d is
plt.gca().get_xticklabels()[index_d].set_visible(False) #hide the tick label for the d tick just created
xticks = list(plt.xticks()[0]) #get x-axis ticks again since d may have been added
index_d = xticks.index(d) #find index where d is
plt.gca().get_xticklines()[index_d].set_markeredgecolor('red') #make the tick color red where d is
Подробнее здесь: https://stackoverflow.com/questions/798 ... matplotlib