Skip to content
Browse files

Rename lmj.plot.make_axis to lmj.plot.axes. Move spine setup code int…

…o a separate function.
  • Loading branch information...
1 parent d8491bd commit 2944f40c9b75dc9e0418243a560acae868053307 Leif Johnson committed
Showing with 86 additions and 68 deletions.
  1. +86 −68 lmj/plot.py
View
154 lmj/plot.py
@@ -1,10 +1,6 @@
'''Leif's convenience methods and data for plotting.'''
-# This is a little evil, but only a little bit -- now we can do :
-#
-# >>> import lmj.plot
-# >>> lmj.plot.plot(...)
-# >>> lmj.plot.show()
+import matplotlib
import matplotlib.pyplot as plt
# import some names directly from matplotlib.pyplot.
@@ -31,94 +27,116 @@
'#17becf',
)
-def make_axis(subplot=111,
- shape=None,
- log='',
- num_x_ticks=None,
- num_y_ticks=None,
- spines=True,
- **kwargs):
- '''Create an axis object to hold a plot.
+matplotlib.rcParams['axes.color_cycle'] = ', '.join(COLOR11)
- Returns a matplotlib.Axis instance.
+
+def axes(subplot=111, shape=None, log='', num_x_ticks=None, num_y_ticks=None, **kwargs):
+ '''Create an axes object to hold a plot.
Arguments
- subplot: This will be passed to matplotlib.pyplot.subplot().
+ ---------
+ subplot : int or tuple of ints
+ This will be passed to `matplotlib.pyplot.subplot()`.
+ shape : tuple of ints
+ If this is provided, it will be passed as the argument to
+ `matplotlib.pyplot.axis()`.
+ log : str
+ A string, containing 'x' and/or 'y', indicating the axes that should
+ have log scales.
+ num_x_ticks : int
+ The number of ticks to position on the x-axis, if any.
+ num_y_ticks : int
+ The number of ticks to position on the y-axis, if any.
- shape: If this is provided, it will be passed as the argument to
- matplotlib.pyplot.axis().
+ Remaining keyword arguments will be passed to `axes()` (if `shape` is not
+ None) or `subplot()` (otherwise).
- log: A string, containing 'x' and/or 'y', indicating the axes that should
- have log scales.
+ Yields
+ -------
+ A matplotlib.Axes instance. This can be used inside a with statement:
- num_x_ticks: The number of ticks to position on the x-axis, if any.
- num_y_ticks: The number of ticks to position on the y-axis, if any.
+ >>> with lmj.plot.axes(...) as ax:
+ ... ax.scatter(...)
+ '''
+ if shape is not None:
+ ax = plt.axes(shape, **kwargs)
+ else:
+ if not isinstance(subplot, (tuple, list)):
+ subplot = (subplot, )
+ ax = plt.subplot(*subplot, **kwargs)
- spines: This parameter can take many different types of values:
+ if 'x' in log:
+ ax.set_xscale('log')
+ if 'y' in log:
+ ax.set_yscale('log')
+
+ if num_x_ticks:
+ plt.locator_params(axis='x', nbins=num_x_ticks)
+ if num_y_ticks:
+ plt.locator_params(axis='y', nbins=num_y_ticks)
+
+ return ax
+
+
+def set_spines(ax, spines=True, offset=6):
+ '''Set the spines on the given Axes object.
+
+ Arguments
+ ---------
+ ax : matplotlib.Axes
+ Set the spines on this Axes object.
+ spines : variable
+ This parameter provides formatting instructions for the axes in the plot.
+ It can take many different types of values:
- dictionary: The strings 'left', 'right', 'top', 'bottom' should be
mapped to numeric values---the numeric value specifies the distance
outward from the plot (in points) to show the corresponding spine.
+ Alternatively, the values in the dictionary can be anything that can
+ be passed to `matplotlib.Spine#set_position`.
- numeric: All spines will be shown, offset outward by this many points.
- string/list/tuple/set: Spines with names included in the
- string/list/tuple/set will be shown at an offset of 6 points. Spines
- not named will not be shown.
+ string/list/tuple/set will be shown at an offset of 6pt. Spines not
+ named will not be shown.
- falsey: No spines will be shown.
Otherwise, the default spine configuration is used---the bottom and left
- spines are shown, offset outward by 6 points, and the top and right
- spines are hidden.
-
- Remaining keyword arguments will be passed to axis() or subplot().
+ spines are shown, offset outward by 6pt, and the top and right spines
+ are hidden.
+ offset : numeric
+ Default offset for spine names mentioned in the `spines` argument.
'''
- if shape is not None:
- ax = plt.axes(shape, **kwargs)
- else:
- if not isinstance(subplot, (tuple, list)):
- subplot = (subplot, )
- ax = plt.subplot(*subplot, **kwargs)
+ if spines is True:
+ spines = 'bottom left'
+ elif isinstance(spines, (int, float, long)):
+ offset = spines
+ spines = 'top right bottom left'
+ elif not spines:
+ spines = {}
- offsets = dict(bottom=6, left=6) if spines else {}
- if isinstance(spines, dict):
- offsets = spines
- if isinstance(spines, (int, float, long)):
- offsets = {s: spines for s in 'top right bottom left'.split()}
if isinstance(spines, str):
- offsets = {s: 6 for s in spines.split()}
+ spines = spines.split()
+
if isinstance(spines, (list, tuple, set)):
- offsets = {k: 6 for k in spines}
+ spines = {k: offset for k in spines}
+
for name, spine in ax.spines.iteritems():
- offset = offsets.get(name)
- if offset is not None:
- spine.set_position(('outward', offset))
+ pos = spines.get(name)
+ if pos is None:
+ spine.set_visible(False)
+ elif isinstance(pos, int):
+ spine.set_position(('outward', pos))
else:
- spine.set_color('none')
+ spine.set_position(pos)
- if offsets.get('top') is not None:
- ax.xaxis.tick_top()
- if offsets.get('right') is not None:
- ax.yaxis.tick_right()
- if offsets.get('bottom') is not None:
+ if 'bottom' in spines:
ax.xaxis.tick_bottom()
- if offsets.get('left') is not None:
+ else:
+ ax.xaxis.set_ticks([])
+ if 'left' in spines:
ax.yaxis.tick_left()
-
- if offsets.get('top') == offsets.get('bottom') == None:
- ax.set_xticks([])
- if offsets.get('left') == offsets.get('right') == None:
- ax.set_yticks([])
-
- if 'x' in log:
- ax.set_xscale('log')
- if 'y' in log:
- ax.set_yscale('log')
-
- if num_x_ticks:
- plt.locator_params(axis='x', nbins=num_x_ticks)
- if num_y_ticks:
- plt.locator_params(axis='y', nbins=num_y_ticks)
-
- return ax
+ else:
+ ax.yaxis.set_ticks([])

0 comments on commit 2944f40

Please sign in to comment.
Something went wrong with that request. Please try again.