AI52487963
AI52487963

Reputation: 1273

Grid of plots with lines overplotted in matplotlib

I have a dataframe that consists of a bunch of x,y data that I'd like to see in scatter form along with a line. The dataframe consists of data with its form repeated over multiple categories. The end result I'd like to see is some kind of grid of the plots, but I'm not totally sure how matplotlib handles multiple subplots of overplotted data.

Here's an example of the kind of data I'm working with:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

category = np.arange(1,10)

total_data = pd.DataFrame()

for i in category:
    x = np.arange(0,100)
    y = 2*x + 10
    data = np.random.normal(0,1,100) * y
    dataframe = pd.DataFrame({'x':x, 'y':y, 'data':data, 'category':i})

    total_data = total_data.append(dataframe)

We have x data, we have y data which is a linear model of some kind of generated dataset (the data variable).

I had been able to generate individual plots based on subsetting the master dataset, but I'd like to see them all side-by-side in a 3x3 grid in this case. However, calling the plots within the loop just overplots them all onto one single image.

Is there a good way to take the following code block and make a grid out of the category subsets? Am I overcomplicating it by doing the subset within the plot call?

plt.scatter(total_data['x'][total_data['category']==1], total_data['data'][total_data['category']==1])
plt.plot(total_data['x'][total_data['category']==1], total_data['y'][total_data['category']==1], linewidth=4, color='black')

If there's a simpler way to generate the by-category scatter plus line, I'm all for it. I don't know if seaborn has a similar or more intuitive method to use than pyplot.

Upvotes: 0

Views: 219

Answers (1)

Quang Hoang
Quang Hoang

Reputation: 150735

You can use either sns.FacetGrid or manual plt.plot. For example:

g = sns.FacetGrid(data=total_data, col='category', col_wrap=3)
g = g.map(plt.scatter, 'x','data')
g = g.map(plt.plot,'x','y', color='k');

Gives:

enter image description here

Or manual plt with groupby:

fig, axes = plt.subplots(3,3)

for (cat, data), ax in zip(total_data.groupby('category'), axes.ravel()):
    ax.scatter(data['x'], data['data'])
    ax.plot(data['x'], data['y'], color='k')

gives:

enter image description here

Upvotes: 2

Related Questions