我正在编写一个在django模板中显示可打印图形的代码。数据源应该来自熊猫数据框。
我尝试过此页面上共享的一个:Embedding a Plotly chart in a Django template
并对其进行了如下修改:
def plotly_scatter(df, _x):
x = df.iloc[:, 1:2]
y = df.iloc[:, 3:4]
trace1 = go.Scatter(x=x, y=y, marker={'color': 'red', 'symbol': 104, 'size': 10},
mode="lines", name='1st Trace')
data=go.Data([trace1])
layout=go.Layout(title="Meine Daten", xaxis={'title':'x1'}, yaxis={'title':'x2'})
figure=go.Figure(data=data,layout=layout)
div = opy.plot(figure, auto_open=False, output_type='div')
return div
已从带有下一行的视图调用此函数
try:
df = pd.read_csv(project.base_file)
context['graph'] = plotly_scatter(df, 1)
except: pass
并尝试了他的模板:
{% if graph %}
<div style="width:600;height:500">
{{ graph|safe }}
</div>
{% endif %}
这是原始输出: