|
4 | 4 |
|
5 | 5 | from matplotlib import docstring
|
6 | 6 | from matplotlib import transforms as mtransforms
|
7 |
| -from matplotlib import line as mlines |
| 7 | +from matplotlib import lines as mlines |
8 | 8 |
|
9 | 9 |
|
10 | 10 | @docstring.dedent_interpd
|
@@ -78,3 +78,90 @@ def axhline(ax, y=0, xmin=0, xmax=1, **kwargs):
|
78 | 78 | ax.add_line(l)
|
79 | 79 | ax.autoscale_view(scalex=False, scaley=scaley)
|
80 | 80 | return l
|
| 81 | + |
| 82 | + |
| 83 | +def hlines(ax, y, xmin, xmax, colors='k', linestyles='solid', |
| 84 | + label='', **kwargs): |
| 85 | + """ |
| 86 | + Plot horizontal lines at each `y` from `xmin` to `xmax`. |
| 87 | +
|
| 88 | + Parameters |
| 89 | + ---------- |
| 90 | + y : scalar or sequence of scalar |
| 91 | + y-indexes where to plot the lines. |
| 92 | +
|
| 93 | + xmin, xmax : scalar or 1D array_like |
| 94 | + Respective beginning and end of each line. If scalars are |
| 95 | + provided, all lines will have same length. |
| 96 | +
|
| 97 | + colors : array_like of colors, optional, default: 'k' |
| 98 | +
|
| 99 | + linestyles : ['solid' | 'dashed' | 'dashdot' | 'dotted'], optional |
| 100 | +
|
| 101 | + label : string, optional, default: '' |
| 102 | +
|
| 103 | + Returns |
| 104 | + ------- |
| 105 | + lines : `~matplotlib.collections.LineCollection` |
| 106 | +
|
| 107 | + Other parameters |
| 108 | + ---------------- |
| 109 | + kwargs : `~matplotlib.collections.LineCollection` properties. |
| 110 | +
|
| 111 | + See also |
| 112 | + -------- |
| 113 | + vlines : vertical lines |
| 114 | +
|
| 115 | + Examples |
| 116 | + -------- |
| 117 | + .. plot:: mpl_examples/pylab_examples/vline_hline_demo.py |
| 118 | +
|
| 119 | + """ |
| 120 | + |
| 121 | + # We do the conversion first since not all unitized data is uniform |
| 122 | + # process the unit information |
| 123 | + ax._process_unit_info([xmin, xmax], y, kwargs=kwargs) |
| 124 | + y = ax.convert_yunits(y) |
| 125 | + xmin = ax.convert_xunits(xmin) |
| 126 | + xmax = ax.convert_xunits(xmax) |
| 127 | + |
| 128 | + if not iterable(y): |
| 129 | + y = [y] |
| 130 | + if not iterable(xmin): |
| 131 | + xmin = [xmin] |
| 132 | + if not iterable(xmax): |
| 133 | + xmax = [xmax] |
| 134 | + |
| 135 | + y = np.asarray(y) |
| 136 | + xmin = np.asarray(xmin) |
| 137 | + xmax = np.asarray(xmax) |
| 138 | + |
| 139 | + if len(xmin) == 1: |
| 140 | + xmin = np.resize(xmin, y.shape) |
| 141 | + if len(xmax) == 1: |
| 142 | + xmax = np.resize(xmax, y.shape) |
| 143 | + |
| 144 | + if len(xmin) != len(y): |
| 145 | + raise ValueError('xmin and y are unequal sized sequences') |
| 146 | + if len(xmax) != len(y): |
| 147 | + raise ValueError('xmax and y are unequal sized sequences') |
| 148 | + |
| 149 | + verts = [((thisxmin, thisy), (thisxmax, thisy)) |
| 150 | + for thisxmin, thisxmax, thisy in zip(xmin, xmax, y)] |
| 151 | + coll = mcoll.LineCollection(verts, colors=colors, |
| 152 | + linestyles=linestyles, label=label) |
| 153 | + ax.add_collection(coll) |
| 154 | + coll.update(kwargs) |
| 155 | + |
| 156 | + if len(y) > 0: |
| 157 | + minx = min(xmin.min(), xmax.min()) |
| 158 | + maxx = max(xmin.max(), xmax.max()) |
| 159 | + miny = y.min() |
| 160 | + maxy = y.max() |
| 161 | + |
| 162 | + corners = (minx, miny), (maxx, maxy) |
| 163 | + |
| 164 | + ax.update_datalim(corners) |
| 165 | + ax.autoscale_view() |
| 166 | + |
| 167 | + return coll |
0 commit comments