1

Consider the following sample data frame:

rng = pd.date_range('1/1/2011', periods=72, freq='H')
df = pd.DataFrame({
        'cat': list('ABCD'*int(len(rng)/4)),
        'D1': np.random.randn(72),
        'D2': np.random.randn(72),
        'D3': np.random.randn(72),
        'D4': np.random.randn(72)
    }, index=rng)

I'm looking for an idiomatic way to scatter-plot this as following:

  1. 4 subplots (tiles), one for each category (A, B, C, or D)
  2. each D series plotted in its own color

I can do this with a bunch of filtering and for-loops, but I'm looking for a more compact pandas-like way.

smci
  • 32,567
  • 20
  • 113
  • 146
Dmitry B.
  • 9,107
  • 3
  • 43
  • 64

1 Answers1

1

This is my guess at what you want.

fig, axes = plt.subplots(2, 2, figsize=(8, 6), sharex=True, sharey=True)

for i, (cat, g) in enumerate(df.groupby('cat')):
    ax = axes[i // 2, i % 2]
    for j, c in g.filter(like='D').iteritems():
        c.plot(ax=ax, title=cat, label=j, style='o')
    ax.legend(loc='best', fontsize=8)

fig.tight_layout()

enter image description here

piRSquared
  • 285,575
  • 57
  • 475
  • 624
  • Yes, this the outcome I want and it's is very similar to the code I have with nested for-loops and filtering (as I mentioned in my question). Is this the most minimalist one can go with pandas? Can `plot`'s `by` argument be leveraged for multi-tiles? Can `scatter` be made recognize columns as series? – Dmitry B. Mar 26 '17 at 16:35