图形不会显示在Python中

时间:2018-12-12 16:09:08

标签: python numpy matplotlib scikit-learn

我是编程和尝试在Python中使用图形的新手。但是我遇到了某种错误,该图将不会显示。我在Ubuntu OS上。希望一些Python专家可以解释出什么问题以及如何解决。

代码:

import csv
import numpy as np
from sklearn.svm import SVR
import matplotlib.pyplot as plt



dates = []
prices = []

def get_data(filename):
    with open(filename, 'r') as csvfile:
        csvFileReader = csv.reader(csvfile)
        next(csvFileReader)
         for row in csvFileReader:
            dates.append(int(row[0].split('-')[0]))
             prices.append(float(row[1]))
    return

 def predict_prices(dates, prices, x):
    dates = np.reshape(dates,(len(dates), 1))
    svr_lin = SVR(kernel='linear', C=1e3)
    svr_poly = SVR(kernel='poly', C=1e3, degree = 2)
    svr_rbf = SVR(kernel='rbf',C=1e3, gamma = 0.1)
    svr_rbf.fit(dates, prices)
    svr_poly.fit(dates, prices)
    svr_rbf.fit(dates, prices)

    plt.scatter(dates, prices, color='black', label='Data')
    plt.plot(dates, svr_rbf.predict(dates), color='red', label='RBF model')
    plt.plot(dates, svr_lin.predict(dates), color='green', label='Linear 
model')
    plt.plot(dates, svr_poly.predict(dates), color='blue', label='Polynomial 
model')
    plt.xlabel('Date')
    plt.ylabel('Price')
    plt.title('Support Vector Regression')
    plt.legend()
    plt.show()

    return svr_rbf.predict(x)[0], svr_lin.predict(x)[0], svr_poly.predict(x) 
[0]
get_data('aapl.csv')
predicted_price = predict_prices(dates, prices, 29)
print(predicted_price)

导致此错误:

  

/home/xxx/.local/lib/python3.6/site-packages/sklearn/svm/base.py:196:
  FutureWarning:在0.22版中,伽玛的默认值将从“自动”更改为“缩放”,以更好地说明未缩放的功能。明确将gamma设置为“自动”或“缩放”,以避免出现此警告。

1 个答案:

答案 0 :(得分:0)

我曾经遇到过这种情况,即情节没有出现。就我而言,这与matplotlib使用的后端有关。

要检查选定的后端,您可以尝试以下操作:

 matplotlib.get_backend()

对于典型的matplotlib安装,通常已经设置好默认后端,但是取决于您的操作系统和特定的用例,您可能需要选择其他设置。

例如,在我当前在ubuntu 18.04上的安装中,我正在使用'Qt5Agg'后端。

您始终可以在以下官方文档中找到更多信息:https://matplotlib.org/faq/usage_faq.html#what-is-a-backend

通过以上链接

复制

  

有四种方法来配置后端。如果他们彼此冲突   其他,将使用下面列表中最后提到的方法,   例如调用use()将覆盖您的matplotlibrc中的设置。

matplotlibrc文件中的backend参数

backend : WXAgg   # use wxpython with antigrain (agg) rendering

为您当前的shell或单个脚本设置MPLBACKEND环境变量:

export MPLBACKEND="module://my_backend"
python simple_plot.py

MPLBACKEND="module://my_backend" python simple_plot.py

要为单个脚本设置后端,您也可以使用-d命令行参数(不建议使用):

python script.py -dbackend

如果您的脚本取决于特定的后端,则可以使用use()函数:

import matplotlib
matplotlib.use('PS') 
相关问题