我正在尝试使用 Plotly 在 OHLC 蜡烛图之上覆盖标记/折线图。
代码
import plotly.graph_objects as go
import pandas as pd
from datetime import datetime
df = pd.DataFrame(
{'index': {0: 0,
1: 1,
2: 2,
3: 3,
4: 4,
5: 5,
6: 6,
7: 7,
8: 8,
9: 9,
10: 10,
11: 11,
12: 12,
13: 13,
14: 14,
15: 15,
16: 16,
17: 17,
18: 18,
19: 19,
20: 20,
21: 21,
22: 22,
23: 23,
24: 24},
'Date': {0: '2018-09-03',
1: '2018-09-04',
2: '2018-09-05',
3: …Run Code Online (Sandbox Code Playgroud) 这是我最近部署的一个简单的网络应用程序:https ://covid19-visualization-seb.herokuapp.com/
我使用了 Dash 框架并使用 Heroku 进行了部署。
我使用 df =pd.read_csv('owid-covid-data.csv')加载数据集。最新的数据集可以在这里找到:
https://covid.ourworldindata.org/data/owid-covid-data.csv
数据每天都在更新。问题是,如果我只df为链接设置变量,那么服务器启动时它只会从源获取数据一次。因此,如果我想每天保持数据最新,我就必须每天重新启动服务器,这是无稽之谈。
Dash 文档提供了页面加载更新功能,如下所示:
import datetime
import dash
import dash_html_components as html
def serve_layout():
return html.H1('The time is: ' + str(datetime.datetime.now()))
app.layout = serve_layout
if __name__ == '__main__':
app.run_server(debug=True)
Run Code Online (Sandbox Code Playgroud)
当我刷新页面时它起作用(时间自行更新)
但我想在页面加载时重新分配一个变量,不知道如何执行此操作,但出现错误。我尝试过这样的事情:
import dash
import pandas as pd
app = dash.Dash()
df = ''
def get_data():
global df
df = pd.read_csv('https://covid.ourworldindata.org/data/owid-covid-data.csv')
app.layout = get_data
if __name__ == '__main__':
app.run_server(debug=True)
Run Code Online (Sandbox Code Playgroud)
有什么帮助吗?
我有一个堆叠分组条形图,我想获取图表上每个条形的每个部分的数据标签。
我的图表如下:
stackData = {
"Leaders Now":[.52, .57, .38, .48],
"Bottom Now": [.20, .27, .19, .18],
# Use differece
"Leaders Plan": [.17, .06, .12, .16],
"Bottom Plan":[.15,.12,.09,.12],
"labels": [
"Revenue",
"Cost",
"Quality",
"Flexibility"
]
}
fig3 = go.Figure(
data=[
go.Bar(
name="Leaders Now",
x=stackData["labels"],
y=stackData["Leaders Now"],
offsetgroup=0,
marker_color = '#024a70'
),
go.Bar(
name="Leaders Plan",
x=stackData["labels"],
y=stackData["Leaders Plan"],
offsetgroup=0,
base=stackData["Leaders Now"],
marker_color = '#051c2c'
),
go.Bar(
name="Bottom Now",
x=stackData["labels"],
y=stackData["Bottom Now"],
offsetgroup=1,
marker_color = '#abe5f0'
),
go.Bar(
name="Bottom Plan",
x=stackData["labels"],
y=stackData["Bottom Plan"], …Run Code Online (Sandbox Code Playgroud) 我试图在应用程序运行时不显示默认的 dcc.graph。我只想在应用程序运行时显示我的图表
这是我的代码,
dbc.Row([
dbc.Col([
dcc.Graph(id='datatable-upload-graph', responsive=True, style={
'display': 'block'
})
], xs=10, sm=8, md=5, lg=6, xl=5)
])
Run Code Online (Sandbox Code Playgroud)
@app.callback(
Output('datatable-upload-graph', 'figure'),
Input('container-datatable', 'data')
)
def display_gantt(container_datatable):
df = pd.DataFrame(container_datatable)
df['Start Date'] = pd.to_datetime(df['Start Date'], errors='coerce')
df['End Date'] = pd.to_datetime(df['End Date'], errors='coerce')
fig = px.timeline(df, x_start="Start Date", x_end="End Date", y="Project Name", color="Status")
fig.update_yaxes(autorange="reversed")
if container_datatable is None:
return []
else:
return fig
app.config['suppress_callback_exceptions'] = True
if __name__ == '__main__':
app.run_server(debug=True, use_reloader=False)
Run Code Online (Sandbox Code Playgroud) 我有使用该shap包绘制的机器学习结果。特别是,我绘制了交互式形状力图和静态形状热图。
目前,我将形状力图保存为 html 文件,但我想将形状热图添加到交互式力图下方的 html 中。
此处显示了交互式形状力图的示例:https://shap.readthedocs.io/en/latest/example_notebooks/tabular_examples/tree_based_models/Force%20Plot%20Colors.html(下面第三张图,交互式图)
形状热图的示例如下:https ://shap.readthedocs.io/en/latest/example_notebooks/api_examples/plots/heatmap.html
我目前对这些图的编码方式是:
import shap
model = RandomForestRegressor()
explainer = shap.TreeExplainer(model)
shap_values = explainer(X)
select = range(8)
features = X.iloc[select]
features_display = X.loc[features.index]
#Create force plot and save it as html:
output_of_force_plot = shap.force_plot(explainer.expected_value, shap_values[:500,:],
X.iloc[:500,:], features_display, show=False)
file ='force_plot.html'
shap.save_html(file, output_of_force_plot)
#Create static heat map:
shap_heatmap = shap.plots.heatmap(shap_values, max_display=8, show=False)
Run Code Online (Sandbox Code Playgroud)
目前,要将其添加shap_heatmap到与我相同的html中,我已经尝试使用plotly( Plotly saving multipleplots into a single htmloutput_of_force_plot )类似问题的答案,但它不起作用:
import plotly
with open('p_graph.html', 'a') …Run Code Online (Sandbox Code Playgroud) 我有以下pandas数据框:
df_dict = {'index': [15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,
26, 27, 28, 29, 30, 31, 32, 33, 34],
'columns': ['cluster', 'week', 'color_line'],
'data': [[3, 1, 'green'],
[3, 2, 'green'],
[3, 3, 'green'],
[3, 4, 'green'],
[3, 5, 'green'],
[3, 6, 'green'],
[4, 7, 'green'],
[3, 8, 'yellow'],
[3, 9, 'yellow'],
[4, 10, 'yellow'],
[3, 11, 'green'],
[3, 12, 'yellow'],
[3, 13, 'yellow'],
[4, 14, 'yellow'],
[4, 15, 'red'],
[4, 16, ' orange'], …Run Code Online (Sandbox Code Playgroud) 我创建了一个 Python Dash 仪表板来交互式显示敏感的客户数据。我在笔记本电脑上通过本地主机使用仪表板。我正在初始化并运行应用程序,如下所示:
app = dash.Dash(__name__)
if __name__ == "__main__":
app.run_server(debug = True)
Run Code Online (Sandbox Code Playgroud)
仪表板的数据是否可以被其他人访问或者只能从我的机器访问?
如果你\xe2\x80\x99和我一样,你喜欢Plotly Express,但是当你遇到Express返回的数字可以\xe2\x80\x99t利用\xe2\x80\x98make_subplots()\xe2\x80的问题时感到沮丧\x99 因为 make_subplots 接受的是痕迹而不是数字。在这篇文章中,我\xe2\x80\x99d想分享我自己的解决方案,关于如何仅使用Plotly Express(和plotly.subplots)创建包含两种不同类型的图形(如下所示)的子图
\n\n我在 Jupyter Notebook 中本地创建了绘图图表,我试图将其另存为 png 文件在本地计算机上。图表示例:
for table in EG_SD_pivots:
df = EG_SD_pivots[table].reset_index()
fig = make_subplots(specs=[[{"secondary_y": True}]])
fig.add_trace(
go.Scatter(x=df.iloc[:, 0], y=df['SD_Credibility'], name="Credibility", marker=dict(color=yellow)), secondary_y=True)
fig.add_trace(
go.Bar(x=df.iloc[:, 0], y=df['EG_LR_Rel'], name="EG Relativity", marker=dict(color=teal)), secondary_y=False)
fig.add_trace(
go.Bar(x=df.iloc[: ,0], y=df['SD_LR_Rel'], name="SD Relativity", marker=dict(color=indigo)), secondary_y=False)
# Add figure title
fig.update_layout(title_text=df.iloc[:0].columns[0], margin=dict(b=200))
# Set x-axis title
fig.update_xaxes(title_text=df.iloc[:0].columns[0])
fig.update_yaxes(title_text="<b>Relativity</b>", secondary_y=False)
fig.update_yaxes(title_text="<b>Credibility</b>", secondary_y=True)
fig.show()
Run Code Online (Sandbox Code Playgroud)
我尝试了许多不同的解决方案,但它们都返回了各种错误...解决方案:
fig.to_image(format="png")-->返回
ValueError:
The orca executable is required to export figures as static images,
but it could not be found on the system …Run Code Online (Sandbox Code Playgroud) 我想编辑数据,可能会向图表添加更多痕迹。我找到了一种将 html 文件显示为图表的方法,但不对其进行编辑。
from IPython.display import HTML
HTML(filename='file_name.html')
Run Code Online (Sandbox Code Playgroud)