Skip to content
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

ENH: support more style params for guide_colorbar #701

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
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
5 changes: 4 additions & 1 deletion doc/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,13 @@ v0.12.2
Enhancements
************

- :class:`~plotnin.guide.guide_colorbar` Now supports additional
styling parameters: `frame_color`, `frame_linewidth`,
`ticks_color`, `ticks_linewidth`.

- All `__all__` variables are explicitly assigned to help static typecheckers
infer module attributes. (:issue:`685`)


v0.12.1
-------
(2023-05-09)
Expand Down
48 changes: 44 additions & 4 deletions plotnine/guides/guide_colorbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,16 @@ class guide_colorbar(guide):
a smoother colorbar. Default is 20.
raster : bool
Whether to render the colorbar as a raster object.
frame_color : None
Color of surrounding frame. If None, no frame is drawn.
frame_linewidth : float
Line width of frame. Default is 1.
ticks : bool
Whether tick marks on colorbar should be visible.
ticks_color : str
Color to use for ticks. Default is "#CCCCCC".
ticks_linewidth : float
Width of the tick marks. Default is 1.
draw_ulim : bool
Whether to show the upper limit tick marks.
draw_llim : bool
Expand All @@ -51,9 +59,13 @@ class guide_colorbar(guide):
barheight = None
nbin = 20 # maximum number of bins
raster = True
frame_color = None
frame_linewidth = 1

# ticks
ticks = True
ticks_color = "#CCCCCC"
ticks_linewidth = 1
draw_ulim = True
draw_llim = True

Expand Down Expand Up @@ -214,7 +226,18 @@ def draw(self):
if not self.draw_llim:
_locations = _locations[1:]

add_ticks(da, _locations, direction)
add_ticks(
da,
_locations,
direction,
self.ticks_color,
self.ticks_linewidth,
)

if self.frame_color:
add_frame(
da, width, height, self.frame_color, self.frame_linewidth
)

# labels #
if self.label:
Expand Down Expand Up @@ -262,6 +285,24 @@ def draw(self):
return box


def add_frame(da, width, height, color, linewidth):
"""
Add frame to colorbar
"""
from matplotlib.patches import Rectangle

rect = Rectangle(
(0, 0),
width,
height,
edgecolor=color,
linewidth=linewidth,
facecolor="none",
snap=True,
)
da.add_artist(rect)


def add_interpolated_colorbar(da, colors, direction):
"""
Add 'rastered' colorbar to DrawingArea
Expand Down Expand Up @@ -307,7 +348,6 @@ def add_interpolated_colorbar(da, colors, direction):
coordinates,
antialiased=False,
shading="gouraud",
linewidth=0,
cmap=cmap,
array=Z.ravel(),
)
Expand Down Expand Up @@ -344,7 +384,7 @@ def add_segmented_colorbar(da, colors, direction):
da.add_artist(coll)


def add_ticks(da, locations, direction):
def add_ticks(da, locations, direction, color, linewidth):
from matplotlib.collections import LineCollection

segments = []
Expand All @@ -368,7 +408,7 @@ def add_ticks(da, locations, direction):
)

coll = LineCollection(
segments, color="#CCCCCC", linewidth=1, antialiased=False
segments, color=color, linewidth=linewidth, antialiased=False
)
da.add_artist(coll)

Expand Down
Loading