Last active
August 24, 2017 13:44
-
-
Save Jwink3101/11610a397a0d25adce656ff8da250e7f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
""" | |
Demo of subplot methods for matplotlib using pyplot only when needed | |
and doing the main plotting on axes objects | |
""" | |
import itertools | |
import numpy as np | |
import matplotlib.pylab as plt | |
############### Method 1: gridspec | |
fig = plt.figure(1) | |
ax = [None for _ in range(6)] | |
ax[0] = plt.subplot2grid((3,4), (0,0), colspan=4) | |
ax[1] = plt.subplot2grid((3,4), (1,0), colspan=1) | |
ax[2] = plt.subplot2grid((3,4), (1,1), colspan=1) | |
ax[3] = plt.subplot2grid((3,4), (1,2), colspan=1) | |
ax[4] = plt.subplot2grid((3,4), (1,3), colspan=1,rowspan=2) | |
ax[5] = plt.subplot2grid((3,4), (2,0), colspan=3) | |
for ix in range(6): | |
ax[ix].set_title('ax[{}]'.format(ix)) | |
fig.tight_layout() | |
############### Method 2: add_subplot | |
fig = plt.figure(2) | |
ax = [None for _ in range(3)] | |
ax[0] = fig.add_subplot(2,2,1) | |
ax[1] = fig.add_subplot(2,2,2) | |
ax[2] = fig.add_subplot(2,2,3) | |
for ix in range(3): | |
ax[ix].set_title('ax[{}]'.format(ix)) | |
fig.tight_layout() | |
############### Method 3: manual | |
fig = plt.figure(3) | |
ax = [None for _ in range(3)] | |
ax[0] = fig.add_axes([0.1,0.1,0.9,0.4]) # Bottom | |
ax[1] = fig.add_axes([0.15,0.6,0.25,0.6]) # They do not *need* to be in a grid | |
ax[2] = fig.add_axes([0.5,0.6,0.4,0.3]) | |
for ix in range(3): | |
ax[ix].set_title('ax[{}]'.format(ix)) | |
# fig.tight_layout() # does not work with this method | |
############### Method 4: clear an axis | |
fig,axes = plt.subplots(2,2,num=4) | |
# label | |
for i,j in itertools.product(range(2),range(2)): | |
ax = axes[i,j] | |
if i == j == 1: | |
continue | |
ax.set_title('ax[{},{}]'.format(i,j)) | |
ax = axes[1,1] | |
ax.set_frame_on(False) | |
ax.set_xticks([]) | |
ax.set_yticks([]) | |
fig.tight_layout() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment