I currently have my problem solved using the code block below. It does what I want, however there is a lot of code duplication and it is a bit hard to read.
I have several figures I would like to create and populate with data that is calculated in a large for loop.
I am having trouble figuring out the syntax to create and set the titles/meta data at the top of my code, and then add all the proper data to the correct figures in the bottom of my code.
I have this:
import matplotlib.pyplot as plt
import numpy as np
figure = plt.figure()
plt.title("Figure 1")
figure.add_subplot(2,2,1)
plt.imshow(np.zeros((2,2)))
# Some logic in a for loop to add subplots
plt.show()
figure = plt.figure()
plt.title("Figure 2")
figure.add_subplot(2,2,1)
# Some Logic in an identical for loop to add different subplots
plt.imshow(np.zeros((2,2)))
plt.show()
I want something that looks more like this:
# Define variables, titles, formatting, etc.
figure = plt.figure()
figure2 = plt.figure()
figure1.title = "Figure 1"
figure2.title = "Figure 2"
# Populate
figure.add_subplot(2,2,1)
figure2.add_subplot(2,2,1)
# Some logic in a for loop to add subplots to both figures
Is there a clean way to do what I am asking with matplotlib? I am mostly looking to cleanup my code a bit and have a program that is easier to expand and maintain.
I really just want a way to define all my figures and there titles in one place and then add images to the correct figure depending on some other logic. Being able to call plt.show() for a specific figure would also be good.