简体   繁体   中英

How can I plot x/y datapoints from three different time periods on the same axes for analysis?

I'm wanting to statistically compare the results of linear regression analyses for air temperature (x) versus gas use (in kWh) across three different years.

I'm unsure how to approach plotting multiple conditions on the same xy axes and then go about the statistical analysis.

I've been using SciKitLearn using code from an excellent tutorial to plot my regression analysis for each time period (testing data included below). However, unsure how i can include multiple conditions in the same plot?

import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error, r2_score

# test data
np.random.seed(0)
x = np.random.rand(100, 1)
y = 2 + 3 * x + np.random.rand(100, 1)

# sckit-learn implementation

# Model initialization
regression_model = LinearRegression()
# Fit the data(train the model)
regression_model.fit(xa, ya)
# Predict
ya_predicted = regression_model.predict(xa)

# model evaluation
rmse = mean_squared_error(ya, ya_predicted)
r2 = r2_score(ya, ya_predicted)

# printing values
print('Slope:' ,regression_model.coef_)
print('Intercept:', regression_model.intercept_)
print('Root mean squared error: ', rmse)
print('R2 score: ', r2)

# plotting values

# data points
plt.scatter(xa, ya, s=10)
plt.xlabel('Average air temperature (\xb0C)')
plt.ylabel('Total daily gas use (kWh)')

# predicted values
plt.plot(xa, ya_predicted, color='r')
plt.show()

Thanks!

To plot multiple conditions just don't call plt.show() until you've plotted them all.

example:

import matplotlib.pyplot as plt
import numpy as np

values = np.linspace(1, 10, 5)
new = [1, 2, 3, 4, 5]
new2 = [4, 5, 6, 7, 8]
plt.scatter(values, new)
plt.plot(values, new2)
plt.show()

you get:

在此处输入图片说明

if you're plotting across three different years, you'll need to plot as a function of time of year so that you have the same x axis for all three graphs. You can use different colors of the graphs and a label legend to indicate the year of each thing you're plotting.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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