How to randomize the colour from "red" in line: "plt.plot(actual_prices, color="red", label=f"Actual {company} Price")"


import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import pandas_datareader as web
import datetime as dt

from sklearn.preprocessing import MinMaxScaler
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Dropout, LSTM

# Load Data
company = ['AAPL', 'FB', 'GOOG', 'F', 'TSLA']

start = dt.datetime(2012, 1, 1)
end = dt.datetime(2020, 1, 1)

data = web.DataReader(company, 'yahoo', start, end)

# Prepare Data
scaler = MinMaxScaler(feature_range=(0, 1))
scaled_data = scaler.fit_transform(data['Close'].values.reshape(-1, 1))

prediction_days = 60

x_axis = []
y_axis = []

for x in range(prediction_days, len(scaled_data)):
    x_axis.append(scaled_data[x-prediction_days:x, 0])
    y_axis.append(scaled_data[x, 0])

x_axis, y_axis = np.array(x_axis), np.array(y_axis)
x_axis = np.reshape(x_axis, (x_axis.shape[0], x_axis.shape[1], 1))

# Build the Graph for Project
model = Sequential()

model.add(LSTM(units=50, return_sequences=True, input_shape=(x_axis.shape[1], 1)))
model.add(LSTM(units=50, return_sequences=True))
model.add(Dense(units=1)) # Prediction of next closing value

model.compile(optimizer='adam', loss='mean_squared_error'), y_axis, epochs=25, batch_size=32)

'''Test The Project Model Accuracy on the Existing Data'''

# Load Test Data
test_start = dt.datetime(2020, 1, 1)
test_end =

test_data = web.DataReader(company, 'yahoo', test_start, test_end)
actual_prices = test_data['Close'].values

total_dataset = pd.concat((data['Close'], test_data['Close']), axis=0)

model_inputs = total_dataset[len(total_dataset) - len(test_data) - prediction_days:].values
model_inputs = model_inputs.reshape(-1, 1)
model_inputs = scaler.transform(model_inputs)

# Make Predictions on Project test Data
x_test = []
for x in range(prediction_days, len(model_inputs)):
    x_test.append(model_inputs[x-prediction_days:x, 0])

x_test = np.array(x_test)
x_test = np.reshape(x_test, (x_test.shape[0], x_test.shape[1], 1))

predicted_prices = model.predict(x_test)
predicted_prices = scaler.inverse_transform(predicted_prices)

# Plot the test predictions
plt.plot(actual_prices, color="red", label=f"Actual {company} Price")
plt.plot(predicted_prices, color='green', label=f"Predicted {company} Price")
plt.title(f"{company} Share Price")
plt.ylabel(f'{company} Share Price')

What I want to is to take the color which I’ve set to "red" and randomize it to any other color for all the elements in "companies" namely AAPL, FB, F, GOOG in the following code snippet:

     "plt.plot(actual_prices, color="red", label=f"Actual {company} Price")"

As You Could See in this, only red is the set color for all the companies and green colored predicted values are not visible as well

P.S. I’ve tried using color="random.rand()" in place of red
P.P.S. I’m New to StackOverflow and a Noob Coder.


This way you can easily manage the colors to be plotted:


# Plot the test predictions
for i in range(len(actual_prices[0])):
    plt.plot(actual_prices[:,i],color=np.random.rand(1,3), label=f"Actual {company} Price")
    plt.plot(predicted_prices[:,i],color=np.array([0,1,0]), label=f"Predicted {company} Price")

plt.title(f"{company} Share Price")
plt.ylabel(f'{company} Share Price')

Answered By – Experience_In_AI

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published