Skip to content

Commit 8ad81a8

Browse files
committed
added broken barh
svn path=/trunk/matplotlib/; revision=2860
1 parent f83fce9 commit 8ad81a8

File tree

6 files changed

+1145
-5
lines changed

6 files changed

+1145
-5
lines changed

CHANGELOG

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
2006-11-05 Added broken_barh function for makring a sequence of
2+
horizontal bars broken by gaps -- see examples/broken_barh.py
3+
4+
2006-11-05 Removed lineprops and markerprops from the Annotation code
5+
and replaced them with an arrow configurable with kwarg
6+
arrowprops. See examples/annotation_demo.py - JDH
7+
18
2006-11-02 Fixed a pylab subplot bug that was causing axes to be
29
deleted with hspace or wspace equals zero in
310
subplots_adjust - JDH

boilerplate.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ def %(func)s(*args, **kwargs):
5454
'axvspan',
5555
'bar',
5656
'barh',
57+
'broken_barh',
5758
'boxplot',
5859
'cohere',
5960
'clabel',

examples/broken_barh.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
"""
2+
Make a "broken" horizontal bar plot, ie one with gaps
3+
"""
4+
from pylab import figure, show, nx
5+
6+
fig = figure()
7+
ax = fig.add_subplot(111)
8+
ax.broken_barh([ (110, 30), (150, 10) ] , (10, 9), facecolors='blue')
9+
ax.broken_barh([ (10, 50), (100, 20), (130, 10)] , (20, 9),
10+
facecolors=('red', 'yellow', 'green'))
11+
ax.set_ylim(5,35)
12+
ax.set_xlim(0,200)
13+
ax.set_xlabel('seconds since start')
14+
ax.set_yticks([15,25])
15+
ax.set_yticklabels(['Bill', 'Jim'])
16+
ax.grid(True)
17+
ax.annotate('race interrupted', (61, 25),
18+
xytext=(0.8, 0.9), textcoords='axes fraction',
19+
arrowprops=dict(facecolor='black', shrink=0.05),
20+
fontsize=16,
21+
horizontalalignment='right', verticalalignment='top')
22+
23+
show()

lib/matplotlib/axes.py

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
from axis import XAxis, YAxis
1616
from cbook import iterable, is_string_like, flatten, enumerate, \
1717
allequal, dict_delall, popd, popall, silent_list, is_numlike
18-
from collections import RegularPolyCollection, PolyCollection, LineCollection, QuadMesh, \
19-
StarPolygonCollection
18+
from collections import RegularPolyCollection, PolyCollection, LineCollection, \
19+
QuadMesh, StarPolygonCollection, BrokenBarHCollection
2020
from colors import colorConverter, normalize, Colormap, \
2121
LinearSegmentedColormap, looks_like_color, is_color_like
2222
import cm
@@ -432,20 +432,28 @@ def _set_lim_and_transforms(self):
432432
if self._sharex is not None:
433433
left=self._sharex.viewLim.ll().x()
434434
right=self._sharex.viewLim.ur().x()
435+
#dleft=self._sharex.dataLim.ll().x()
436+
#dright=self._sharex.dataLim.ur().x()
435437
else:
436438
left=zero()
437439
right=one()
440+
#dleft=zero()
441+
#dright=one()
438442
if self._sharey is not None:
439443
bottom=self._sharey.viewLim.ll().y()
440444
top=self._sharey.viewLim.ur().y()
445+
#dbottom=self._sharey.dataLim.ll().y()
446+
#dtop=self._sharey.dataLim.ur().y()
441447
else:
442448
bottom=zero()
443449
top=one()
450+
#dbottom=zero()
451+
#dtop=one()
444452

445453
self.viewLim = Bbox(Point(left, bottom), Point(right, top))
454+
#self.dataLim = Bbox(Point(dleft, dbottom), Point(dright, dtop))
446455
self.dataLim = unit_bbox()
447456

448-
449457
self.transData = get_bbox_transform(self.viewLim, self.bbox)
450458
self.transAxes = get_bbox_transform(unit_bbox(), self.bbox)
451459

@@ -2626,7 +2634,32 @@ def barh(self, bottom, width, height=0.8, left=0,
26262634
)
26272635
return patches
26282636

2637+
def broken_barh(self, xranges, yrange, **kwargs):
2638+
"""
2639+
A colleciton of horizontal bars spanning yrange with a sequence of
2640+
xranges
2641+
2642+
xranges : sequence of (xmin, xwidth)
2643+
yrange : (ymin, ywidth)
2644+
2645+
optional kwargs:
2646+
edgecolors
2647+
facecolors
2648+
linewidths
2649+
antialiaseds
2650+
2651+
these can either be a single argument, ie facecolors='black'
2652+
or a sequence of arguments for the various bars, ie
2653+
facecolors='black', 'red', 'green'
2654+
2655+
"""
2656+
col = BrokenBarHCollection(xranges, yrange, **kwargs)
2657+
self.add_collection(col, autolim=True)
2658+
self.autoscale_view()
2659+
2660+
return col
26292661

2662+
26302663
def stem(self, x, y, linefmt='b-', markerfmt='bo', basefmt='r-'):
26312664
"""
26322665
STEM(x, y, linefmt='b-', markerfmt='bo', basefmt='r-')

lib/matplotlib/collections.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -278,8 +278,22 @@ def get_verts(self, dataTrans=None):
278278
return [tuple(xy) for xy in self._offsets]
279279
raise NotImplementedError('Vertices in data coordinates are calculated\n'
280280
+ 'with offsets only if _transOffset == dataTrans.')
281-
282-
281+
282+
class BrokenBarHCollection(PolyCollection):
283+
"""
284+
A colleciton of horizontal bars spanning yrange with a sequence of
285+
xranges
286+
"""
287+
def __init__(self, xranges, yrange, **kwargs):
288+
"""
289+
xranges : sequence of (xmin, xwidth)
290+
yrange : ymin, ywidth
291+
"""
292+
ymin, ywidth = yrange
293+
ymax = ymin + ywidth
294+
verts = [ [(xmin, ymin), (xmin, ymax), (xmin+xwidth, ymax), (xmin+xwidth, ymin)] for xmin, xwidth in xranges]
295+
PolyCollection.__init__(self, verts, **kwargs)
296+
283297
class RegularPolyCollection(PatchCollection):
284298
def __init__(self,
285299
dpi,

0 commit comments

Comments
 (0)