【问题标题】:Networkx plot gets cut off in herokuNetworkx情节在heroku中被切断
【发布时间】:2021-10-14 19:02:38
【问题描述】:

所以我在 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 sn-p

<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/

【问题讨论】:

    标签: python matplotlib heroku networkx


    【解决方案1】:

    原来是一个graphviz问题。 'neato' nx.drawing.nx_pydot.graphviz_layout(self.graph, prog='neato') 出于某种原因无法正常工作,我将其替换为 nx.drawing.layout.spring_layout(self.graph) 并且一切正常。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-31
      • 1970-01-01
      • 2020-10-04
      • 1970-01-01
      相关资源
      最近更新 更多