1

I have code that generates four subplots, but i want to generate those charts through loops , Currently i am following this piece of code to generate the chart Code:

plt.figure(figsize=(20, 12))
plt.subplot(221)
sns.barplot(x = 'Category', y = 'POG_Added', data = df)
xticks(rotation = 90)
plt.xticks(size = 11)
plt.yticks(size = 11)
plt.xlabel("Category",size = 13)
plt.ylabel("POG_Added",size = 13)

plt.subplot(222)
sns.barplot(x = 'Category', y = 'Live_POG', data = df)
xticks(rotation = 90)
plt.xticks(size = 11)
plt.yticks(size = 11)
plt.xlabel("Category",size = 13)
plt.ylabel("Live_POG",size = 13)

plt.subplot(223)
sns.lineplot(x = 'Category', y = 'D01_CVR', data = df)
#sns.barplot(x = 'Category', y = 'D2-08-Visits', data = df,label='D2-08_Visits')
xticks(rotation = 90)
plt.xticks(size = 11)
plt.yticks(size = 11)
plt.xlabel("Category",size = 13)
plt.ylabel("D01_CVR",size = 13)

plt.subplot(224)

plt.xticks(rotation='vertical')
ax = sns.barplot(x='Category',y='D2-08-Units',data=df)
ax2 = ax.twinx()
ax2.plot(ax.get_xticks(), df["D01_CVR"], alpha = .75, color = 'r')

plt.subplots_adjust(hspace=0.55,wspace=0.55)
plt.show()

enter image description here

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
  • Please [provide a reproducible copy of the DataFrame with `to_clipboard`](https://stackoverflow.com/questions/52413246/how-do-i-provide-a-reproducible-copy-of-my-existing-dataframe) – Trenton McKinney Oct 04 '19 at 19:07
  • Alternatively, [please provide a reproducible copy of DataFrame with Python equilvalent of R's `dput`](https://stackoverflow.com/questions/22418895/pythons-equivalent-for-rs-dput-function). – Parfait Oct 04 '19 at 19:16

2 Answers2

1

Here's how I do things like that:

import numpy as np
import matplotlib.pyplot as plt

data = [np.random.random((10, 10)) for _ in range(6)]

fig, axs = plt.subplots(ncols=3, nrows=2, figsize=(9, 6))
for ax, dat in zip(axs.ravel(), data):
    ax.imshow(dat)

This produces:

matplotlib output

The idea is that plt.subplots() produces an array of Axes objects, so you can loop over it and make your plots in the loop. In this case I need ndarray.ravel() because axs is a 2D array.

Matt Hall
  • 7,614
  • 1
  • 23
  • 36
0

Consider tightening up repetitive code by:

  • Set unchanging aesthetics like all x-ticks and y-ticks font sizes in one call with plt.rc calls.
  • Build plt.subplots() and use its array of Axes objects.
  • Use ax argument of seaborn's barplot and lineplot to loop above Axes array.

While not completely DRY given the special two plots, below is adjustment:

# AXES AND TICKS FONT SIZES
plt.rc('xtick', labelsize=11)
plt.rc('ytick', labelsize=11)
plt.rc('axes', labelsize=13)

# FIGURE AND SUBPLOTS SETUP
fig, axes = plt.subplots(nrows=2, ncols=2, figsize=(20, 12))

# BAR PLOTS (FIRST ROW)
for i, col in enumerate(['POG_Added', 'Live_POG']):
    sns.barplot(x='Category', y=col, data=df, ax=axes[0,i])
    axes[0,i].tick_params(axis='x', labelrotation=90)

# LINE PLOT 
sns.lineplot(x='Category', y='D01_CVR', data=df, ax=axes[1,0])
axes[1,0].tick_params(axis='x', labelrotation=90)

# BAR + LINE DUAL PLOT
sns.barplot(x='Category', y='D2-08-Units', data=df, ax=axes[1,1])
ax2 = axes[1,1].twinx()
ax2.plot(axes[1,1].get_xticks(), df["D01_CVR"], alpha = .75, color = 'r')
axes[1,1].tick_params(axis='x', labelrotation=90)
Parfait
  • 104,375
  • 17
  • 94
  • 125