G.Hanna
G.Hanna

Reputation: 45

How to stack multiple graphs

I have a function that takes a dictionary (key: string, values: 2D numbers) and displays the values of the dictionary in a graph (one color per key).

I would like to use this function directly to display in the same page four graphs, each graph corresponding to a particular dictionary

if possible I would like to have a script like :

def my_function(dico):
    display picture

def global_function(multiple):
    plt.subplot(1,2,1)
    my_function(multiple[0])
    plt.subplot(1,2,2)
    my_function(multiple[1])

Upvotes: 1

Views: 51

Answers (1)

phisquared
phisquared

Reputation: 57

You can plot these graphs as subplots to one figure.

import numpy as np
import matplotlib.pyplot as plt


#plotting function
def plot_2_subplots (x1, y1, x2, y2):
    f, (ax1, ax2) = plt.subplots(1, 2, sharey=True)
    ax1.plot(x1, y1)
    ax1.set_title('Plot 1')
    ax2.plot(x2, y2)
    ax2.set_title('Plot 2')

lists_a = sorted(d.items()) # return a list of tuples (sorted by key)
x1, y1 = zip(*lists_a) # unpack into tuple

lists_b = sorted(d.items()) # return a list of tuples (sorted by key)
x2, y2 = zip(*lists_b) # unpack into tuple

#Call plotting function
plot_2_subplots(x1, y1, x2, y2)

This generates this figure:enter image description here

Upvotes: 2

Related Questions