20

I've been using matplotlib for five months now on a daily basis, and I still find creation of new figures confusing.

Usually I create a figure with 2x2 subplots using, for example, somthing like:

import matplotlib.pyplot as plt
import itertools as it
fig,axes = plt.subplots(2,2)
axit = (ax for ax in it.chain(*axes))
for each of four data series I want to plot:
    ax = next(axit)
    ax.plot(...)

The question I have now is: how can operate completely independently of pyplot, ie, how can I create a figure, populate it with plots, make style changes, and only tell that figure to appear at the exact moment I want it to appear. Here is what I am having trouble with:

import matplotlib as mpl
gs = gridspec.GridSpec(2,2)
fig = mpl.figure.Figure()
ax1 = fig.add_subplot(gs[0])
ax1.plot([1,2,3])
ax2 = fig.add_subplot(gs[1])
ax2.plot([3,2,1])

After running the above, the only thing that comes to mind would be to use:

plt.draw()

But this does not work. What is missing to make the figure with the plots appear? Also, is

fig = mpl.figure.Figure()

all I have to do to create the figure without pyplot?

3
  • Why don't you want to use pyplot? Commented Jun 12, 2015 at 18:18
  • I'm using Tkinter to embed matplotlib in a GUI. I've gotten this up and running after having seen some code to do it in a book and also online. The way they embed a plot in the GUI doesn't use pyplot. Now I'm trying to add some new features, like instead of just a simple plot, using multiple plots with gridspec. Commented Jun 12, 2015 at 18:52
  • This is an example on how to embed a figure in Tk: matplotlib.org/examples/user_interfaces/embedding_in_tk.html Commented Jun 12, 2015 at 20:08

2 Answers 2

3

This works for me without matplotlib.pyplot

import sys
from PyQt5 import QtWidgets
from matplotlib.backends.backend_qt5agg import (
    FigureCanvasQTAgg as FigureCanvas)
from matplotlib.figure import Figure
import numpy as np

fig=Figure()
canvas=FigureCanvas(fig)
ax=canvas.figure.add_subplot(111)

x=np.arange(-5,5,0.1)
y=np.sin(x)

ax.plot(x,y)
canvas.show()

app=QtWidgets.QApplication(sys.argv)
app.exec()

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

2

You could attach a suitable backend to your figure manually and then show it:

from matplotlib.backends import backend_qt4agg  # e.g.
backend_qt4agg.new_figure_manager_given_figure(1, fig)
fig.show()

... but why not use pyplot?

6 Comments

I'm using Tkinter to embed matplotlib in a GUI. I've gotten this up and running after having seen some code to do it in a book and also online. The way they embed a plot in the GUI doesn't use pyplot. Now I'm trying to add some new features, like instead of just a simple plot, using multiple plots with gridspec.
But just so I understand, I've read the following from docs: "matplotlib can target different outputs, and each of these capabilities is called a backend" "There are two types of backends: user interface backends (for use in pygtk, wxpython, tkinter, qt4, or macosx; also referred to as “interactive backends”) and hardcopy backends to make image files (PNG, SVG, PDF, PS; also referred to as “non-interactive backends”)."
"To make things a little more customizable for graphical user interfaces, matplotlib separates the concept of the renderer (the thing that actually does the drawing) from the canvas (the place where the drawing goes)." "The canonical renderer for user interfaces is Agg" So, as I understood, you are using backend Qt4agg, which is "Agg rendering to a Qt4 canvas". Tkinter uses Tkagg, which is "Agg rendering to a Tk canvas". From the Artists Tutorial:
"There are three layers to the matplotlib API. The matplotlib.backend_bases.FigureCanvas is the area onto which the figure is drawn, the matplotlib.backend_bases.Renderer is the object which knows how to draw on the FigureCanvas, and the matplotlib.artist.Artist is the object that knows how to use a renderer to paint onto the canvas."
So just to see if I understood: when you don't use pyplot, you have to manually connect the artist to the backend (which is a renderer+canvas). And that is what you did in the line: backend_qt4agg.new_figure_manager_given_figure(1, fig) is that right?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.