Matplotlib 和 Numpy 数学

发布于 2024-10-19 12:04:21 字数 1938 浏览 2 评论 0原文

我正在尝试使用 Matplotlib 和 Numpy 获得一些吸引力,但这并不容易。

我正在做一个迷你项目来开始处理 Matplotlib 和 Numpy 但我陷入困境......

这是代码:

# Modules
import datetime
import numpy as np
import matplotlib.finance as finance
import matplotlib.mlab as mlab
import matplotlib.pyplot as plot

# Define quote
startdate = datetime.date(2010,10,1)
today = enddate = datetime.date.today()
ticker = 'uso'

# Catch CSV
fh = finance.fetch_historical_yahoo(ticker, startdate, enddate)

# From CSV to REACARRAY
r = mlab.csv2rec(fh); fh.close()
# Order by Desc
r.sort()


### Methods Begin
def moving_average(x, n, type='simple'):
    """
    compute an n period moving average.

    type is 'simple' | 'exponential'

    """
    x = np.asarray(x)
    if type=='simple':
        weights = np.ones(n)
    else:
        weights = np.exp(np.linspace(-1., 0., n))

    weights /= weights.sum()


    a =  np.convolve(x, weights, mode='full')[:len(x)]
    a[:n] = a[n]
    return a
### Methods End


prices = r.adj_close
dates = r.date
ma20 = moving_average(prices, 20, type='simple')
ma50 = moving_average(prices, 50, type='simple')

# Get when ma20 crosses ma50
equal = np.round(ma20,1)==np.round(ma50,1)
dates_cross  = (dates[equal])
prices_cross = (prices[equal])

# Get when ma20 > ma50
ma20_greater_than_ma50 = np.round(ma20,1) > np.round(ma50,1)
dates_ma20_greater_than_ma50  = (dates[ma20_greater_than_ma50])
prices_ma20_greater_than_ma50 = (prices[ma20_greater_than_ma50])

print dates_ma20_greater_than_ma50
print prices_ma20_greater_than_ma50

现在我需要做这样的事情:

store the price of the "price_cross"
see if one day after the "ma20_greater_than_ma50" statment is true, if true store the price as "price of the one day after"
now do "next price_cross" - "price of the one day after"  (price2 - price1) for all occurences

我怎样才能做这个数学,更重要的是。如何获得 Matplotlib 和 Numpy 的吸引力。我应该买什么书?

给我一些线索。

此致,

I'm trying get some traction with Matplotlib and Numpy but it is not very easy.

I'm doing a mini project to start dealing with Matplotlib and Numpy but I'm stuck...

Here is the code:

# Modules
import datetime
import numpy as np
import matplotlib.finance as finance
import matplotlib.mlab as mlab
import matplotlib.pyplot as plot

# Define quote
startdate = datetime.date(2010,10,1)
today = enddate = datetime.date.today()
ticker = 'uso'

# Catch CSV
fh = finance.fetch_historical_yahoo(ticker, startdate, enddate)

# From CSV to REACARRAY
r = mlab.csv2rec(fh); fh.close()
# Order by Desc
r.sort()


### Methods Begin
def moving_average(x, n, type='simple'):
    """
    compute an n period moving average.

    type is 'simple' | 'exponential'

    """
    x = np.asarray(x)
    if type=='simple':
        weights = np.ones(n)
    else:
        weights = np.exp(np.linspace(-1., 0., n))

    weights /= weights.sum()


    a =  np.convolve(x, weights, mode='full')[:len(x)]
    a[:n] = a[n]
    return a
### Methods End


prices = r.adj_close
dates = r.date
ma20 = moving_average(prices, 20, type='simple')
ma50 = moving_average(prices, 50, type='simple')

# Get when ma20 crosses ma50
equal = np.round(ma20,1)==np.round(ma50,1)
dates_cross  = (dates[equal])
prices_cross = (prices[equal])

# Get when ma20 > ma50
ma20_greater_than_ma50 = np.round(ma20,1) > np.round(ma50,1)
dates_ma20_greater_than_ma50  = (dates[ma20_greater_than_ma50])
prices_ma20_greater_than_ma50 = (prices[ma20_greater_than_ma50])

print dates_ma20_greater_than_ma50
print prices_ma20_greater_than_ma50

Now I need to do something like this:

store the price of the "price_cross"
see if one day after the "ma20_greater_than_ma50" statment is true, if true store the price as "price of the one day after"
now do "next price_cross" - "price of the one day after"  (price2 - price1) for all occurences

How can I do this math and more important. How can I get traction with Matplotlib and Numpy. What books should I buy?

Give me some clues.

Best Regards,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

小巷里的女流氓 2024-10-26 12:04:21

我同意乔什的观点,但想添加 matplotlib 画廊:

http://matplotlib.sourceforge.net/gallery。 我的大多数绘图

都是从直接复制接近我想要的内容开始的,然后修改它以满足我的需要。 matplotlib 库中有很多这样的例子。

I agree with Josh, but wanted to add the matplotlib gallery:

http://matplotlib.sourceforge.net/gallery.html

Most of my plots start off directly copying something close to what I want, and then modifying it to fit my needs. The matplotlib gallery has many such examples.

为你鎻心 2024-10-26 12:04:21

我想说的是,你不一定需要出去购买任何书籍。更好(也更便宜)的解决方案是查看在线教程,例如:

链接

http://matplotlib.sourceforge.net/examples/index.html

并尝试将文档中的内容拼凑起来并搜索相关关键字。从您提供的代码(假设您编写的)中,您对 numpy 有一定的了解。您需要更具体地说明您遇到的问题,以获得更具体/详细的帮助。

I would say that you don't necessarily need to go out and purchase any books. The better (and cheaper) solution is to take a look at online tutorials like:

Link

http://matplotlib.sourceforge.net/examples/index.html

and try to piece together things from the documentation and searching pertinent keywords. From the code you've presented (assuming you wrote it), you have some grasp of numpy. You will need to be a bit more specific with the problems you're encountering to get more specific/detailed help.

陌路黄昏 2024-10-26 12:04:21

首先是一个列表,浏览完后您可能会发现对您来说最重要的部分:

  1. Python 教程 http ://docs.python.org/tutorial/
  2. 来自 http://docs.scipy 的 Numpy 用户指南.org/doc/
  3. Matplotlib 用户指南 http://matplotlib.sourceforge.net/users /index.html
  4. Numpy/Scipy 附加文档源 http://www.scipy.org/Additional_Documentation

您可能想要订阅 numpy 和/或 matplotlib 的邮件列表。

Here's a list to start with, you probably find the parts that are most important for you after browsing through them:

  1. Python tutorial http://docs.python.org/tutorial/
  2. Numpy user guide from http://docs.scipy.org/doc/
  3. Matplotlib user guide http://matplotlib.sourceforge.net/users/index.html
  4. Numpy/Scipy additional documentation sources http://www.scipy.org/Additional_Documentation

You may want to subscribe to the mailling lists for numpy and/or matplotlib.

日暮斜阳 2024-10-26 12:04:21

matplotlib 和 numpy 有大量有用的函数,在实现之前你应该先谷歌一下。

例如,请参阅 matplotlib movavg 函数。

matplotlib and numpy have a huge list of useful functions, you should always google first before implementing.

for example see matplotlib movavg function.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文