2

我有一些烛台数据存储在列表中(日期时间、开盘、收盘、高点、低点)。使用 matplotlib 绘制这些数据的最佳方法是什么?我是否必须自动通过 numpy ?在这种情况下,我将如何将列表转换为 numpy 可以理解的内容?

提前致谢 。

4

4 回答 4

5

Actually, there's no reason to do anything other than what you already have. Matplotlib will handle converting things for you.

It sounds like you have a list of sequences of time, open, close, high low?

Something like:

from datetime import datetime
#            date                 open   close    high    low
quotes = [(datetime(2012, 2, 1), 103.62, 102.01, 103.62, 101.90),
          (datetime(2012, 2, 2), 102.24, 102.90, 103.16, 102.09),
          ...
          (datetime(2012, 4, 12), 100.89, 102.59, 102.86, 100.51)]

That's actually the exact data structure that matplotlib's candlestick function expects.

You just need to convert the datetimes to matplotlib's internal date format. Use matplotlib.dates.date2num.

E.g.

from matplotlib.dates import date2num

# I'm assuming you have tuples, so we can't modify them in-place...
quotes = [(date2num(item[0]),) + item[1:] for item in quotes]

Other than that, have a look at some of the matplotlib finance examples. This one is a good start.

于 2012-04-24T15:33:04.927 回答
0

您可以轻松地将 python 列表转换为 numpy 列表

import numpy as np
l1 = [1, 2, 3, 4]
a = np.array(l1)

尽管 matplotlib 也接受 python 列表(我认为它在内部将其转换为 numpy 数组)

于 2012-04-24T05:14:38.450 回答
0

您可以使用itemgetter

from operator import itemgetter
data = [(1,2,3), (2,3,4), (3,4,5)]

import pylab as plt
plt.plot(map(itemgetter(0), data),map(itemgetter(1),data),'o')

如果您有多个图(您可以根据日期时间数据绘制高、低、高低差等),最好将其转换为 numpy 数组,正如@Spot 所建议的那样。

于 2012-04-24T11:49:15.690 回答
0

可能最好的方法是将所有这些数据绘制在一个图表中,这样您就可以看到相对关系。您可以通过这种方式在图表中绘制多条线:

import pylab

data = [(5,2,3), (2,8,4), (3,5,9)]
t_data = zip(*data) #transform the data

crd = range(len(t_data[0])) #coordinates

pylab.plot(crd, t_data[0], crd, t_data[1], crd, t_data[2])

pylab.show()

但是,如果您想在不同的图表中打印每个类别,您可以这样做:

fig = pylab.figure()

sub_fig_1 = fig.add_subplot(3,1,1)
sub_fig_1.plot(t_data[0])

sub_fig_2 = fig.add_subplot(3,1,2)
sub_fig_2.plot(t_data[1])

sub_fig_3 = fig.add_subplot(3,1,3)
sub_fig_3.plot(t_data[2])

pylab.show()
于 2012-04-24T14:07:50.210 回答