Permalink
Browse files

Start moving reusable code into an lmj.plot module.

  • Loading branch information...
1 parent 55bb323 commit a6884e5aa21dee8f9d1b0aa728d1e60701b06a2d Leif Johnson committed Oct 28, 2013
Showing with 233 additions and 89 deletions.
  1. +1 −0 .gitignore
  2. +1 −0 lmj/__init__.py
  3. +126 −0 lmj/plot.py
  4. +103 −87 scripts/{py-grep-plot → lmj-plot}
  5. +2 −2 setup.py
View
@@ -1,3 +1,4 @@
build
dist
*.egg-info
+*.pyc
View
@@ -0,0 +1 @@
+__import__('pkg_resources').declare_namespace(__name__)
View
@@ -0,0 +1,126 @@
+'''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.pyplot as plt
+
+# import some names directly from matplotlib.pyplot.
+clf = plt.clf
+gcf = plt.gcf
+plot = plt.plot
+savefig = plt.savefig
+scatter = plt.scatter
+show = plt.show
+tight_layout = plt.tight_layout
+
+# colors from https://github.com/mbostock/d3/wiki/Ordinal-Scales
+COLOR11 = (
+ '#111111',
+ '#1f77b4',
+ '#2ca02c',
+ '#9467bd',
+ '#ff7f0e',
+ '#d62728',
+ '#8c564b',
+ '#e377c2',
+ '#7f7f7f',
+ '#bcbd22',
+ '#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.
+
+ Returns a matplotlib.Axis instance.
+
+ Arguments
+ subplot: This will be passed to matplotlib.pyplot.subplot().
+
+ shape: If this is provided, it will be passed as the argument to
+ matplotlib.pyplot.axis().
+
+ log: A string, containing 'x' and/or 'y', indicating the axes that should
+ have log scales.
+
+ 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.
+
+ spines: This parameter 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.
+
+ - 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.
+
+ - 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().
+ '''
+ if shape is not None:
+ ax = plt.axes(shape, **kwargs)
+ else:
+ if not isinstance(subplot, (tuple, list)):
+ subplot = (subplot, )
+ ax = plt.subplot(*subplot, **kwargs)
+
+ offsets = dict(bottom=6, left=6)
+ if isinstance(spines, dict):
+ offsets = spines
+ elif isinstance(spines, (int, float, long)):
+ offsets = {s: spines for s in 'top right bottom left'.split()}
+ elif isinstance(spines, str):
+ offsets = {s: 6 for s in spines.split()}
+ elif isinstance(spines, (list, tuple, set)):
+ offsets = {k: 6 for k in spines}
+ elif not spines:
+ offsets = {}
+ for name, spine in ax.spines.iteritems():
+ offset = offsets.get(name)
+ if offset is not None:
+ spine.set_position(('outward', offset))
+ else:
+ spine.set_color('none')
+
+ 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:
+ ax.xaxis.tick_bottom()
+ if offsets.get('left') is not None:
+ 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
Oops, something went wrong.

0 comments on commit a6884e5

Please sign in to comment.