0

I have a lines object which was created with the following:

junk = plt.plot([xxxx], [yyyy])
for x in junk:
    print type(x)

<class 'matplotlib.lines.Line2D'>

I need to find the names of the two lists 'xxxx' and 'yyyy'. How can I get them from the class attributes?

ali_m
  • 71,714
  • 23
  • 223
  • 298
matt
  • 43
  • 6
  • That's not how Python works. When you pass objects as inputs to a function, the function doesn't know anything about what variable names those objects are bound to outside of the scope of the function call. For all we know, the same object might be bound to multiple different variable names (e.g. `a = b = 1`), so how could we ever know which name was the 'correct' one? – ali_m May 13 '16 at 20:26

1 Answers1

0

You can use dir to see the content of an object in python, or check the docs for the class. I guess the objects you are looking for are xdata and ydata (although I'm a bit confused, in your post you ask for the names of the lists?)

In [27]:

import numpy as np
import matplotlib.pyplot as plt
​
x = np.arange(0, 5, 0.1);
y = np.sin(x)
junk = plt.plot(x, y)
for x in junk:
    #print(dir(x))
    print(x.get_xdata())
    print(x.get_ydata())
[ 0.   0.1  0.2  0.3  0.4  0.5  0.6  0.7  0.8  0.9  1.   1.1  1.2  1.3  1.4
  1.5  1.6  1.7  1.8  1.9  2.   2.1  2.2  2.3  2.4  2.5  2.6  2.7  2.8  2.9
  3.   3.1  3.2  3.3  3.4  3.5  3.6  3.7  3.8  3.9  4.   4.1  4.2  4.3  4.4
  4.5  4.6  4.7  4.8  4.9]
[ 0.          0.09983342  0.19866933  0.29552021  0.38941834  0.47942554
  0.56464247  0.64421769  0.71735609  0.78332691  0.84147098  0.89120736
  0.93203909  0.96355819  0.98544973  0.99749499  0.9995736   0.99166481
  0.97384763  0.94630009  0.90929743  0.86320937  0.8084964   0.74570521
  0.67546318  0.59847214  0.51550137  0.42737988  0.33498815  0.23924933
  0.14112001  0.04158066 -0.05837414 -0.15774569 -0.2555411  -0.35078323
 -0.44252044 -0.52983614 -0.61185789 -0.68776616 -0.7568025  -0.81827711
 -0.87157577 -0.91616594 -0.95160207 -0.97753012 -0.993691   -0.99992326
 -0.99616461 -0.98245261]

Hope it helps.

lrnzcig
  • 3,868
  • 4
  • 36
  • 50
  • Thanks for the reply. In the example you gave, xdata is a numpy array called 'x'. I would like to be able to use the name, 'x' in this case', for something else. Is it possible to obtain the name of the input array? – matt May 13 '16 at 20:31
  • Hi. I was suspecting something like that, although I wonder why would you want to do it. Take a look to [this answer](http://stackoverflow.com/a/1538380/3519000). Good luck. – lrnzcig May 13 '16 at 21:04
  • Hi thanks again for the reply. The reason I want this is because I am using violinplot(), which takes a list of lists and plots each list as a violin (a variation of a box plot). To change the colors of the violins, I can iterate over the violins, but that makes me lose track of the names. So that is my real problem. I have the code pasted below list_data = sorted(list_data) violin_parts = plt.violinplot(list_data) for pc in violin_parts['bodies']: #iterate over all violins Somehow get names for each??? – matt May 16 '16 at 13:57
  • The violins at the for loop should come in the same order as your input data. Take a look at the comment at [this answer](http://stackoverflow.com/a/26291582/3519000), if I understood correctly you could do exactly the same. Cheers. – lrnzcig May 16 '16 at 14:16