On Feb 6, 2009, at 8:45 AM, Zunbeltz Izaola wrote:
Dear all,
I would like to have a plot where the frame only have left and
bottom border. I can not find in the documentation any function to
draw
the Rectangle contained in figure() only with this 2 lines. It is
possilbe?
Hi Zunbeltz,
Attached is an example of a custom Axes class that does what you want.
There are examples at the bottom of the file that show its use. I
worked on generalizing this idea for inclusion in MPL, but the code
got really nasty, really quickly.
On a side note, if any of the MPL devs think this would make a useful
API example (since this topic has come up a few times on the list),
feel free to do whatever you want with it.
Cheers,
-Tony
#!/usr/bin/env python
Frame classes for customizing frame borders that surround the plot axes.
import numpy as np
import matplotlib.axes as maxes
import matplotlib.pyplot as plt
import matplotlib.artist as martist
import matplotlib.collections as col
import matplotlib.projections as projections
class Frame(martist.Artist):
Draw frame along the edges of the axes patch.
Frame position can be controlled upon initialization or by setting
`positions` property with a list of positions
['left', 'right', 'top', 'bottom' | 'all']
_position_list = ('left', 'right', 'top', 'bottom')
def __init__(self, axes, positions=('left', 'bottom'), **kwargs):
`positions` is a list of strings of frame positions to plot.
['left', 'right', 'top', 'bottom' | 'all']
super(Frame, self).__init__()
# TODO: allow more keyword configuration
self.axes = axes
rc = plt.rcParams
self.color = kwargs.pop('color', rc['axes.edgecolor'])
self.linewidth = kwargs.pop('linewidth', rc['axes.linewidth'])
self.linestyle = kwargs.pop('linestyle', 'solid')
self.positions = positions
def get_data(self):
Convenience method returns tuple of (x, y) data in `self.axes`
x, y = [], []
ax = self.axes
for artist in (ax.lines, ax.patches):
if not artist == []:
x.append(np.concatenate([a.get_xdata() for a in artist]))
y.append(np.concatenate([a.get_ydata() for a in artist]))
# TODO: get scatter data from ax.collections
return (np.concatenate(x), np.concatenate(y))
def _set_frame_position(self, positions):
Set positions where frame will be drawn.
`positions` is a list of strings of frame positions to plot.
['left', 'right', 'top', 'bottom' | 'all']
self._frame_on = self._frame_dict_from(positions)
def _get_frame_position(self):
return [p for p in self._position_list if self._frame_on[p]]
# xposition tuples turn on frame for (bottom, top)
_xposition_pairs = {(True, False): 'bottom', (False, True): 'top',
(True, True): 'both', (False, False): 'none'}
def _get_xposition(self, frame_on=None):
Returns position that matches `XAxis.set_ticks_position` inputs.
`frame_on` is a dict that matches frame positions with bools.
if frame_on is None:
frame_on = self._frame_on
return self._xposition_pairs[(frame_on['bottom'], frame_on['top'])]
# yposition tuples turn on frame for (left, right)
_yposition_pairs = {(True, False): 'left', (False, True): 'right',
(True, True): 'both', (False, False): 'none'}
def _get_yposition(self, frame_on=None):
Returns position that matches `YAxis.set_ticks_position` inputs.
`frame_on` is a dict that matches frame positions with bools.
if frame_on is None:
frame_on = self._frame_on
return self._yposition_pairs[(frame_on['left'], frame_on['right'])]
def _frame_dict_from(self, positions):
Parse `positions` and return xposition, yposition tuple
`positions` is a list of strings of frame positions to plot.
['left', 'right', 'top', 'bottom' | 'all']
frame_dict = dict.fromkeys(self._position_list, False)
if 'all' in positions:
frame_dict = dict.fromkeys(self._position_list, True)
else:
for position in positions:
frame_dict[position] = True
return frame_dict
def _set_ticks(self):
Overide this method to customize tick positioning.
# Draw ticks on axes only where a frame is drawn
self.axes.xaxis.set_ticks_position(self._get_xposition())
self.axes.yaxis.set_ticks_position(self._get_yposition())
_frame_lines = dict(bottom=[(0., 0.), (1., 0.)], top=[(0., 1.), (1., 1.)],
left=[(0., 0.), (0., 1.)], right=[(1., 0.), (1., 1.)])
def _make_frame(self):
Get axis frame specified by `self._frame_on`.
lines = [self._frame_lines[p] for p in