Interactive plot with cufflinks,plotly, iplot and ipywidgets

  AI, Data Visualization, Numpy, Pandas, Plotly, Python

import pandas as pd
import numpy as np
# model
from lmfit import Minimizer, Parameters, report_fit
#plot
import chart_studio.plotly as py
import ipywidgets as widgets
from ipywidgets import interact, interact_manual
import cufflinks as cf

theCountry='Canada'
threshhold=10
theData=confirmed_series_21[confirmed_series_21[theCountry]>threshhold] 

data=theData[theCountry]
start_date= data.index[0]
end_date= data.index[-1]                           
dateData=pd.date_range(start=start_date,end=end_date)
forecastDays=60

dateForecast= pd.date_range(start=end_date,periods=forecastDays+1)[1:] 
dateObsForecast=dateData.append(dateForecast)
#dateObsForecast

                         
# define objective function: returns the array to be minimized
  # normalize case data
last=data[-1]
data=data/last
  # set x values interval = 1day
dataLen=data.count()
x = np.linspace(1, dataLen, dataLen)

# create a set of Parameters
params = Parameters()
params.add('amp', value=1)
params.add('scale', value=1, min=0.1)
params.add('loc', value=70, min=0)
params.add('decaybase',value=1, min=0.85,max=1.005)  # shparly increase value>1

# do fit, here with the default leastsq algorithm
minner = Minimizer(Cauchy_cumulative_hazard_residual, params, fcn_args=(x, data))
result = minner.minimize()

# calculate final result
forecastdays=forecastDays
final = data + result.residual
x1=np.linspace(1,dataLen+forecastdays,dataLen+forecastdays)
scale=result.params['scale'].value
amp=result.params['amp'].value
loc=result.params['loc'].value
decaybase=result.params['decaybase'].value

y1=amp*Cauchy_cumulative_hazard(x1,loc,scale,decaybase)  # forecast
nn=np.int(np.ceil(loc))
y1[:nn]=final.iloc[:nn] # replace data before

# write error report
report_fit(result)
fig0=plt.figure(figsize=(15,9))
plt.plot(x, data*last, 'k+')
plt.plot(x1,y1*last,'r-')
plt.plot(x, final*last, 'b-')
fig0.show()

output = pd.DataFrame({'date' : [],'Forecast':[],'Cases': [],'Fitting':[],'Increase':[]})
output

output['date']=dateObsForecast
output['Forecast']=y1*last
output['Cases'].iloc[:dataLen]=data.values*last
output['Fitting'].iloc[:dataLen]=final.values*last
output['Increase'].iloc[1:]=(y1[1:]-y1[:-1])*last
output=output.set_index('date')

@interact
def plot_ProjectedCanadaCOVID19():
    fig=output.iplot(asFigure=True,
                 mode='lines+markers',
                 size=6,secondary_y = 'Increase',
                 secondary_y_title='Increase',
                 xTitle='Date',
                 yTitle='Cases',
                 title='Projected COVID-19 Cases in Canada',
                 theme='solar',
                 filename='COVID19-Canada',
                 sharing='public')
    fig.show()
    url=py.iplot(fig, filename='COVID19-Canada')
    print(url)