Skip to content

Added Tabular legend example #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 43 additions & 3 deletions galleries/examples/text_labels_and_annotations/legend_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import matplotlib.collections as mcol
from matplotlib.legend_handler import HandlerLineCollection, HandlerTuple
from matplotlib.lines import Line2D
from matplotlib.patches import Rectangle

t1 = np.arange(0.0, 2.0, 0.1)
t2 = np.arange(0.0, 2.0, 0.01)
Expand Down Expand Up @@ -96,7 +97,7 @@
# Assign two of the handles to the same legend entry by putting them in a tuple
# and using a generic handler map (which would be used for any additional
# tuples of handles like (p1, p3)).
l = ax1.legend([(p1, p3), p2], ['two keys', 'one key'], scatterpoints=1,
_ = ax1.legend([(p1, p3), p2], ['two keys', 'one key'], scatterpoints=1,
numpoints=1, handler_map={tuple: HandlerTuple(ndivide=None)})

# Second plot: plot two bar charts on top of each other and change the padding
Expand All @@ -109,11 +110,51 @@
rpos = ax2.bar(x_left, y_pos, width=0.5, color='k', label='+1')

# Treat each legend entry differently by using specific `HandlerTuple`s
l = ax2.legend([(rpos, rneg), (rneg, rpos)], ['pad!=0', 'pad=0'],
_ = ax2.legend([(rpos, rneg), (rneg, rpos)], ['pad!=0', 'pad=0'],
handler_map={(rpos, rneg): HandlerTuple(ndivide=None),
(rneg, rpos): HandlerTuple(ndivide=None, pad=0.)})
plt.show()

fig = plt.figure()
ax = fig.add_subplot(111)

# Define the styles and colors
styles = ["--", "--", "--", ".", ".", ".", "^", "^", "^"]
colors = ["r", "g", "b", "r", "g", "b", "r", "g", "b"]

# Use a loop to create the plots
lines = []
for i in range(9):
line, = ax.plot(range(10), np.random.randn(10), colors[i] + styles[i])
lines.append(line)

# create blank rectangle
extra = Rectangle((0, 0), 1, 1, fc="w", fill=False, edgecolor='none', linewidth=0)

# Create organized list containing all handles for table.
# Extra represents empty space
legend_handle = [
extra, extra, extra, extra, extra, lines[0], lines[1], lines[2], extra,
lines[3], lines[4], lines[5], extra, lines[6], lines[7], lines[8]
]

# Define the labels
label_row_1 = [r"$f_{i,j}$", r"$i = 1$", r"$i = 2$", r"$i = 3$"]
label_j_1 = [r"$j = 1$"]
label_j_2 = [r"$j = 2$"]
label_j_3 = [r"$j = 3$"]
label_empty = [""]

# Organize labels for table construction
legend_labels = np.concatenate([
label_row_1, label_j_1, label_empty * 3,
label_j_2, label_empty * 3, label_j_3, label_empty * 3
])

# Create legend
ax.legend(legend_handle, legend_labels, loc=9, ncol=4, shadow=True, handletextpad=-2)

plt.show()
# %%
# Finally, it is also possible to write custom classes that define
# how to stylize legends.
Expand All @@ -125,7 +166,6 @@ class HandlerDashedLines(HandlerLineCollection):
"""
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)
Expand Down