def plot(self): if self.backend == "mpl": plt.show() elif self.backend == "ipt": iplot_mpl(self.fig) else: plot_mpl(self.fig)
def EpochRawData(raw, event_id, stimChannel, preStim=-0.2, postStim=0.2, initial_event_=True, min_duration_=.01, plot=False, reject_=None): events = mne.find_events(raw, stimChannel, 'onset', initial_event=initial_event_, consecutive=True, min_duration=min_duration_) picks = mne.pick_types(raw.info, meg='mag') if reject_ is not None: reject_dict = {'mag': reject_} else: reject_dict = None epochs = mne.Epochs(raw, events, reject=reject_dict, event_id=event_id, tmin=preStim, tmax=postStim, preload=True, baseline=None, picks=picks) if plot: fig = mne.viz.plot_events(events, raw.info['sfreq'], raw.first_samp, show=False) # convert plot to plotly update = dict(layout=dict(showlegend=True), data=[dict(name=e) for e in event_id]) iplot_mpl(plt.gcf(), update) return epochs
def plotFromMPL(): ''' take the current matplotlib plot and instead render it with plotly ''' fig = plt.gcf() # Get current figure ply.iplot_mpl(fig, show_link=False)
import plotly.graph_objs as go py.iplot(go.Data([go.Scatter(x=X, y=Y,)])) # 直接在interactive地在jupyter里显示 py.plot(fig, filename='the_graph.html') # 或者直接保存成文件 # 之后可以用加载html的方式显示出来 with open(html_path) as f: display(HTML(f.read())) # convert matplot to plotly # https://plot.ly/matplotlib/modifying-a-matplotlib-figure/ # https://plot.ly/matplotlib/ # # plot... py.iplot_mpl(plt.gcf()) # plot stacked traces = [] cum = None for sr in info_data['position']: data = info_data['position'][sr] if cum is None: cum = np.zeros(data.shape) text = data.apply(str) cum += data.values
offline.init_notebook_mode() offline.iplot([{"y": [1, 2, 1]}]) # %% import numpy as np import matplotlib.pyplot as plt from plotly import offline as py py.init_notebook_mode() t = np.linspace(0, 20, 500) plt.plot(t, np.sin(t)) py.iplot_mpl(plt.gcf()) # %% from IPython.display import JSON data = {"foo": {"bar": "baz"}, "a": 1} JSON(data) # %% import sympy as sp sp.init_printing(use_latex="mathjax") x, y, z = sp.symbols("x y z") f = sp.sin(x * y) + sp.cos(y * z) sp.integrate(f, x)
def creatSTwithPlot(self,futshare,Cal1,c1share,Cal2,c2share,Put1,p1share,Put2,p2share,showrange,up,down,customcur,risk_free_rate=0.0136): stcom = [] Cal1=int(Cal1) Cal2=int(Cal2) Put1=int(Put1) Put2=int(Put2) if futshare!=0: st = ['future',self.get_future(),0,futshare] stcom.append(st) if c1share!=0: st = ['Call',Cal1, float(self.Callless[self.Callless['履約價']==Cal1]['成交'].values[0]), self.Callless[self.Callless['履約價']==Cal1]['組合價'].values[0], c1share] stcom.append(st) if c2share!=0: st = ['Call',Cal2, float(self.Callless[self.Callless['履約價']==Cal2]['成交'].values[0]), self.Callless[self.Callless['履約價']==Cal2]['組合價'].values[0], c2share] stcom.append(st) if p1share!=0: st = ['Put', Put1, float(self.Putless[self.Putless['履約價']==Put1]['成交'].values[0]), self.Putless[self.Putless['履約價']==Put1]['組合價'].values[0], p1share] stcom.append(st) if p2share!=0: st = ['Put',Put2, float(self.Putless[self.Putless['履約價']==Put2]['成交'].values[0]), self.Putless[self.Putless['履約價']==Put2]['組合價'].values[0], p2share] stcom.append(st) print(stcom) if len(stcom)>0: lastEXP = self.lastexprice twsecurrent = self.TWSEquote() TWSErange = np.arange(lastEXP-showrange,lastEXP+showrange,10) Tcul = self.Call.dropna().TCUL.iloc[0]/255#self.Callless['TCUL'].iloc[3]/255 #risk_free_rate = self.risk_free_rate futureprofit = [] Callprofit = [] Putprofit = [] Callintimeprofit = [] Putintimeprofit = [] Calltimectrl =[] Puttimectrl = [] for s in stcom: if s[0] == 'future': futureprofit.append((TWSErange-s[1])*s[3]) if s[0] =='Call': Callprofit.append(((abs(TWSErange-s[1])+TWSErange-s[1])/2-s[2])*s[4]) IV = self.Call[self.Call['履約價']==s[1]]['隱含波動率'].values[0]/100 Callintimeprofit.append((bs_call(TWSErange,s[1],Tcul,risk_free_rate,IV)-s[2])*s[4]) Calltimectrl.append((bs_call(TWSErange,s[1],Tcul*customcur,risk_free_rate,IV)-s[2])*s[4]) if s[0] =='Put': Putprofit.append(((abs(s[1]-TWSErange)-TWSErange+s[1])/2-s[2])*s[4]) IV = self.Put[self.Put['履約價']==s[1]]['隱含波動率'].values[0]/100 Putintimeprofit.append((bs_put(TWSErange,s[1],Tcul,risk_free_rate,IV)-s[2])*s[4]) Puttimectrl.append((bs_put(TWSErange,s[1],Tcul*customcur,risk_free_rate,IV)-s[2])*s[4]) STprofit = 0 STtime = 0 customcurve = 0 if len(futureprofit)!=0: for f in futureprofit: STprofit = STprofit + f STtime = STtime + f customcurve = customcurve + f if len(Callprofit)!=0: for Call in Callprofit: STprofit = STprofit + Call if len(Putprofit)!=0: for Put in Putprofit: STprofit = STprofit + Put if len(Callintimeprofit)!=0: for Callt in Callintimeprofit: STtime = STtime + Callt if len(Putintimeprofit)!=0: for Putt in Putintimeprofit: STtime = STtime + Putt if len(Calltimectrl)!=0: for Callti in Calltimectrl: customcurve = customcurve + Callti if len(Puttimectrl)!=0: for Putti in Puttimectrl: customcurve = customcurve + Putti lastexpX = [lastEXP,lastEXP] lastexpY = [-down,up] twseX = [twsecurrent,twsecurrent] futureX = [self.get_future(),self.get_future()] twseY = [-down+40,up-40] opX = np.array([s[3] for s in stcom if s[0]!='future']).mean() optionX = [opX, opX] Xra = 300 lastexpXup = [lastEXP+Xra,lastEXP+Xra] lastexpXdown = [lastEXP-Xra,lastEXP-Xra] fig = plt.figure(figsize=(16*3/4,9*3/4)) ax = fig.add_subplot(1,1,1) ax.plot(TWSErange,np.zeros(len(TWSErange)),'gray',label='zeros') ax.plot(lastexpX,lastexpY,color=(0.2,0.2,0.7),label='last_exp') ax.plot(lastexpXup,lastexpY,color=(0.2,0.7,0.2),label='exp_up') ax.plot(lastexpXdown,lastexpY,color=(0.2,0.7,0.2),label='exp_down') ax.plot(twseX,twseY,'y',label='twse_current') ax.plot(futureX,twseY,'c',label='future_current') ax.plot(optionX,twseY,'r',label='option_current') ax.plot(TWSErange,STprofit,label='EXdcurve',color = 'k',linewidth=1.5 ) ax.plot(TWSErange,STtime,label='todaycurve',color = 'dimgray') ax.plot(TWSErange,customcurve,label='customcurve',color = (0.3,0.6,0.6)) #ax.legend() ax.set_xlim(lastEXP-showrange,lastEXP+showrange) ax.grid() ax.set_xlabel('Stock price') ax.set_ylabel('Profit (loss)') #plt.show() iplot_mpl(fig)