简体   繁体   English

尝试绘制迭代,但plt.plot为空

[英]try plotting an iteration but plt.plot is empty

a = frame_query("select ....",db)

With my code, the var 'c' is a numpy.ndarray which I would like to plot. 对于我的代码,var'c'是我要绘制的numpy.ndarray。 However when I execute the following code, i get an empty plot! 但是,当我执行以下代码时,我得到了一个空图!

for i in a.values:
    c = (i[:1]-a.values[-1:])/a.values[-1:]*100
    plt.plot(c)
plt.show()

print c

gives: 得到:

[[ 28.57142857]]
[[ 27.27272727]]
[[ 27.92207792]]
[[ 28.57142857]]
[[ 22.07792208]]
[[ 22.07792208]]
[[ 22.07792208]]

Where exactly am I going wrong? 我到底哪里出问题了?

Thank you in advance. 先感谢您。

The simplest way to adapt your code is probably to start an empty list before the loop, append the values from c onto it within the loop and then plot it after the loop. 修改代码的最简单方法可能是在循环之前开始一个空列表,将c的值附加到循环内的列表上,然后在循环之后进行绘制。 For example: 例如:

c_series = []

for i in a.values:
    c = (i[:1]-a.values[-1:])/a.values[-1:]*100
    c_series.append(c[0])

plt.plot(c)
plt.show()

Note using c[0] is safe here, because the logic of the line above guarantees that the ndarray will only have one member. 请注意,此处使用c[0]是安全的,因为上述行的逻辑保证了ndarray仅具有一个成员。

However , that's a bit of an odd way to deal with your data structure. 但是 ,这是处理数据结构的一种奇怪方法。 As it seems that a.values is an ndarray , you could also simply do this by using the facilities that numpy provides to perform arithmetical operations on arrays (I can't test it as I don't have a verbatim copy of your a.values ): 似乎a.values是一个ndarray ,您也可以通过使用numpy提供的对数组执行算术运算的功能来简单地执行此操作(由于没有a的逐字副本,因此我无法对其进行测试a.values ):

const = a.values[-1:]
c_series = (a.values - const) / const*100
plt.plot(c_series)
plt.show()

In general - when using numpy arrays, it's often better to keep them as arrays (called vectorising the code), rather than dealing with them element by element in a loop. 通常,使用numpy数组时,通常最好将它们保留为数组(称为向量化代码),而不是在循环中逐个元素地处理它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM