首页 > 解决方案 > Networkx情节在heroku中被切断

问题描述

所以我在heroku上部署了一个django应用程序。它创建一个networkx图。
这个函数基本上是绘图并返回base64图像。

    def draw(self, plot=False):
        self.compile() # creates the self.graph using list of edges
        fig, ax = plt.subplots(figsize=(6, 4.5))
        nx.draw_networkx(self.graph,
                         pos=self.pos,
                         node_color=self.node_color,
                         node_size=self.node_size,
                         edgecolors=self.edgecolors,
                         edge_color=self.edge_color,
                         width=1.5,
                         ax=ax)

        ax.set_xlim([2 * x for x in ax.get_xlim()])
        ax.set_ylim([2 * y for y in ax.get_ylim()])
        if plot:
            plt.show()
            
        buffer = BytesIO()
        plt.savefig(buffer, format='png', bbox_inches="tight")
        plt.close()
        img64 = base64.b64encode(buffer.getvalue()).decode("utf-8").replace("\n", "")
        return img64  

Html 片段

<div id="section2" class="section">
    <div id="table_div" class="row">
        <div class="col s12 center-align">
            <!--- TABLE TEMPLATE -->
            {% if image != None %}
                <div class="card-panel">
                <img id="img" src="data:image/png;base64,{{ image }}"\>
                </div>
            {% endif %}
        </div>
    </div>
</div>

我的问题是,当我在本地运行它时,图形绘制工作完全正常。
在此处输入图像描述

但是,当我将它部署到 heroku 时,我得到一个裁剪的图像
在此处输入图像描述

那么这里到底出了什么问题?(我如何确保图形不会被裁剪)

随时直接检查问题https://ongko.herokuapp.com/appdata/applet/Graph/

标签: pythonmatplotlibherokunetworkx

解决方案


事实证明这是一个graphviz问题。'neato'nx.drawing.nx_pydot.graphviz_layout(self.graph, prog='neato')由于某种原因无法正常工作,我用它替换了它,nx.drawing.layout.spring_layout(self.graph)并且事情按预期进行。


推荐阅读