pythonmatplotlibplotvisualizationfigure

How to activate plt.show() when creating axes without pyplot in matplotlib?


I have a script like this which when executed opens up a new window with the figure.

import matplotlib.pyplot as plt

fig = plt.Figure()
# ax = fig.add_axes(rect=(0.1, 0.0, 0.9, 1.0))  # does not open a new window
ax = fig.add_subplot()  # works
plt.show()

The problem is that when I use the add_axes function, the plt.show() call seems to do nothing at all.

The fig.savefig still seems to work, but I would like to see the figure by plt.show as well. Any ideas? Thanks


Solution

  • Use plt.figure() instead of plt.Figure(). They are different:

    import matplotlib.pyplot as plt
    
    fig = plt.figure()  # replaced Figure with figure
    ax = fig.add_axes(rect=(0.1, 0.0, 0.9, 1.0))
    ax = fig.add_subplot()  # works
    plt.show()