Matplotlib - Django 中的 Mpld3 fig_to_html()
Matplotlib - Mpld3 fig_to_html() in Django
我正在尝试在我的浏览器中显示 mpld3 中的散点。
这是我的 views.py 片段:
plt.scatter([1, 10], [5, 9])
fig = plt.figure()
html_graph = mpld3.fig_to_html(fig)
return render(request, 'home.html', {'graph': [html_graph]})
并且在 home.html 内部:
{% for elem in graph %}
{{elem|safe}}
{% endfor %}
但我唯一看到的是控件。我也尝试过:
fig, ax = plt.subplot()
但这只显示控件和图形,没有散点。
有什么建议吗?
提前致谢
您需要先创建图形,然后绘制散点图。
fig = plt.figure()
plt.scatter([1, 10], [5, 9])
html_graph = mpld3.fig_to_html(fig)
或者,也许更好
fig, ax = plt.subplots()
ax.scatter([1, 10], [5, 9])
html_graph = mpld3.fig_to_html(fig)
因为在后一种情况下,您肯定会将散点图绘制到轴 ax
上,这是您显示的图形的一部分。
我正在尝试在我的浏览器中显示 mpld3 中的散点。
这是我的 views.py 片段:
plt.scatter([1, 10], [5, 9])
fig = plt.figure()
html_graph = mpld3.fig_to_html(fig)
return render(request, 'home.html', {'graph': [html_graph]})
并且在 home.html 内部:
{% for elem in graph %}
{{elem|safe}}
{% endfor %}
但我唯一看到的是控件。我也尝试过:
fig, ax = plt.subplot()
但这只显示控件和图形,没有散点。
有什么建议吗?
提前致谢
您需要先创建图形,然后绘制散点图。
fig = plt.figure()
plt.scatter([1, 10], [5, 9])
html_graph = mpld3.fig_to_html(fig)
或者,也许更好
fig, ax = plt.subplots()
ax.scatter([1, 10], [5, 9])
html_graph = mpld3.fig_to_html(fig)
因为在后一种情况下,您肯定会将散点图绘制到轴 ax
上,这是您显示的图形的一部分。