3

Following this I know that I can extract the xticks labels and positions using:

import matplotlib.pyplot as plt

plt.scatter(x_data, y_data)
locs, labels=plt.xticks()

the new variable labels is a matplotlib.cbook.silent_list, which doesn't behave like a normal list. Is there a way to access and modify any attribute value of the labels elements?
Specifically I would like to know if I can select a subset of the labels (i.e. slice the silent_list) and modify a particular attribute for that subset.

Here is a toy example:

import numpy as np
import matplotlib.pyplot as plt

x=np.array([1,2,3,4,5,6,7,8])
y=np.random.normal(0, 1, (8, 1))
plt.scatter(x, y)
locs, labels=plt.xticks()    

As an example, let say I want to change the labels color to red for all but the first and last element of labels; if I open one of the elements of the variable I can see that there is the attribute _color with value k, which I would like to change in r:

here

I tried to slice it:

labels[1:-1]

But it returns:

Out[]: [Text(2,0,'2'), Text(4,0,'4'), Text(6,0,'6'), Text(8,0,'8')]

and this is as far as I managed to go.
I couldn't figure out a way to access the attribute and change its value.

NB: I am looking for a general way to access these attributes and change the value, I do not care about changing the labels color specifically. That's just an example.

CAPSLOCK
  • 6,243
  • 3
  • 33
  • 56
  • 2
    `plt.setp(labels[2:-2], color="red")` (note the 2 because one label is outside of the axes) – ImportanceOfBeingErnest Mar 27 '19 at 13:57
  • [This](https://stackoverflow.com/a/11250884/4932316) is exactly what you need. The key here is `fig.canvas.draw()` and then use `get_text()` – Sheldore Mar 27 '19 at 14:14
  • Possible duplicate of [Modify tick label text](https://stackoverflow.com/questions/11244514/modify-tick-label-text) – Sheldore Mar 27 '19 at 14:14
  • @Bazingaa not really, I am specifically asking how to access the properties of that object. It's way broader than simply changing the text and it allows me a great degree of flexibility in what I can do with the labels. – CAPSLOCK Mar 27 '19 at 14:26
  • Ok, so IOBE's comment solves your problem – Sheldore Mar 27 '19 at 14:32
  • I am trying to generalize it right now =). I believe it does – CAPSLOCK Mar 27 '19 at 14:33
  • @ImportanceOfBeingErnest @Bazingaa since we are here. Could you also help me out with understanding what are the rows in `labels`? I posted a different [question](https://stackoverflow.com/questions/55378410/matplotlib-xticks-extracting-the-labels-object-what-are-the-elements-in-ther) about it – CAPSLOCK Mar 27 '19 at 14:35
  • @Gio: If I understood your question on "rows" correctly, the rows you see in the image you posted are the attributes of your labels objects. You can change their values using `get` and `set` like I did in my answer below for changing the colors – Sheldore Mar 27 '19 at 14:46
  • @Bazingaa what I do not understand is why if I have `N` ticks and `N` labels I have `2*N` rows – CAPSLOCK Mar 27 '19 at 14:54
  • @Gio: I saw that IOBE answered your comment on a different post. – Sheldore Mar 27 '19 at 15:51
  • @Bazingaa yup, that's solved as well. – CAPSLOCK Mar 27 '19 at 15:55

1 Answers1

2

You might be interested in an alternative solution where you can choose which specific ticks you want to color. Here I have to loop from [1:-1] because the first and the last ticks do not appear on the graph here but they appear in the labels

import numpy as np; np.random.seed(134)
import matplotlib.pyplot as plt

fig, ax = plt.subplots()

x=np.array([1,2,3,4,5,6,7,8])
y=np.random.normal(0, 1, (8, 1))
plt.scatter(x, y)

fig.canvas.draw()
xticks = ax.get_xticklabels()

target_ticks = [1, 3, 6, len(xticks)-2]

for i, lab in enumerate(xticks[1:-1]):
    if i+1 in target_ticks:
        lab.set_color('r')

enter image description here

Sheldore
  • 37,862
  • 7
  • 57
  • 71