我是 Python 和 Streamlit 的新手,在 Streamlit 上绘制两个图表时遇到问题。
在 jupyter 笔记本上,它可以很好地使用此代码:
train_size = X_train.shape[0]
f,axs = plt.subplots(1,2,figsize=(20,10))
axs[0].plot(df['date'][:train_size], df['close'][:train_size], color='black')
axs[0].plot(df['date'][train_size:], linear_regression_validation_predict, color='red')
axs[0].plot(df['date'][train_size:], df['close'][train_size:], color='green')
axs[1].plot(df['date'][train_size:], linear_regression_validation_predict, color='red')
axs[1].plot(df['date'][train_size:], df['close'][train_size:], color='green')
但是当我尝试此代码时在 Streamlit 上:
train_size = X_train.shape[0]
f,axs = plt.subplots(1,2,figsize=(20,10))
axs[0].plot(df['date'][:train_size], df['close'][:train_size], color='black')
axs[0].plot(df['date'][train_size:], linear_regression_validation_predict, color='red')
axs[0].plot(df['date'][train_size:], df['close'][train_size:], color='green')
axs[1].plot(df['date'][train_size:], linear_regression_validation_predict, color='red')
axs[1].plot(df['date'][train_size:], df['close'][train_size:], color='green')
st.pyplot(fig=f)
我只显示了第一个图表。
我期待您的反馈。
我生成了一些随机数据,但无法重现您的问题,您可以看到两个图都是可见的。
import streamlit as st
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
# Creating random data for X_train
np.random.seed(0) # For reproducibility
X_train = np.random.randn(100, 1) # 100 random samples, 1 feature
df = pd.DataFrame({'date': pd.date_range(start='2023-01-01', periods=100),
'close': np.random.rand(100) * 100})
train_size = X_train.shape[0]
f, axs = plt.subplots(1, 2, figsize=(20, 10))
# Assuming linear_regression_validation_predict is defined somewhere in your code
linear_regression_validation_predict = np.random.rand(100) * 100 # Dummy data for prediction
axs[0].plot(df['date'][:train_size], df['close'][:train_size], color='black')
axs[0].plot(df['date'][train_size:], linear_regression_validation_predict[:100 - train_size], color='red')
axs[0].plot(df['date'][train_size:], df['close'][train_size:], color='green')
# this generates a blank plot because it's not selecting data
# axs[1].plot(df['date'][train_size:], linear_regression_validation_predict[:100 - train_size], color='red')
# axs[1].plot(df['date'][train_size:], df['close'][train_size:], color='green')
axs[1].plot(df['date'][:train_size], df['close'][:train_size], color='black')
axs[1].plot(df['date'][train_size:], linear_regression_validation_predict[:100 - train_size], color='red')
axs[1].plot(df['date'][train_size:], df['close'][train_size:], color='green')
st.pyplot(fig=f)