【发布时间】:2020-12-20 13:43:30
【问题描述】:
我想使用streamlit实现图表的输出,有模型和初始数据,之前图表显示在Speeder、PyCharn和Colab中,但在这里它不起作用,只是显示为空,就像一张白纸.
这是它输出 localhost streamlit 的内容
def SIR(y, t, N, beta, gamma):
S, I, R = y
dSdt = -beta * S * I / N
dIdt = beta * S * I / N - gamma * I
dRdt = gamma * I
return dSdt, dIdt, dRdt
N = 1000
beta = 1.0
D = 4.0
gamma = 1.0 / D
S0, I0, R0 = 999, 1, 0
t = np.linspace(0, 49, 50)
y0 = S0, I0, R0
ret = odeint(SIR, y0, t, args=(N, beta, gamma))
S, I, R = ret.T
def plotsir(t, S, I, R):
f, ax = plt.subplots(1,1,figsize=(10,4))
ax.plot(t, S, 'b', alpha=0.7, linewidth=2, label='Susceptible')
ax.plot(t, I, 'y', alpha=0.7, linewidth=2, label='Infected')
ax.plot(t, R, 'g', alpha=0.7, linewidth=2, label='Recovered')
ax.set_xlabel('Time (days)')
ax.yaxis.set_tick_params(length=0)
ax.xaxis.set_tick_params(length=0)
ax.grid(b=True, which='major', c='w', lw=2, ls='-')
legend = ax.legend()
legend.get_frame().set_alpha(0.5)
for spine in ('top', 'right', 'bottom', 'left'):
ax.spines[spine].set_visible(False)
plt.show()
st.pyplot(plt)
进行导入:
import streamlit as st
from scipy.integrate import odeint
import numpy as np
import matplotlib.pyplot as plt
【问题讨论】:
标签: python matplotlib streamlit