Python Forum
matplotlib, no plot when using Figure
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
matplotlib, no plot when using Figure
#1
Hello,
as I am breaking by brains about this, I hope you can give me a suggestion.
I work on Linux / openSUSE 15.3 and python 3.9.4 in a virtual console.
I can not understand why this is not providing a plot output

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

fig = Figure(figsize=(5, 4), dpi=100)           # Create a in memory figure
ax  = fig.add_subplot(111)                      # Create a axes in fig
ax.plot([1, 2, 3, 4], [1, 4, 2, 3])
plt.show()                                      # Show in X
and this does

fig, ax = plt.subplots()               
ax.plot([1, 2, 3, 4], [1, 4, 2, 3])
plt.show() 
Thanks, Anthony
Reply
#2
import matplotlib.pyplot as plt

fig = plt.figure()
ax  = fig.add_subplot(1, 1, 1)  # or 111
ax.plot([1, 2, 3, 4], [1, 4, 2, 3])
plt.show()
Why does this work for plt.figure() but not matplotlib.figure.Figure()? So I wrote this program and inspect a and b.
import matplotlib.pyplot as plt
from matplotlib.figure import Figure

a = plt.figure()
b = Figure()
plt.show()  # Just needed a line to set a breakpoint
a and b look similar except for "canvas"
a.canvas =canvas:<matplotlib.backends.backend_tkagg.FigureCanvasTkAgg object at 0x000001D8E2594D30>
b.canvas = canvas:<matplotlib.backend_bases.FigureCanvasBase object at 0x000001D8E5DB1D00>

So it appears that matplotlib.pyplot.figure creates a matplotlib.figure.Figure() and sets the canvas to a FigureCanvasTkAgg type.

For more information about agg backend you can look here:

https://matplotlib.org/stable/gallery/us...asagg.html
Reply
#3
Dear DeanHystad,

you are absolutely right with your answer.
I checked it in the the source code.
b = Figure() : is a figure in memory only
a = plt.figure() : is a same figure but linked to a canvas (screen frame buffer)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Graphic line plot with matplotlib, text file in pytho khadija 2 1,375 Aug-15-2022, 12:00 PM
Last Post: khadija
  plot on the same figure using a for loo with matplotlib drSlump 2 1,700 Oct-13-2021, 07:11 AM
Last Post: drSlump
  Matplotlib: How do I convert Dates from Excel to use in Matplotlib JaneTan 1 3,214 Mar-11-2021, 10:52 AM
Last Post: buran
  Python Matplotlib: How do I plot lines with different end point in the same graph? JaneTan 0 1,575 Feb-28-2021, 11:56 AM
Last Post: JaneTan
  How to plot intraday data of several days in one plot mistermister 3 2,896 Dec-15-2020, 07:43 PM
Last Post: deanhystad
  saving only one line of a figure as an image (python matplotlib) nitrochloric 0 2,011 Nov-23-2020, 01:41 PM
Last Post: nitrochloric
  Difference Between Figure Axis and Sub Plot Axis in MatplotLib JoeDainton123 2 2,460 Aug-21-2020, 10:17 PM
Last Post: JoeDainton123
  Understanding The Arguments for matplotlib.pyplot.plot JoeDainton123 0 1,921 Aug-19-2020, 01:19 AM
Last Post: JoeDainton123
  How to plot gantt chart using matplotlib Mekala 1 2,627 Jul-16-2020, 07:21 PM
Last Post: Marbelous
  How can I make a plot representing connection relationship with python matplotlib? Berlintofind 1 1,942 May-08-2020, 09:27 PM
Last Post: jefsummers

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020