a = frame_query("select ....",db)
With my code, the var 'c' is a numpy.ndarray which I would like to plot. 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. 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.
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
):
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.
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加