Skip to content

Commit

Permalink
0.3.4
Browse files Browse the repository at this point in the history
  • Loading branch information
Mr-Milk committed Mar 30, 2024
1 parent 6580e4e commit 3475766
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 25 deletions.
1 change: 1 addition & 0 deletions docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
'legend.borderaxespad': 0.5,
'legend.columnspacing': 1.0
}
plot_apply_rcparams = True
plot_pre_code = """
import numpy as np
from matplotlib import pyplot as plt
Expand Down
8 changes: 5 additions & 3 deletions docs/source/cover.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.colors import Normalize

from legendkit import legend, cat_legend, colorbar, colorart, hstack, vstack
from legendkit import legend, cat_legend, colorbar, colorart, hstack, vstack, size_legend

mpl.rcParams['legend.handleheight'] = 1
mpl.rcParams['legend.handlelength'] = 1
Expand Down Expand Up @@ -83,9 +84,10 @@
colors=["#A7D2CB", "#F2D388", "#A7D2CB", "#F2D388"],
labels=["Item 1", "Item 2", "Item 3", "Item 4"],
)
legend = cat_legend(**args, title="Legend", handle="circle")
legend1 = cat_legend(**args, title="Category", handle="circle")
legend2 = size_legend(sizes=np.arange(1, 401), title="Size", handle="circle")
cart = colorart(norm=norm, cmap="cool", ax=ax, title="Colorart")
hstack([legend, cart], spacing=20, title="Stack colorbar and legend",
hstack([legend1, legend2, cart], spacing=20, title="Stack colorbar and legend",
alignment="left",
loc="upper left", bbox_to_anchor=(1, 1), bbox_transform=ax.transAxes,
frameon=True, ax=ax, padding=2)
Expand Down
4 changes: 2 additions & 2 deletions legendkit/__init__.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
"""Legend creation and manipulation with ease for matplotlib"""

__version__ = "0.3.3"
__version__ = "0.3.4"

from ._colorbar import Colorbar
from ._colorart import ColorArt
from ._colorbar import Colorbar
from ._legend import ListLegend, CatLegend, SizeLegend
# To register default setting and legend handlers
from ._register import register
Expand Down
29 changes: 21 additions & 8 deletions legendkit/_colorart.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,10 +159,10 @@ def __init__(self,
self.is_axes = True
if not isinstance(ax, Axes):
self.is_axes = False
self.set_figure(ax)
self.figure = ax

else:
self.set_figure(ax.figure)
self.figure = ax.figure
self.axes = ax
if rasterized:
# Force rasterization
Expand Down Expand Up @@ -271,7 +271,6 @@ def __init__(self,
Locs().transform(ax, loc, bbox_to_anchor=bbox_to_anchor,
bbox_transform=bbox_transform,
deviation=deviation)
print(self._bbox_to_anchor)

self.textpad = mpl.rcParams[
'legend.handletextpad'] if textpad is None else textpad
Expand All @@ -281,6 +280,7 @@ def __init__(self,
'legend.borderaxespad'] if borderaxespad is None else borderaxespad

# the container for title, colorbar, ticks and tick labels
self._final_pack = None
self._cbar_box = None
self._process_values()
self._get_locator_formatter()
Expand Down Expand Up @@ -321,7 +321,8 @@ def _make_cbar_box(self):
# Add cbar
canvas = DrawingArea(da_width, da_height, clip=False)
# self._add_color_patches(self._cbar_canvas)
canvas.set_figure(self.figure)
# canvas.set_figure(self.figure)
canvas.figure = self.figure

cmap_caller = get_colormap(self.cmap)
colors_list = cmap_caller(np.arange(cmap_caller.N))
Expand All @@ -334,10 +335,10 @@ def _make_cbar_box(self):
if self.orientation == "vertical":
for i, (y1, y2) in enumerate(zip(locs, locs[1::])):
rects.append(Rectangle((0, y1), width=self.width,
height=y2-y1, fc=colors_list[i]))
height=y2 - y1, fc=colors_list[i]))
else:
for i, (x1, x2) in enumerate(zip(locs, locs[1::])):
rects.append(Rectangle((x1, 0), width=x2-x1,
rects.append(Rectangle((x1, 0), width=x2 - x1,
height=self.height,
fc=colors_list[i]))
else:
Expand Down Expand Up @@ -405,23 +406,35 @@ def _make_cbar_box(self):
children=[title_canvas, canvas],
align=self.alignment
)
title_pack.set_figure(self.figure)
# title_pack.set_figure(self.figure)
title_pack.figure = self.figure
final_pack = title_pack
else:
final_pack = canvas
self._final_pack = final_pack
self._cbar_box = AnchoredOffsetbox(
self._loc, child=final_pack,
pad=self.borderpad,
borderpad=self.borderaxespad,
bbox_transform=self._bbox_transform,
bbox_to_anchor=self._bbox_to_anchor,
frameon=False)
self._cbar_box.set_figure(self.figure)
# self._cbar_box.set_figure(self.figure)
self._cbar_box.figure = self.figure
if self.is_axes:
self.axes.add_artist(self._cbar_box)
else:
self.figure.add_artist(self._cbar_box)

def get_bbox(self, renderer=None):
return self._final_pack.get_bbox(renderer=renderer)

def get_window_extent(self, renderer=None):
return self._final_pack.get_window_extent(renderer=renderer)

def set_offset(self, offset):
self._final_pack.set_offset(offset)

def _get_text_size(self, ticklabels):
"""Used to get the proper size for drawing area"""
renderer = self.figure.canvas.get_renderer()
Expand Down
2 changes: 1 addition & 1 deletion legendkit/_colorbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,4 +205,4 @@ def get_corner(self):
xlims = np.sort(self.ax.get_xlim())
ylims = np.sort(self.ax.get_ylim())
return (xlims[0], ylims[0]), (xlims[0], ylims[1]), \
(xlims[1], ylims[1]), (xlims[1], ylims[0])
(xlims[1], ylims[1]), (xlims[1], ylims[0])
25 changes: 16 additions & 9 deletions legendkit/_legend.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
import numpy as np
from matplotlib import _api
from matplotlib.axes import Axes
from matplotlib.collections import Collection, PatchCollection
from matplotlib.colors import is_color_like, Normalize
from matplotlib.collections import Collection
from matplotlib.colors import is_color_like
from matplotlib.figure import FigureBase
from matplotlib.font_manager import FontProperties
from matplotlib.legend import Legend
from matplotlib.lines import Line2D
from matplotlib.markers import MarkerStyle
from matplotlib.offsetbox import VPacker, HPacker
from matplotlib.patches import Patch, Rectangle
from matplotlib.patches import Patch

from ._handlers import CircleHandler, RectHandler, BoxplotHanlder
from ._locs import Locs
Expand Down Expand Up @@ -210,14 +210,20 @@ def __init__(self,
title_loc=title_loc)
self._has_parent = ax is not None
self._is_axes = isinstance(ax, Axes)
parent = None
if ax is None:
axes = plt.gca()
parent = plt.gca()
axes = [parent]
self._is_axes = True
else:
if not self._is_axes:
fig = ax
axes = fig.get_axes()
parent = fig
else:
axes = [ax]
parent = ax

self._title_loc = title_loc
self.titlepad = titlepad
self._is_patch = False
Expand Down Expand Up @@ -281,7 +287,7 @@ def val_or_rc(val, rc_name):
loc = "center right"
else:
loc, bbox_to_anchor, bbox_transform = \
Locs().transform(ax, loc, bbox_to_anchor=bbox_to_anchor,
Locs().transform(parent, loc, bbox_to_anchor=bbox_to_anchor,
bbox_transform=bbox_transform,
deviation=deviation)
if handler_map is None:
Expand All @@ -304,7 +310,7 @@ def val_or_rc(val, rc_name):
)

final_options = {**default_kwargs, **kwargs}
super().__init__(ax,
super().__init__(parent,
handles=legend_handles,
labels=legend_labels,
**final_options)
Expand All @@ -317,13 +323,14 @@ def val_or_rc(val, rc_name):
# Attach as legend element
# 1. ax.get_legend() will work
# 2. legend won't be clipped
if isinstance(ax, Axes):
if self._is_axes:
ax = axes[0]
if ax.legend_ is None:
ax.legend_ = self
else:
ax.add_artist(self)
else:
ax.legends.append(self)
fig.legends.append(self)

def _parse_handler(self, handle, handle_size, config=None):
if not isinstance(handle, str):
Expand Down Expand Up @@ -386,7 +393,7 @@ def _title_layout(self):
self._legend_box = packer(pad=pad, sep=sep,
align=alignment, children=children)

self._legend_box.set_figure(self.figure)
self._legend_box.figure = self.figure
self._legend_box.axes = self.axes

# call this to maintain consistent behavior as legend
Expand Down
3 changes: 2 additions & 1 deletion legendkit/handles.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
"""
from abc import ABC

from matplotlib.collections import Collection
from matplotlib.lines import Line2D
from matplotlib.patches import Patch
from matplotlib.collections import Collection


class SquareItem(Patch, ABC):
Expand All @@ -48,6 +48,7 @@ def __init__(self, *args, **kwargs):

class BoxplotItem(Collection, ABC):
"""Create boxplot for legend handles"""

def __init__(self, *args, **kwargs):
user_ec = kwargs.get('ec')
user_edgecolor = kwargs.get('edgecolor')
Expand Down
2 changes: 1 addition & 1 deletion legendkit/layout.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from functools import partial
from typing import List, Optional, Dict
from typing import List, Dict

from matplotlib.artist import Artist
from matplotlib.legend import Legend
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,5 @@ dev = [
"numpydoc",
"sphinx_gallery",
"furo",
"mpl_fontkit"
]

0 comments on commit 3475766

Please sign in to comment.