• <small id='z4sls'></small><noframes id='z4sls'>

  • <legend id='z4sls'><style id='z4sls'><dir id='z4sls'><q id='z4sls'></q></dir></style></legend>
  • <i id='z4sls'><tr id='z4sls'><dt id='z4sls'><q id='z4sls'><span id='z4sls'><b id='z4sls'><form id='z4sls'><ins id='z4sls'></ins><ul id='z4sls'></ul><sub id='z4sls'></sub></form><legend id='z4sls'></legend><bdo id='z4sls'><pre id='z4sls'><center id='z4sls'></center></pre></bdo></b><th id='z4sls'></th></span></q></dt></tr></i><div id='z4sls'><tfoot id='z4sls'></tfoot><dl id='z4sls'><fieldset id='z4sls'></fieldset></dl></div>

    <tfoot id='z4sls'></tfoot>
      • <bdo id='z4sls'></bdo><ul id='z4sls'></ul>

      1. 在 NetworkX 中无法将图形保存为 jpg 或 png 文件

        in NetworkX cannot save a graph as jpg or png file(在 NetworkX 中无法将图形保存为 jpg 或 png 文件)
          <tbody id='SpZ2a'></tbody>
        1. <legend id='SpZ2a'><style id='SpZ2a'><dir id='SpZ2a'><q id='SpZ2a'></q></dir></style></legend>

        2. <small id='SpZ2a'></small><noframes id='SpZ2a'>

        3. <i id='SpZ2a'><tr id='SpZ2a'><dt id='SpZ2a'><q id='SpZ2a'><span id='SpZ2a'><b id='SpZ2a'><form id='SpZ2a'><ins id='SpZ2a'></ins><ul id='SpZ2a'></ul><sub id='SpZ2a'></sub></form><legend id='SpZ2a'></legend><bdo id='SpZ2a'><pre id='SpZ2a'><center id='SpZ2a'></center></pre></bdo></b><th id='SpZ2a'></th></span></q></dt></tr></i><div id='SpZ2a'><tfoot id='SpZ2a'></tfoot><dl id='SpZ2a'><fieldset id='SpZ2a'></fieldset></dl></div>
          • <bdo id='SpZ2a'></bdo><ul id='SpZ2a'></ul>

              <tfoot id='SpZ2a'></tfoot>
                • 本文介绍了在 NetworkX 中无法将图形保存为 jpg 或 png 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我在 NetworkX 中有一个包含一些信息的图表.图表显示后,我想将其保存为 jpgpng 文件.我使用了 matplotlib 函数 savefig 但是当图像被保存时,它不包含任何东西.它只是一个白色的图像.

                  I have a graph in NetworkX containing some info. After the graph is shown, I want to save it as jpg or png file. I used the matplotlib function savefig but when the image is saved, it does not contain anything. It is just a white image.

                  这是我写的示例代码:

                  import networkx as nx
                  import matplotlib.pyplot as plt
                  
                  fig = plt.figure(figsize=(12,12))
                  ax = plt.subplot(111)
                  ax.set_title('Graph - Shapes', fontsize=10)
                  
                  G = nx.DiGraph()
                  G.add_node('shape1', level=1)
                  G.add_node('shape2', level=2)
                  G.add_node('shape3', level=2)
                  G.add_node('shape4', level=3)
                  G.add_edge('shape1', 'shape2')
                  G.add_edge('shape1', 'shape3')
                  G.add_edge('shape3', 'shape4')
                  pos = nx.spring_layout(G)
                  nx.draw(G, pos, node_size=1500, node_color='yellow', font_size=8, font_weight='bold')
                  
                  plt.tight_layout()
                  plt.show()
                  plt.savefig("Graph.png", format="PNG")
                  

                  为什么保存的图片里面没有任何东西(只有白色)?

                  Why is the image saved without anything inside (just white) ?

                  这是保存的图像(只是空白):

                  This is the image saved (just blank):

                  推荐答案

                  plt.show方法有关.

                  show方法的帮助:

                  def show(*args, **kw):
                      """
                      Display a figure.
                  
                      When running in ipython with its pylab mode, display all
                      figures and return to the ipython prompt.
                  
                      In non-interactive mode, display all figures and block until
                      the figures have been closed; in interactive mode it has no
                      effect unless figures were created prior to a change from
                      non-interactive to interactive mode (not recommended).  In
                      that case it displays the figures but does not block.
                  
                      A single experimental keyword argument, *block*, may be
                      set to True or False to override the blocking behavior
                      described above.
                      """
                  

                  当您在脚本中调用 plt.show() 时,似乎文件对象仍处于打开状态,而 plt.savefig 写入方法无法读取那个流完全.但是 plt.show 有一个 block 选项可以改变这种行为,所以你可以使用它:

                  When you call plt.show() in your script, it seems something like file object is still open, and plt.savefig method for writing can not read from that stream completely. but there is a block option for plt.show that can change this behavior, so you can use it:

                  plt.show(block=False)
                  plt.savefig("Graph.png", format="PNG")
                  

                  或者只是评论它:

                  # plt.show()
                  plt.savefig("Graph.png", format="PNG")
                  

                  或者在展示之前保存:

                  plt.savefig("Graph.png", format="PNG")
                  plt.show()
                  

                  演示:这里

                  这篇关于在 NetworkX 中无法将图形保存为 jpg 或 png 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Running .jl file from R or Python(从 R 或 Python 运行 .jl 文件)
                  Running Julia .jl file in python(在 python 中运行 Julia .jl 文件)
                  Using PIP in a Azure WebApp(在 Azure WebApp 中使用 PIP)
                  How to run python3.7 based flask web api on azure(如何在 azure 上运行基于 python3.7 的烧瓶 web api)
                  Azure Python Web App Internal Server Error(Azure Python Web 应用程序内部服务器错误)
                  Run python dlib library on azure app service(在 azure app 服务上运行 python dlib 库)

                    <tbody id='lWguJ'></tbody>

                      1. <small id='lWguJ'></small><noframes id='lWguJ'>

                      2. <i id='lWguJ'><tr id='lWguJ'><dt id='lWguJ'><q id='lWguJ'><span id='lWguJ'><b id='lWguJ'><form id='lWguJ'><ins id='lWguJ'></ins><ul id='lWguJ'></ul><sub id='lWguJ'></sub></form><legend id='lWguJ'></legend><bdo id='lWguJ'><pre id='lWguJ'><center id='lWguJ'></center></pre></bdo></b><th id='lWguJ'></th></span></q></dt></tr></i><div id='lWguJ'><tfoot id='lWguJ'></tfoot><dl id='lWguJ'><fieldset id='lWguJ'></fieldset></dl></div>
                        • <bdo id='lWguJ'></bdo><ul id='lWguJ'></ul>
                          <tfoot id='lWguJ'></tfoot>
                          <legend id='lWguJ'><style id='lWguJ'><dir id='lWguJ'><q id='lWguJ'></q></dir></style></legend>