Skip to content

Commit

Permalink
Merge pull request #348 from nschloe/legend-opacity
Browse files Browse the repository at this point in the history
Legend opacity
  • Loading branch information
nschloe committed Nov 23, 2019
2 parents e44a75c + 16f166b commit 7e2e54e
Show file tree
Hide file tree
Showing 16 changed files with 47 additions and 39 deletions.
2 changes: 1 addition & 1 deletion test/test_barchart_legend_reference.tex
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

\begin{axis}[
legend cell align={left},
legend style={draw=white!80.0!black},
legend style={fill opacity=0.8, draw opacity=1, text opacity=1, draw=white!80.0!black},
tick align=outside,
tick pos=left,
x grid style={white!69.01960784313725!black},
Expand Down
2 changes: 1 addition & 1 deletion test/test_basic_sin_reference.tex
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
axis background/.style={fill=white!89.80392156862746!black},
axis line style={white},
legend cell align={left},
legend style={at={(0.03,0.03)}, anchor=south west, draw=white!80.0!black, fill=white!89.80392156862746!black},
legend style={fill opacity=0.8, draw opacity=1, text opacity=1, at={(0.03,0.03)}, anchor=south west, draw=white!80.0!black, fill=white!89.80392156862746!black},
tick align=outside,
tick pos=left,
title={Simple plot \(\displaystyle \frac{\alpha}{2}\)},
Expand Down
2 changes: 1 addition & 1 deletion test/test_histogram_reference.tex
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

\begin{axis}[
legend cell align={left},
legend style={draw=white!80.0!black},
legend style={fill opacity=0.8, draw opacity=1, text opacity=1, draw=white!80.0!black},
tick align=outside,
tick pos=left,
x grid style={white!69.01960784313725!black},
Expand Down
2 changes: 1 addition & 1 deletion test/test_legend3_reference.tex
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

\begin{axis}[
legend cell align={left},
legend style={at={(0.03,0.97)}, anchor=north west, draw=white!80.0!black},
legend style={fill opacity=0.8, draw opacity=1, text opacity=1, at={(0.03,0.97)}, anchor=north west, draw=white!80.0!black},
tick align=outside,
tick pos=left,
x grid style={white!69.01960784313725!black},
Expand Down
16 changes: 8 additions & 8 deletions test/test_legend_best_location.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,39 +10,39 @@ def plot():
t = np.arange(0.0, 2.0 * np.pi, 0.4)

# Legend best location is "upper right"
l, = axes[0].plot(t, np.cos(t) * np.exp(-t), linewidth=0.5)
(l,) = axes[0].plot(t, np.cos(t) * np.exp(-t), linewidth=0.5)
axes[0].legend((l,), ("UR",), loc=0)

# Legend best location is "upper left"
l, = axes[1].plot(t, np.cos(t) * np.exp(0.15 * t), linewidth=0.5)
(l,) = axes[1].plot(t, np.cos(t) * np.exp(0.15 * t), linewidth=0.5)
axes[1].legend((l,), ("UL",), loc=0)

# Legend best location is "lower left"
l, = axes[2].plot(t, np.cos(5.0 * t) + 1, linewidth=0.5)
(l,) = axes[2].plot(t, np.cos(5.0 * t) + 1, linewidth=0.5)
axes[2].legend((l,), ("LL",), loc=0)

# Legend best location is "lower right"
l, = axes[3].plot(
(l,) = axes[3].plot(
t, 2 * np.cos(5.0 * t) * np.exp(-0.5 * t) + 0.2 * t, linewidth=0.5
)
axes[3].legend((l,), ("LR",), loc=0)

# Legend best location is "center left"
l, = axes[4].plot(t[30:], 2 * np.cos(10 * t[30:]), linewidth=0.5)
(l,) = axes[4].plot(t[30:], 2 * np.cos(10 * t[30:]), linewidth=0.5)
axes[4].plot(t, -1.5 * np.ones_like(t), t, 1.5 * np.ones_like(t))
axes[4].legend((l,), ("CL",), loc=0)

# Legend best location is "center right"
l, = axes[5].plot(t[:30], 2 * np.cos(10 * t[:30]), linewidth=0.5)
(l,) = axes[5].plot(t[:30], 2 * np.cos(10 * t[:30]), linewidth=0.5)
axes[5].plot(t, -1.5 * np.ones_like(t), t, 1.5 * np.ones_like(t))
axes[5].legend((l,), ("CR",), loc=0)

# Legend best location is "lower center"
l, = axes[6].plot(t, -3 * np.cos(t) * np.exp(-0.1 * t), linewidth=0.5)
(l,) = axes[6].plot(t, -3 * np.cos(t) * np.exp(-0.1 * t), linewidth=0.5)
axes[6].legend((l,), ("LC",), loc=0)

# Legend best location is "upper center"
l, = axes[7].plot(t, 3 * np.cos(t) * np.exp(-0.1 * t), linewidth=0.5)
(l,) = axes[7].plot(t, 3 * np.cos(t) * np.exp(-0.1 * t), linewidth=0.5)
axes[7].legend((l,), ("UC",), loc=0)

# Legend best location is "center"
Expand Down
18 changes: 9 additions & 9 deletions test/test_legend_best_location_reference.tex
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
\begin{groupplot}[group style={group size=3 by 3}]
\nextgroupplot[
legend cell align={left},
legend style={draw=white!80.0!black},
legend style={fill opacity=0.8, draw opacity=1, text opacity=1, draw=white!80.0!black},
tick align=outside,
tick pos=left,
x grid style={white!69.01960784313725!black},
Expand Down Expand Up @@ -41,7 +41,7 @@

\nextgroupplot[
legend cell align={left},
legend style={at={(0.03,0.97)}, anchor=north west, draw=white!80.0!black},
legend style={fill opacity=0.8, draw opacity=1, text opacity=1, at={(0.03,0.97)}, anchor=north west, draw=white!80.0!black},
tick align=outside,
tick pos=left,
x grid style={white!69.01960784313725!black},
Expand Down Expand Up @@ -74,7 +74,7 @@

\nextgroupplot[
legend cell align={left},
legend style={at={(0.03,0.03)}, anchor=south west, draw=white!80.0!black},
legend style={fill opacity=0.8, draw opacity=1, text opacity=1, at={(0.03,0.03)}, anchor=south west, draw=white!80.0!black},
tick align=outside,
tick pos=left,
x grid style={white!69.01960784313725!black},
Expand Down Expand Up @@ -107,7 +107,7 @@

\nextgroupplot[
legend cell align={left},
legend style={at={(0.03,0.03)}, anchor=south west, draw=white!80.0!black},
legend style={fill opacity=0.8, draw opacity=1, text opacity=1, at={(0.03,0.03)}, anchor=south west, draw=white!80.0!black},
tick align=outside,
tick pos=left,
x grid style={white!69.01960784313725!black},
Expand Down Expand Up @@ -140,7 +140,7 @@

\nextgroupplot[
legend cell align={left},
legend style={at={(0.91,0.5)}, anchor=east, draw=white!80.0!black},
legend style={fill opacity=0.8, draw opacity=1, text opacity=1, at={(0.91,0.5)}, anchor=east, draw=white!80.0!black},
tick align=outside,
tick pos=left,
x grid style={white!69.01960784313725!black},
Expand Down Expand Up @@ -191,7 +191,7 @@

\nextgroupplot[
legend cell align={left},
legend style={at={(0.09,0.5)}, anchor=west, draw=white!80.0!black},
legend style={fill opacity=0.8, draw opacity=1, text opacity=1, at={(0.09,0.5)}, anchor=west, draw=white!80.0!black},
tick align=outside,
tick pos=left,
x grid style={white!69.01960784313725!black},
Expand Down Expand Up @@ -262,7 +262,7 @@

\nextgroupplot[
legend cell align={left},
legend style={at={(0.5,0.09)}, anchor=south, draw=white!80.0!black},
legend style={fill opacity=0.8, draw opacity=1, text opacity=1, at={(0.5,0.09)}, anchor=south, draw=white!80.0!black},
tick align=outside,
tick pos=left,
x grid style={white!69.01960784313725!black},
Expand Down Expand Up @@ -295,7 +295,7 @@

\nextgroupplot[
legend cell align={left},
legend style={at={(0.5,0.91)}, anchor=north, draw=white!80.0!black},
legend style={fill opacity=0.8, draw opacity=1, text opacity=1, at={(0.5,0.91)}, anchor=north, draw=white!80.0!black},
tick align=outside,
tick pos=left,
x grid style={white!69.01960784313725!black},
Expand Down Expand Up @@ -327,7 +327,7 @@
\addlegendentry{UC}

\nextgroupplot[
legend style={draw=white!80.0!black},
legend style={fill opacity=0.8, draw opacity=1, text opacity=1, draw=white!80.0!black},
tick align=outside,
tick pos=left,
x grid style={white!69.01960784313725!black},
Expand Down
2 changes: 1 addition & 1 deletion test/test_legend_columns_reference.tex
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
\begin{axis}[
legend cell align={left},
legend columns=2,
legend style={at={(0.5,0.91)}, anchor=north, draw=white!80.0!black},
legend style={fill opacity=0.8, draw opacity=1, text opacity=1, at={(0.5,0.91)}, anchor=north, draw=white!80.0!black},
tick align=outside,
tick pos=left,
x grid style={white!69.01960784313725!black},
Expand Down
2 changes: 1 addition & 1 deletion test/test_legend_labels_reference.tex
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

\begin{axis}[
legend cell align={left},
legend style={draw=white!80.0!black},
legend style={fill opacity=0.8, draw opacity=1, text opacity=1, draw=white!80.0!black},
tick align=outside,
tick pos=left,
x grid style={white!69.01960784313725!black},
Expand Down
2 changes: 1 addition & 1 deletion test/test_legend_line_scatter_reference.tex
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

\begin{axis}[
legend cell align={left},
legend style={at={(0.03,0.97)}, anchor=north west, draw=white!80.0!black},
legend style={fill opacity=0.8, draw opacity=1, text opacity=1, at={(0.03,0.97)}, anchor=north west, draw=white!80.0!black},
tick align=outside,
tick pos=left,
x grid style={white!69.01960784313725!black},
Expand Down
2 changes: 1 addition & 1 deletion test/test_legends2.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def plot():
# note that plot returns a list of lines. The 'l1, = plot' usage
# extracts the first element of the list inot l1 using tuple unpacking.
# So l1 is a Line2D instance, not a sequence of lines
l1, = axes[k].plot(t2, np.exp(-t2), linewidth=0.5)
(l1,) = axes[k].plot(t2, np.exp(-t2), linewidth=0.5)
axes[k].plot(t2, np.sin(2 * np.pi * t2), "--go", t1, np.log(1 + t1), ".")
axes[k].plot(t2, np.exp(-t2) * np.sin(2 * np.pi * t2), "rs-.")

Expand Down
18 changes: 9 additions & 9 deletions test/test_legends2_reference.tex
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
\begin{groupplot}[group style={group size=3 by 3}]
\nextgroupplot[
legend cell align={left},
legend style={at={(0.03,0.97)}, anchor=north west, draw=white!80.0!black},
legend style={fill opacity=0.8, draw opacity=1, text opacity=1, at={(0.03,0.97)}, anchor=north west, draw=white!80.0!black},
tick align=outside,
tick pos=left,
x grid style={white!69.01960784313725!black},
Expand Down Expand Up @@ -52,7 +52,7 @@

\nextgroupplot[
legend cell align={left},
legend style={at={(0.03,0.03)}, anchor=south west, draw=white!80.0!black},
legend style={fill opacity=0.8, draw opacity=1, text opacity=1, at={(0.03,0.03)}, anchor=south west, draw=white!80.0!black},
tick align=outside,
tick pos=left,
x grid style={white!69.01960784313725!black},
Expand Down Expand Up @@ -98,7 +98,7 @@

\nextgroupplot[
legend cell align={left},
legend style={at={(0.97,0.03)}, anchor=south east, draw=white!80.0!black},
legend style={fill opacity=0.8, draw opacity=1, text opacity=1, at={(0.97,0.03)}, anchor=south east, draw=white!80.0!black},
tick align=outside,
tick pos=left,
x grid style={white!69.01960784313725!black},
Expand Down Expand Up @@ -144,7 +144,7 @@

\nextgroupplot[
legend cell align={left},
legend style={at={(0.97,0.5)}, anchor=east, draw=white!80.0!black},
legend style={fill opacity=0.8, draw opacity=1, text opacity=1, at={(0.97,0.5)}, anchor=east, draw=white!80.0!black},
tick align=outside,
tick pos=left,
x grid style={white!69.01960784313725!black},
Expand Down Expand Up @@ -190,7 +190,7 @@

\nextgroupplot[
legend cell align={left},
legend style={at={(0.09,0.5)}, anchor=west, draw=white!80.0!black},
legend style={fill opacity=0.8, draw opacity=1, text opacity=1, at={(0.09,0.5)}, anchor=west, draw=white!80.0!black},
tick align=outside,
tick pos=left,
x grid style={white!69.01960784313725!black},
Expand Down Expand Up @@ -236,7 +236,7 @@

\nextgroupplot[
legend cell align={left},
legend style={at={(0.91,0.5)}, anchor=east, draw=white!80.0!black},
legend style={fill opacity=0.8, draw opacity=1, text opacity=1, at={(0.91,0.5)}, anchor=east, draw=white!80.0!black},
tick align=outside,
tick pos=left,
x grid style={white!69.01960784313725!black},
Expand Down Expand Up @@ -282,7 +282,7 @@

\nextgroupplot[
legend cell align={left},
legend style={at={(0.5,0.09)}, anchor=south, draw=white!80.0!black},
legend style={fill opacity=0.8, draw opacity=1, text opacity=1, at={(0.5,0.09)}, anchor=south, draw=white!80.0!black},
tick align=outside,
tick pos=left,
x grid style={white!69.01960784313725!black},
Expand Down Expand Up @@ -328,7 +328,7 @@

\nextgroupplot[
legend cell align={left},
legend style={at={(0.5,0.91)}, anchor=north, draw=white!80.0!black},
legend style={fill opacity=0.8, draw opacity=1, text opacity=1, at={(0.5,0.91)}, anchor=north, draw=white!80.0!black},
tick align=outside,
tick pos=left,
x grid style={white!69.01960784313725!black},
Expand Down Expand Up @@ -374,7 +374,7 @@

\nextgroupplot[
legend cell align={left},
legend style={at={(0.5,0.5)}, anchor=center, draw=white!80.0!black},
legend style={fill opacity=0.8, draw opacity=1, text opacity=1, at={(0.5,0.5)}, anchor=center, draw=white!80.0!black},
tick align=outside,
tick pos=left,
x grid style={white!69.01960784313725!black},
Expand Down
2 changes: 1 addition & 1 deletion test/test_legends_reference.tex
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

\begin{axis}[
legend cell align={left},
legend style={draw=white!80.0!black},
legend style={fill opacity=0.8, draw opacity=1, text opacity=1, draw=white!80.0!black},
tick align=outside,
tick pos=left,
title={Masked line demo},
Expand Down
2 changes: 1 addition & 1 deletion test/test_steps_reference.tex
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

\begin{axis}[
legend cell align={left},
legend style={at={(0.03,0.97)}, anchor=north west, draw=white!80.0!black},
legend style={fill opacity=0.8, draw opacity=1, text opacity=1, at={(0.03,0.97)}, anchor=north west, draw=white!80.0!black},
tick align=outside,
tick pos=left,
x grid style={white!69.01960784313725!black},
Expand Down
2 changes: 1 addition & 1 deletion test/test_text_overlay_reference.tex
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

\begin{axis}[
legend cell align={left},
legend style={at={(0.03,0.97)}, anchor=north west, draw=white!80.0!black},
legend style={fill opacity=0.8, draw opacity=1, text opacity=1, at={(0.03,0.97)}, anchor=north west, draw=white!80.0!black},
tick align=outside,
tick pos=left,
x grid style={white!69.01960784313725!black},
Expand Down
2 changes: 1 addition & 1 deletion tikzplotlib/__about__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
__email__ = "nico.schloemer@gmail.com"
__copyright__ = "Copyright (c) 2010-2019, {} <{}>".format(__author__, __email__)
__license__ = "License :: OSI Approved :: MIT License"
__version__ = "0.8.5"
__version__ = "0.8.6"
__status__ = "Development Status :: 5 - Production/Stable"
10 changes: 9 additions & 1 deletion tikzplotlib/_legend.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,15 @@ def draw_legend(data, obj):
bbox_center = obj.get_bbox_to_anchor()._bbox._points[1]
position = [bbox_center[0], bbox_center[1]]

legend_style = []
legend_style = [
# These are the mpl default setting for legends, see
# <https://matplotlib.org/3.1.1/api/legend_api.html#matplotlib.legend.Legend>.
# Keep an eye on <https://github.com/matplotlib/matplotlib/issues/15764> to see
# if we can get() the opacity from the legend object.
"fill opacity=0.8",
"draw opacity=1",
"text opacity=1",
]
if position:
ff = data["float format"]
legend_style.append(
Expand Down

0 comments on commit 7e2e54e

Please sign in to comment.