Skip to content

Pep8ify examples #3425

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 21 commits into from
Sep 9, 2014
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
pep8ify: examples/axes_grid
Signed-off-by: Thomas Hisch <t.hisch@gmail.com>
  • Loading branch information
twmr committed Aug 27, 2014
commit d0d9632a31ea29e6d64bf15514134e8f15d30cc5
11 changes: 6 additions & 5 deletions examples/axes_grid/demo_axes_divider.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import matplotlib.pyplot as plt


def get_demo_image():
import numpy as np
from matplotlib.cbook import get_sample_data
f = get_sample_data("axes_grid/bivariate_normal.npy", asfileobj=False)
z = np.load(f)
# z is a numpy array of 15x15
return z, (-3,4,-4,3)
return z, (-3, 4, -4, 3)


def demo_simple_image(ax):
Expand All @@ -20,7 +21,7 @@ def demo_simple_image(ax):
def demo_locatable_axes_hard(fig1):

from mpl_toolkits.axes_grid1 \
import SubplotDivider, LocatableAxes, Size
import SubplotDivider, LocatableAxes, Size

divider = SubplotDivider(fig1, 2, 2, 2, aspect=True)

Expand All @@ -30,9 +31,9 @@ def demo_locatable_axes_hard(fig1):
# axes for colorbar
ax_cb = LocatableAxes(fig1, divider.get_position())

h = [Size.AxesX(ax), # main axes
Size.Fixed(0.05), # padding, 0.1 inch
Size.Fixed(0.2), # colorbar, 0.3 inch
h = [Size.AxesX(ax), # main axes
Size.Fixed(0.05), # padding, 0.1 inch
Size.Fixed(0.2), # colorbar, 0.3 inch
]

v = [Size.AxesY(ax)]
Expand Down
37 changes: 20 additions & 17 deletions examples/axes_grid/demo_axes_grid.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import AxesGrid


def get_demo_image():
import numpy as np
from matplotlib.cbook import get_sample_data
f = get_sample_data("axes_grid/bivariate_normal.npy", asfileobj=False)
z = np.load(f)
# z is a numpy array of 15x15
return z, (-3,4,-4,3)
return z, (-3, 4, -4, 3)


def demo_simple_grid(fig):
"""
A grid of 2x2 images with 0.05 inch pad between images and only
the lower-left axes is labeled.
"""
grid = AxesGrid(fig, 141, # similar to subplot(141)
nrows_ncols = (2, 2),
grid = AxesGrid(fig, 141, # similar to subplot(141)
nrows_ncols=(2, 2),
axes_pad = 0.05,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess the autopep8 tool got fatigued and gave up removing the spaces for these kwargs...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't reproduce it. Probably I messed it up.

edit: looked into the wrong line. It is indeed a problem of autopep8 - created hhatto/autopep8#158

label_mode = "1",
)
Expand All @@ -24,7 +26,8 @@ def demo_simple_grid(fig):
for i in range(4):
im = grid[i].imshow(Z, extent=extent, interpolation="nearest")

# This only affects axes in first column and second row as share_all = False.
# This only affects axes in first column and second row as share_all =
# False.
grid.axes_llc.set_xticks([-2, 0, 2])
grid.axes_llc.set_yticks([-2, 0, 2])

Expand All @@ -33,8 +36,8 @@ def demo_grid_with_single_cbar(fig):
"""
A grid of 2x2 images with a single colorbar
"""
grid = AxesGrid(fig, 142, # similar to subplot(142)
nrows_ncols = (2, 2),
grid = AxesGrid(fig, 142, # similar to subplot(142)
nrows_ncols=(2, 2),
axes_pad = 0.0,
share_all=True,
label_mode = "L",
Expand All @@ -50,7 +53,7 @@ def demo_grid_with_single_cbar(fig):

for cax in grid.cbar_axes:
cax.toggle_label(False)

# This affects all axes as share_all = True.
grid.axes_llc.set_xticks([-2, 0, 2])
grid.axes_llc.set_yticks([-2, 0, 2])
Expand All @@ -61,8 +64,8 @@ def demo_grid_with_each_cbar(fig):
A grid of 2x2 images. Each image has its own colorbar.
"""

grid = AxesGrid(F, 143, # similar to subplot(143)
nrows_ncols = (2, 2),
grid = AxesGrid(F, 143, # similar to subplot(143)
nrows_ncols=(2, 2),
axes_pad = 0.1,
label_mode = "1",
share_all = True,
Expand All @@ -83,14 +86,15 @@ def demo_grid_with_each_cbar(fig):
grid.axes_llc.set_xticks([-2, 0, 2])
grid.axes_llc.set_yticks([-2, 0, 2])


def demo_grid_with_each_cbar_labelled(fig):
"""
A grid of 2x2 images. Each image has its own colorbar.
"""

grid = AxesGrid(F, 144, # similar to subplot(144)
nrows_ncols = (2, 2),
axes_pad = ( 0.45, 0.15),
grid = AxesGrid(F, 144, # similar to subplot(144)
nrows_ncols=(2, 2),
axes_pad = (0.45, 0.15),
label_mode = "1",
share_all = True,
cbar_location="right",
Expand All @@ -100,15 +104,15 @@ def demo_grid_with_each_cbar_labelled(fig):
)
Z, extent = get_demo_image()

# Use a different colorbar range every time
# Use a different colorbar range every time
limits = ((0, 1), (-2, 2), (-1.7, 1.4), (-1.5, 1))
for i in range(4):
im = grid[i].imshow(Z, extent=extent, interpolation="nearest",
vmin = limits[i][0], vmax = limits[i][1])
im = grid[i].imshow(Z, extent=extent, interpolation="nearest",
vmin=limits[i][0], vmax=limits[i][1])
grid.cbar_axes[i].colorbar(im)

for i, cax in enumerate(grid.cbar_axes):
cax.set_yticks((limits[i][0], limits[i][1]))
cax.set_yticks((limits[i][0], limits[i][1]))

# This affects all axes because we set share_all = True.
grid.axes_llc.set_xticks([-2, 0, 2])
Expand All @@ -127,4 +131,3 @@ def demo_grid_with_each_cbar_labelled(fig):

plt.draw()
plt.show()

40 changes: 20 additions & 20 deletions examples/axes_grid/demo_axes_grid2.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@
from mpl_toolkits.axes_grid1 import ImageGrid
import numpy as np


def get_demo_image():
from matplotlib.cbook import get_sample_data
f = get_sample_data("axes_grid/bivariate_normal.npy", asfileobj=False)
z = np.load(f)
# z is a numpy array of 15x15
return z, (-3,4,-4,3)
return z, (-3, 4, -4, 3)


def add_inner_title(ax, title, loc, size=None, **kwargs):
Expand All @@ -28,27 +29,27 @@ def add_inner_title(ax, title, loc, size=None, **kwargs):

# prepare images
Z, extent = get_demo_image()
ZS = [Z[i::3,:] for i in range(3)]
ZS = [Z[i::3, :] for i in range(3)]
extent = extent[0], extent[1]/3., extent[2], extent[3]

# demo 1 : colorbar at each axes

grid = ImageGrid(F, 211, # similar to subplot(111)
nrows_ncols = (1, 3),
direction="row",
axes_pad = 0.05,
add_all=True,
label_mode = "1",
share_all = True,
cbar_location="top",
cbar_mode="each",
cbar_size="7%",
cbar_pad="1%",
)

grid = ImageGrid(F, 211, # similar to subplot(111)
nrows_ncols=(1, 3),
direction="row",
axes_pad = 0.05,
add_all=True,
label_mode = "1",
share_all = True,
cbar_location="top",
cbar_mode="each",
cbar_size="7%",
cbar_pad="1%",
)

for ax, z in zip(grid, ZS):
im = ax.imshow(z, origin="lower", extent=extent, interpolation="nearest")
im = ax.imshow(
z, origin="lower", extent=extent, interpolation="nearest")
ax.cax.colorbar(im)

for ax, im_title in zip(grid, ["Image 1", "Image 2", "Image 3"]):
Expand All @@ -59,8 +60,8 @@ def add_inner_title(ax, title, loc, size=None, **kwargs):
ax.cax.toggle_label(True)
#axis = ax.cax.axis[ax.cax.orientation]
#axis.label.set_text("counts s$^{-1}$")
#axis.label.set_size(10)
#axis.major_ticklabels.set_size(6)
# axis.label.set_size(10)
# axis.major_ticklabels.set_size(6)

# changing the colorbar ticks
grid[1].cax.set_xticks([-1, 0, 1])
Expand All @@ -69,11 +70,10 @@ def add_inner_title(ax, title, loc, size=None, **kwargs):
grid[0].set_xticks([-2, 0])
grid[0].set_yticks([-2, 0, 2])


# demo 2 : shared colorbar

grid2 = ImageGrid(F, 212,
nrows_ncols = (1, 3),
nrows_ncols=(1, 3),
direction="row",
axes_pad = 0.05,
add_all=True,
Expand Down
19 changes: 9 additions & 10 deletions examples/axes_grid/demo_axes_hbox_divider.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
from mpl_toolkits.axes_grid1.axes_divider import HBoxDivider
import mpl_toolkits.axes_grid1.axes_size as Size


def make_heights_equal(fig, rect, ax1, ax2, pad):
# pad in inches

h1, v1 = Size.AxesX(ax1), Size.AxesY(ax1)
h2, v2 = Size.AxesX(ax2), Size.AxesY(ax2)

Expand All @@ -16,23 +17,22 @@ def make_heights_equal(fig, rect, ax1, ax2, pad):
horizontal=[h1, pad_h, h2],
vertical=[v1, pad_v, v2])


ax1.set_axes_locator(my_divider.new_locator(0))
ax2.set_axes_locator(my_divider.new_locator(2))


if __name__ == "__main__":

arr1 = np.arange(20).reshape((4,5))
arr2 = np.arange(20).reshape((5,4))
arr1 = np.arange(20).reshape((4, 5))
arr2 = np.arange(20).reshape((5, 4))

fig, (ax1, ax2) = plt.subplots(1,2)
fig, (ax1, ax2) = plt.subplots(1, 2)
ax1.imshow(arr1, interpolation="nearest")
ax2.imshow(arr2, interpolation="nearest")

rect = 111 # subplot param for combined axes
make_heights_equal(fig, rect, ax1, ax2, pad=0.5) # pad in inches
rect = 111 # subplot param for combined axes
make_heights_equal(fig, rect, ax1, ax2, pad=0.5) # pad in inches

for ax in [ax1, ax2]:
ax.locator_params(nbins=4)

Expand All @@ -47,4 +47,3 @@ def make_heights_equal(fig, rect, ax1, ax2, pad):
bbox=dict(boxstyle="round, pad=1", fc="w"))

plt.show()

31 changes: 14 additions & 17 deletions examples/axes_grid/demo_axes_rgb.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,49 +3,48 @@

from mpl_toolkits.axes_grid1.axes_rgb import make_rgb_axes, RGBAxes


def get_demo_image():
from matplotlib.cbook import get_sample_data
f = get_sample_data("axes_grid/bivariate_normal.npy", asfileobj=False)
z = np.load(f)
# z is a numpy array of 15x15
return z, (-3,4,-4,3)

return z, (-3, 4, -4, 3)


def get_rgb():
Z, extent = get_demo_image()

Z[Z<0] = 0.
Z[Z < 0] = 0.
Z = Z/Z.max()

R = Z[:13,:13]
G = Z[2:,2:]
B = Z[:13,2:]
R = Z[:13, :13]
G = Z[2:, 2:]
B = Z[:13, 2:]

return R, G, B


def make_cube(r, g, b):
ny, nx = r.shape
R = np.zeros([ny, nx, 3], dtype="d")
R[:,:,0] = r
R[:, :, 0] = r
G = np.zeros_like(R)
G[:,:,1] = g
G[:, :, 1] = g
B = np.zeros_like(R)
B[:,:,2] = b
B[:, :, 2] = b

RGB = R + G + B

return R, G, B, RGB



def demo_rgb():
fig, ax = plt.subplots()
ax_r, ax_g, ax_b = make_rgb_axes(ax, pad=0.02)
#fig.add_axes(ax_r)
#fig.add_axes(ax_g)
#fig.add_axes(ax_b)
# fig.add_axes(ax_r)
# fig.add_axes(ax_g)
# fig.add_axes(ax_b)

r, g, b = get_rgb()
im_r, im_g, im_b, im_rgb = make_cube(r, g, b)
Expand All @@ -56,13 +55,11 @@ def demo_rgb():
ax_b.imshow(im_b, **kwargs)




def demo_rgb2():
fig = plt.figure(2)
ax = RGBAxes(fig, [0.1, 0.1, 0.8, 0.8], pad=0.0)
#fig.add_axes(ax)
#ax.add_RGB_to_figure()
# fig.add_axes(ax)
# ax.add_RGB_to_figure()

r, g, b = get_rgb()
kwargs = dict(origin="lower", interpolation="nearest")
Expand Down
16 changes: 8 additions & 8 deletions examples/axes_grid/demo_colorbar_with_inset_locator.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=[6, 3])

axins1 = inset_axes(ax1,
width="50%", # width = 10% of parent_bbox width
height="5%", # height : 50%
width="50%", # width = 10% of parent_bbox width
height="5%", # height : 50%
loc=1)

im1=ax1.imshow([[1,2],[2, 3]])
plt.colorbar(im1, cax=axins1, orientation="horizontal", ticks=[1,2,3])
im1 = ax1.imshow([[1, 2], [2, 3]])
plt.colorbar(im1, cax=axins1, orientation="horizontal", ticks=[1, 2, 3])
axins1.xaxis.set_ticks_position("bottom")

axins = inset_axes(ax2,
width="5%", # width = 10% of parent_bbox width
height="50%", # height : 50%
width="5%", # width = 10% of parent_bbox width
height="50%", # height : 50%
loc=3,
bbox_to_anchor=(1.05, 0., 1, 1),
bbox_transform=ax2.transAxes,
Expand All @@ -26,8 +26,8 @@
# of the legend. you may want to play with the borderpad value and
# the bbox_to_anchor coordinate.

im=ax2.imshow([[1,2],[2, 3]])
plt.colorbar(im, cax=axins, ticks=[1,2,3])
im = ax2.imshow([[1, 2], [2, 3]])
plt.colorbar(im, cax=axins, ticks=[1, 2, 3])

plt.draw()
plt.show()
Loading