简体   繁体   English

Python 多图在一张图中

[英]Python multiple graph in one plot

im making a program to predict the price of stocks from different big companies, and now i have few models which show graphics, and i want that they will show them in one plot.我正在制作一个程序来预测来自不同大公司的股票价格,现在我几乎没有显示图形的模型,我希望它们能在一个图中显示它们。 So they all start from date A and goes to date B. Now i have only that first goes from date A to date B, then date B to date C and so on.所以它们都从日期 A 开始到日期 B。现在我只有第一个从日期 A 到日期 B,然后是日期 B 到日期 C 等等。 What is wrong and how to fix it ?出了什么问题以及如何解决?

for model in modelNames:
# Ploting
    if (model == "LinearRegression"):
        forecast = clfreg.predict(X_lately)
        dfreg['LinearRegression'] = np.nan
        #print(forecast, confidencereg, forecast_out)

        last_date = dfreg.iloc[-1].name
        last_unix = last_date
        next_unix = last_unix + datetime.timedelta(days=1)

        for i in forecast:
            next_date = next_unix
            #print(next_date)
            next_unix += datetime.timedelta(days=1)
            dfreg.loc[next_date] = [np.nan for _ in range(len(dfreg.columns) - 1)] + [i]
    if (model == "Quadratic2"):
        forecast = clfpoly2.predict(X_lately)
        dfreg['Quadratic2'] = np.nan
        #print(forecast, confidencereg, forecast_out)

        last_date = dfreg.iloc[-1].name
        last_unix = last_date
        next_unix = last_unix + datetime.timedelta(days=1)

        for i in forecast:
            next_date = next_unix
            #print(next_date)
            next_unix += datetime.timedelta(days=1)
            dfreg.loc[next_date] = [np.nan for _ in range(len(dfreg.columns) - 1)] + [i]
    if (model == "Quadratic3"):
        forecast = clfpoly3.predict(X_lately)
        dfreg['Quadratic3'] = np.nan
        #print(forecast, confidencereg, forecast_out)

        last_date = dfreg.iloc[-1].name
        last_unix = last_date
        next_unix = last_unix + datetime.timedelta(days=1)

        for i in forecast:
            next_date = next_unix
            #print(next_date)
            next_unix += datetime.timedelta(days=1)
            dfreg.loc[next_date] = [np.nan for _ in range(len(dfreg.columns) - 1)] + [i]
    if (model == "LassoCV"):
        forecast = clfLasso.predict(X_lately)
        dfreg['LassoCV'] = np.nan
        #print(forecast, confidencereg, forecast_out)

        last_date = dfreg.iloc[-1].name
        last_unix = last_date
        next_unix = last_unix + datetime.timedelta(days=1)

        for i in forecast:
            next_date = next_unix
            #print(next_date)
            next_unix += datetime.timedelta(days=1)
            dfreg.loc[next_date] = [np.nan for _ in range(len(dfreg.columns) - 1)] + [i]

plt.plot(dfreg['LinearRegression'].tail(500))
plt.plot(dfreg['Quadratic2'])
plt.plot(dfreg['Quadratic3'])
plt.plot(dfreg['LassoCV'])

plt.legend(loc=4)
plt.title("Price prediction (Best: "+str(modelNames[bsIndex[0]])+")")
plt.xlabel('Date')
plt.ylabel('Price')
plt.show()

在此处输入图像描述

I think your x-axis is in a different scale, or that's what matplotlib think.我认为您的 x 轴的比例不同,或者这就是 matplotlib 的想法。

Have you traid adding the x to plt.plot , such as plt.plot(x,y)你有没有把x添加到plt.plot ,例如plt.plot(x,y)

try this:尝试这个:

plt.plot(date, dfreg['LinearRegression'].tail(500))
plt.plot(date, dfreg['Quadratic2'])
plt.plot(date, dfreg['Quadratic3'])
plt.plot(date, dfreg['LassoCV'])

This example works for me:这个例子对我有用:


import numpy as np
import matplotlib.pyplot as plt

list_one = np.random.random(100)
list_two = np.random.random(100)
list_three = np.random.random(100)

range_list = range(100)

plt.plot(range_list,list_one)
plt.plot(range_list,list_two)
plt.plot(range_list,list_three)

plt.show()

例子

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM