Matplotilb-需要从类属性中查找源数据

时间:2016-05-13 19:52:48

标签: python oop matplotlib

我有一个使用以下内容创建的lines对象:

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

<class 'matplotlib.lines.Line2D'>

我需要找到两个列表'xxxx''yyyy'的名称。如何从类属性中获取它们?

1 个答案:

答案 0 :(得分:0)

您可以使用dir在python中查看对象的内容,或者查看该类的docs。我想你正在寻找的对象是xdataydata(虽然我有点困惑,但在你的帖子中你要求列出名字?)

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]

希望它有所帮助。