首页 > 解决方案 > 在 html 页面中嵌入 matplotlib 条形图

问题描述

我正在尝试在 html 页面中嵌入 matplotlib 条形图。我几乎尝试了一切。这是我的代码-

意见.py-

def result(request)
    plt.bar(x,h)
    plt.xlabel("Personality")
    plt.ylabel("course")
    plt.show()
return render(request,'result.html')

标签: htmldjangomatplotlibembed

解决方案


您使用plt.show()用于显示图形。如果你想在 HTML 上显示这个,你可以使用plt.savefig('my_plot.png')保存图像 并获取模板上的图像路径。

   def result(request)
        plt.bar(x,h)
        plt.xlabel("Personality")
        plt.ylabel("course")
        plt.savefig('my_plot.png')
    
        return render(request,'result.html')

结果.html

<img src='my_plot.png'>

推荐阅读