Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4771 from AmyTeegarden/legend_demo5
NF - New legend example with line collection
- Loading branch information
Showing
1 changed file
with
71 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
from __future__ import (absolute_import, division, | ||
print_function, unicode_literals) | ||
import six | ||
from matplotlib import pyplot as plt | ||
import numpy as np | ||
from matplotlib.legend_handler import HandlerLineCollection | ||
import matplotlib.collections as mcol | ||
from matplotlib.lines import Line2D | ||
|
||
|
||
class HandlerDashedLines(HandlerLineCollection): | ||
""" | ||
Custom Handler for LineCollection instances. | ||
""" | ||
def create_artists(self, legend, orig_handle, | ||
xdescent, ydescent, width, height, fontsize, trans): | ||
# figure out how many lines there are | ||
numlines = len(orig_handle.get_segments()) | ||
xdata, xdata_marker = self.get_xdata(legend, xdescent, ydescent, | ||
width, height, fontsize) | ||
leglines = [] | ||
# divide the vertical space where the lines will go | ||
# into equal parts based on the number of lines | ||
ydata = ((height) / (numlines + 1)) * np.ones(xdata.shape, float) | ||
# for each line, create the line at the proper location | ||
# and set the dash pattern | ||
for i in range(numlines): | ||
legline = Line2D(xdata, ydata * (numlines - i) - ydescent) | ||
self.update_prop(legline, orig_handle, legend) | ||
# set color, dash pattern, and linewidth to that | ||
# of the lines in linecollection | ||
try: | ||
color = orig_handle.get_colors()[i] | ||
except IndexError: | ||
color = orig_handle.get_colors()[0] | ||
try: | ||
dashes = orig_handle.get_dashes()[i] | ||
except IndexError: | ||
dashes = orig_handle.get_dashes()[0] | ||
try: | ||
lw = orig_handle.get_linewidths()[i] | ||
except IndexError: | ||
lw = orig_handle.get_linewidths()[0] | ||
if dashes[0] != None: | ||
legline.set_dashes(dashes[1]) | ||
legline.set_color(color) | ||
legline.set_transform(trans) | ||
legline.set_linewidth(lw) | ||
leglines.append(legline) | ||
return leglines | ||
|
||
x = np.linspace(0, 5, 100) | ||
|
||
plt.figure() | ||
colors = ['red', 'orange', 'yellow', 'green', 'blue'] | ||
styles = ['solid', 'dashed', 'dashed', 'dashed', 'solid'] | ||
lines = [] | ||
for i, color, style in zip(range(5), colors, styles): | ||
plt.plot(x, np.sin(x) - .1 * i, c=color, ls=style) | ||
|
||
|
||
# make proxy artists | ||
# make list of one line -- doesn't matter what the coordinates are | ||
line = [[(0, 0)]] | ||
# set up the proxy artist | ||
lc = mcol.LineCollection(5 * line, linestyles=styles, colors=colors) | ||
# create the legend | ||
plt.legend([lc], ['multi-line'], handler_map={type(lc): HandlerDashedLines()}, | ||
handlelength=2.5, handleheight=3) | ||
|
||
plt.show() |