Mysterious Otter
Mysterious Otter

Reputation: 4029

Outputting two graphs at once using matplotlib

In short, I want to write a function that will output a scatter matrix and boxplot at one time in python. I figured I would do this by created a figure with a 2x1 plotting array. However when I run the code using a Jupyter notebook:

def boxplotscatter(data):
    f, ax = plt.subplots(2, 1, figsize = (12,6))
    ax[0] = scatter_matrix(data, alpha = 0.2, figsize = (6,6), diagonal = 'kde')
    ax[1] = data.boxplot()

I get, using data called pdf:

enter image description here

That isn't exactly what I expected -- I wanted to output the scatter matrix and below it the boxplot, not two empty grids and a boxplot embedded in a scatter matrix.

Thoughts on fixing this code?

Upvotes: 3

Views: 182

Answers (2)

josecoto
josecoto

Reputation: 752

I think you just need to pass the axis as an argument of your plotting function.

f, ax = plt.subplots(2, 1, figsize = (12,6))
def boxplotscatter(data, ax):
    ax[0] = scatter_matrix(data, alpha = 0.2, figsize = (6,6), diagonal = 'kde')
    ax[1] = data.boxplot()

Upvotes: 1

jadsq
jadsq

Reputation: 3382

The issue here is that you are not actually plotting on the created axes , you are just replacing the content of your list ax. I'm not very familiar with the object oriented interface for matplotlib but the right syntax would look more like ax[i,j].plot() rather than ax[i] = plot(). Now the only real solution I can provide is using functional interface, like that :

def boxplotscatter(data):
    f, ax = plt.subplots(2, 1, figsize = (12,6))
    plt.subplot(211)
    scatter_matrix(data, alpha = 0.2, figsize = (6,6), diagonal = 'kde')  
    plt.subplot(212)
    data.boxplot()

Upvotes: 0

Related Questions