【问题标题】:Matplotlib - Mpld3 fig_to_html() in DjangoMatplotlib - Django 中的 Mpld3 fig_to_html()
【发布时间】:2017-09-17 14:32:10
【问题描述】:

我正在尝试在浏览器中显示 mpld3 中的分散点。

这是我的意见.py sn-p:

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()

但这仅显示控件和图形,没有散点。

有什么建议吗?

提前致谢

【问题讨论】:

    标签: django matplotlib mpld3


    【解决方案1】:

    您需要先创建图形,然后绘制散点图。

    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,这是您显示的图形的一部分。

    【讨论】:

    • 我每隔一段时间就会收到一个“主线程不在主循环”异常,你知道这是什么或者它是否与这种方法有关?
    • 不,我不知道这是从哪里来的。这里不应该依赖这段代码,而且“偶尔”听起来真的不像是代码本身的问题。
    • 我正在尝试导入 matplotlib 以在我的应用程序中运行此代码,就像 import matplotlib.pyplot as plt, mpld3 但它说 ModuleNotFoundError: No module named 'mpld3',我该如何解决这个问题?我已经安装了 matplotlib
    • @Rahul 如果你想使用mpld3你需要安装它。
    • @ImportanceOfBeingErnest 我做到了,它就像一个魅力,但你能帮我做一件事吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多