Skip to content

figure created using figure.Figure doesn't save figure #12447

Closed

Description

@abbazs

Figure object created using "from matplotlib import figure" is not able to save figure.

Code for reproduction

import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
from matplotlib import figure

def figure_bug(file):
    fig = figure.Figure(figsize=(2,5), constrained_layout=True)
    gs = gridspec.GridSpec(5, 1, figure=fig)
    axs = [fig.add_subplot(gs[x]) for x in range(1, 5)] 
    fig.text(0.5, 0.5, file, fontsize=25)
    fig.savefig(file)
    plt.close(fig)

if __name__ == '__main__':
    for i in range(0, 110):
        file_name = f'{i}.png'
        figure_bug(file_name)
        print(f'Done {i}')

Actual outcome

AttributeError Traceback (most recent call last)
D:\...\img_test\layou_test.py in <module>()
     14     for i in range(0, 110):
     15         file_name = f'{i}.png'
---> 16         figure_bug(file_name)
     17         print(f'Done {i}')

D:\....\img_test\layou_test.py in gridspec_bug(file)
      8     axs = [fig.add_subplot(gs[x]) for x in range(1, 5)]
      9     fig.text(0.5, 0.5, file, fontsize=25)
---> 10     fig.savefig(file)
     11     plt.close(fig)
     12

C:\....\matplotlib\figure.py in savefig(self, fname, frameon, transparent, **kwargs)
   2095             self.set_frameon(frameon)
   2096
-> 2097         self.canvas.print_figure(fname, **kwargs)
   2098
   2099         if frameon:

AttributeError: 'NoneType' object has no attribute 'print_figure'

Expected outcome
Saved image

Matplotlib version

  • Operating system: Windows
  • Matplotlib version: 3.0.0
  • Matplotlib backend (print(matplotlib.get_backend())): Qt5Agg
  • Python version: 3.6.5
  • Jupyter version (if applicable): NA
  • Other libraries: NA

Activity

  1. anntzer commented on Oct 8, 2018

    @anntzer
    Contributor

    (I think we should perhaps attach a FigureCanvasBase by default to Figures...).

  2. WeatherGod commented on Oct 8, 2018

    @WeatherGod
    Member
  3. anntzer commented on Oct 8, 2018

    @anntzer
    Contributor

    Eh, that won't be easy.

    Why? It's literally self.canvas = FigureCanvasBase() instead of self.canvas = None...

    And still won't be very useful in the end.

    Well, then you'd get to save these figures...

  4. abbazs commented on Oct 8, 2018

    @abbazs
    Author

    I'm creating several images in loop and the memory keeps increasing and one solution given in so was to create figure from figure.Figure instead of plt.figure

    Eh, that won't be easy. And still won't be very useful in the end. Can I ask why you are instantiating the Figure object directly? Usually, the only time one does that is when embedding in a GUI widget and in those situations, you don't instantiate the baseclass, you instantiate the GUI-specific Figure class.
    …
    On Mon, Oct 8, 2018 at 2:11 PM Antony Lee @.***> wrote: (I think we should perhaps attach a FigureCanvasBase by default to Figures...). — You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub <#12447 (comment)>, or mute the thread https://github.com/notifications/unsubscribe-auth/AARy-MKi-Z1TE5cOuhZkMm0OMyV2AKhFks5ui5VFgaJpZM4XNagC .

  5. WeatherGod commented on Oct 8, 2018

    @WeatherGod
    Member
  6. jklymak commented on Oct 9, 2018

    @jklymak
    Member

    What sticks around after figure.close? We should plug that shouldn't we?

  7. WeatherGod commented on Oct 9, 2018

    @WeatherGod
    Member
  8. anntzer commented on Oct 9, 2018

    @anntzer
    Contributor

    There isn't actually a fig.close() method... (that makes sense as "closing" is a pyplot concept, not a figure concept)
    There's plt.close(fig) and that does the right thing.

  9. jklymak commented on Oct 9, 2018

    @jklymak
    Member

    Indeed. But OTOH:

    import os
    import psutil
    import matplotlib.pyplot as plt
    
    while True:
      fig, ax = plt.subplots()
      ax.plot(range(10))
      process = psutil.Process(os.getpid())
      print(process.memory_info().rss)
      plt.close(fig)
      del(fig)

    shouldn't keep growing memory use ad-infinitum, should it?

  10. anntzer commented on Oct 9, 2018

    @anntzer
    Contributor

    That's almost certainly #9141 (this only appears to leak when using qt, not with other interactive or noninteractive backends).

  11. jklymak commented on Oct 9, 2018

    @jklymak
    Member

    OK, agreed - with Agg memory use grows a tiny bit, but very very slowly.

  12. rudrathegreat commented on Nov 8, 2018

    @rudrathegreat

    Another way I found which works (requirements below) -

    fig, ax = plt.subplots(figsize=(12, 6))  # create matplotlib figure and axes

    And then, if you can, be able to save this to your folder -

    plt.savefig('Images/image.png')

    And possibly even save it using scipy.io.wavfile.write -

    write('Audio/audio.wav', RATE, data)

    Requirements

    Python 3.7

    Matplotlib 2.2.3

    Scipy 1.15.0 (optional)

    NumPy 1.10.0 (recommendation)

    I hope that helped!

  13. anntzer commented on Jan 14, 2020

    @anntzer
    Contributor

    Closed by #12450 (AFAICT).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions