如何在同一个HTML页面中对所有绘图图进行脱机绘图?

2024-04-19 13:28:22 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用jupyter-notebook制作一个python脚本,它从我的网站sql-server获取数据,我想在每次页面加载时使用javascript函数调用这个脚本。所以页面上会有绘图仪。 这是我的代码:

# coding: utf-8

# In[1]:


#import os
#os.chdir("D:/datasets/Trell")


# In[2]:


import json
from pandas.io.json import json_normalize
import pandas as pd
import numpy as np
import warnings
warnings.filterwarnings('ignore')

from plotly.offline import init_notebook_mode,plot, iplot
init_notebook_mode(connected=True)
import plotly.graph_objs as go
import plotly.offline as offline
offline.init_notebook_mode()
import plotly.tools as tls



# In[3]:


# importing the requests library
import requests

# api-endpoint
URL = "https://*****.co.in/*****/*******.php"

# location given here
token= '************'

query= 'SELECT userId,createdAt,userName,trails_count,bio FROM users WHERE createdAt >= "2018-07-01"'

# defining a params dict for the parameters to be sent to the API
PARAMS = {'token':token, 'query':query}

# sending get request and saving the response as response object
r = requests.post(url = URL, data = PARAMS)


# In[4]:


data=r.json()


# In[5]:


df=pd.DataFrame(data)


# In[6]:


df.head(1)


# In[7]:


df['date'] = pd.DatetimeIndex(df.createdAt).normalize()


# In[8]:


df['user']=1


# In[9]:


df_user=df.groupby(['date'],as_index=False)['user'].agg('sum')


# In[10]:


data = [go.Scatter( x=df_user['date'], y=df_user['user'] )]
plot(data, filename='time-series.')


# In[11]:


df_user['day_of_week']=df_user['date'].dt.weekday_name
df_newuser_day=df_user.groupby(['day_of_week'],as_index=False)['user'].agg('sum')
df_newuser_day=df_newuser_day.sort_values(['user'],ascending=False)

trace = go.Bar(
    x=df_newuser_day['day_of_week'],
    y=df_newuser_day.user,
    marker=dict(
        color="blue",
        #colorscale = 'Blues',
        reversescale = True
    ),
)

layout = go.Layout(
    title='Days of Week on which max. users register (July)'
)

data = [trace]
fig = go.Figure(data=data, layout=layout)
plot(fig, filename="medal.")

但问题是,每次执行plot()函数时,新的HTML选项卡都会打开,其中提到的filename=。在

我只想当我执行文件时,所有的图都在一个HTML页下,而且我想在每个绘图之前给标题加上<h1>标记,这样绘图就可以理解了。那么有没有一种方法可以做到这一点,同时在plotly图之前添加一些HTMlCSS标记,这样它看起来就像一个干净的网页,上面有所有的plotly图,以及<h1>标签下提到的标题。在

就像我希望所有的图表一个接一个出现在同一个页面上。在

另外,我不想使用iplot,因为它只在同一个笔记本中绘图,并且不保存文件。在


Tags: theinimportjsongodfdataplot
2条回答

迟答:子批次可能是这个问题的答案。在

例如,要创建2行2列的子批

from plotly import tools
plots = tools.make_subplots(rows=2, cols=2, print_grid=True)

要使绘图显示在同一页中,请使用plotly offlineiplot方法,而不是{}。在

所以声明。在

plot(fig, filename="medal.")

会变成。在

^{pr2}$

如果希望在绘图之前添加HTML,请使用ipython提供的display和{}。在

from IPython.core.display import display, HTML
display(HTML('<h1>Hello, world!</h1>'))
iplot(fig)

因此,首先我们可以先插入html,然后绘制图形!在

要了解更多信息,请访问此^{}

相关问题 更多 >