模块 'pylab' 没有属性 'scatter'

Module 'pylab' has no attribute 'scatter'

我正在研究股票行情数据的线性回归模型,但我无法让 Pylab 正常工作。我已经成功地绘制了数据,但我想得到一条最适合我所拥有的数据的线。 (不用于任何特定目的,只是一组随机数据以使用线性回归。)

import pylab
import urllib.request
from matplotlib import pyplot as plt
from bs4 import BeautifulSoup
import requests

def chartStocks(*tickers):

    # Run loop for each ticker passed in as an argument
    for ticker in tickers:

        # Convert URL into text for parsing
        url = "http://finance.yahoo.com/q/hp?s=" + str(ticker) + "+Historical+Prices"
        sourceCode = requests.get(url)
        plainText = sourceCode.text
        soup = BeautifulSoup(plainText, "html.parser")

        # Find all links on the page
        for link in soup.findAll('a'):
            href = link.get('href')
            link = []
            for c in href[:48]:
                link.append(c)
            link = ''.join(link)

            # Find the URL for the stock ticker CSV file and convert the data to text
            if link == "http://real-chart.finance.yahoo.com/table.csv?s=":
                csv_url = href
                res = urllib.request.urlopen(csv_url)
                csv = res.read()
                csv_str = str(csv)

                # Parse the CSV to create a list of data points
                point = []
                points = []
                curDay = 0
                day = []
                commas = 0               
                lines = csv_str.split("\n")
                lineOne = True
                for line in lines:
                    commas = 0
                    if lineOne == True:
                        lineOne = False
                    else:
                        for c in line:
                            if c == ",":
                                commas += 1
                            if commas == 4:
                                point.append(c)
                            elif commas == 5:
                                for x in point:
                                    if x == ",":
                                        point.remove(x)
                                point = ''.join(point)
                                point = float(point)
                                points.append(point)
                                day.append(curDay)
                                curDay += 1
                                point = []
                                commas = 0
                points = list(reversed(points))

                # Plot the data
                pylab.scatter(day,points)
                pylab.xlabel('x')
                pylab.ylabel('y')
                pylab.title('title')

                k, b = pylab.polyfit(day,points,1)
                yVals = k * day + b
                pylab.plot(day,yVals,c='r',linewidth=2)
                pylab.title('title')

                pylab.show()


chartStocks('AAPL')

出于某种原因,我收到了一个属性错误,我不确定为什么。我是否错误地将数据传递给 pylab.scatter()?我不确定传递 x 和 y 值列表是否是正确的方法。我没能找到其他 运行 关注此问题的人,而且 .scatter 绝对是 Pylab 的一部分,所以我不确定发生了什么。

当您使用 pylab 时,它会导入一些其他包。因此,当您执行 import pylab 时,您会得到带有前缀 np 的 numpy,因此您需要 np.polyfit。正如这个问题所示,我认为如果您直接导入 numpy 来执行此操作,代码的读者会更清楚。

我认为存在版本冲突。尝试:

plt.pyplot.scatter(day,points)