Skip to content

Set facecolor of FilledArrow axisline style and fix tight layout #24085

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

Merged
merged 4 commits into from
Jan 7, 2023
Merged
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
6 changes: 6 additions & 0 deletions lib/mpl_toolkits/axisartist/axis_artist.py
Original file line number Diff line number Diff line change
Expand Up @@ -1044,11 +1044,17 @@ def get_tightbbox(self, renderer=None):
self._axis_artist_helper.update_lim(self.axes)
self._update_ticks(renderer)
self._update_label(renderer)

self.line.set_path(self._axis_artist_helper.get_line(self.axes))
if self.get_axisline_style() is not None:
self.line.set_line_mutation_scale(self.major_ticklabels.get_size())

bb = [
*self.major_ticklabels.get_window_extents(renderer),
*self.minor_ticklabels.get_window_extents(renderer),
self.label.get_window_extent(renderer),
self.offsetText.get_window_extent(renderer),
self.line.get_window_extent(renderer),
]
bb = [b for b in bb if b and (b.width != 0 or b.height != 0)]
if bb:
Expand Down
60 changes: 54 additions & 6 deletions lib/mpl_toolkits/axisartist/axisline_style.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
"""
Provides classes to style the axis lines.
"""
import math

import numpy as np

import matplotlib as mpl
from matplotlib.patches import _Style, FancyArrowPatch
from matplotlib.transforms import IdentityTransform
from matplotlib.path import Path
from matplotlib.transforms import IdentityTransform


class _FancyAxislineStyle:
Expand Down Expand Up @@ -54,10 +58,10 @@ def set_path(self, path):
def draw(self, renderer):
"""
Draw the axis line.
1) transform the path to the display coordinate.
2) extend the path to make a room for arrow
3) update the path of the FancyArrowPatch.
4) draw
1) Transform the path to the display coordinate.
2) Extend the path to make a room for arrow.
3) Update the path of the FancyArrowPatch.
4) Draw.
"""
path_in_disp = self._line_transform.transform_path(self._line_path)
mutation_size = self.get_mutation_scale() # line_mutation_scale()
Expand All @@ -66,10 +70,25 @@ def draw(self, renderer):
self._path_original = extended_path
FancyArrowPatch.draw(self, renderer)

def get_window_extent(self, renderer=None):

path_in_disp = self._line_transform.transform_path(self._line_path)
mutation_size = self.get_mutation_scale() # line_mutation_scale()
extended_path = self._extend_path(path_in_disp,
mutation_size=mutation_size)
self._path_original = extended_path
return FancyArrowPatch.get_window_extent(self, renderer)

class FilledArrow(SimpleArrow):
"""The artist class that will be returned for SimpleArrow style."""
"""The artist class that will be returned for FilledArrow style."""
_ARROW_STYLE = "-|>"

def __init__(self, axis_artist, line_path, transform,
line_mutation_scale, facecolor):
super().__init__(axis_artist, line_path, transform,
line_mutation_scale)
self.set_facecolor(facecolor)


class AxislineStyle(_Style):
"""
Expand Down Expand Up @@ -140,6 +159,35 @@ def new_line(self, axis_artist, transform):
_style_list["->"] = SimpleArrow

class FilledArrow(SimpleArrow):
"""
An arrow with a filled head.
"""

ArrowAxisClass = _FancyAxislineStyle.FilledArrow

def __init__(self, size=1, facecolor=None):
"""
Parameters
----------
size : float
Size of the arrow as a fraction of the ticklabel size.
facecolor : color, default: :rc:`axes.edgecolor`
Fill color.

.. versionadded:: 3.7
"""

if facecolor is None:
facecolor = mpl.rcParams['axes.edgecolor']
self.size = size
self._facecolor = facecolor
super().__init__(size=size)

def new_line(self, axis_artist, transform):
linepath = Path([(0, 0), (0, 1)])
axisline = self.ArrowAxisClass(axis_artist, linepath, transform,
line_mutation_scale=self.size,
facecolor=self._facecolor)
return axisline

_style_list["-|>"] = FilledArrow
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
60 changes: 59 additions & 1 deletion lib/mpl_toolkits/axisartist/tests/test_axislines.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from matplotlib.testing.decorators import image_comparison
from matplotlib.transforms import IdentityTransform

from mpl_toolkits.axisartist.axislines import SubplotZero, Subplot
from mpl_toolkits.axisartist.axislines import AxesZero, SubplotZero, Subplot
from mpl_toolkits.axisartist import Axes, SubplotHost


Expand Down Expand Up @@ -90,3 +90,61 @@ def test_ParasiteAxesAuxTrans():
ax1.set_ylim((0, 5))

ax2.contour(xx, yy, data, colors='k')


@image_comparison(['axisline_style.png'], remove_text=True, style='mpl20')
def test_axisline_style():
fig = plt.figure(figsize=(2, 2))
ax = fig.add_subplot(axes_class=AxesZero)
ax.axis["xzero"].set_axisline_style("-|>")
ax.axis["xzero"].set_visible(True)
ax.axis["yzero"].set_axisline_style("->")
ax.axis["yzero"].set_visible(True)

for direction in ("left", "right", "bottom", "top"):
ax.axis[direction].set_visible(False)


@image_comparison(['axisline_style_size_color.png'], remove_text=True,
style='mpl20')
def test_axisline_style_size_color():
fig = plt.figure(figsize=(2, 2))
ax = fig.add_subplot(axes_class=AxesZero)
ax.axis["xzero"].set_axisline_style("-|>", size=2.0, facecolor='r')
ax.axis["xzero"].set_visible(True)
ax.axis["yzero"].set_axisline_style("->, size=1.5")
ax.axis["yzero"].set_visible(True)

for direction in ("left", "right", "bottom", "top"):
ax.axis[direction].set_visible(False)


@image_comparison(['axisline_style_tight.png'], remove_text=True,
style='mpl20')
def test_axisline_style_tight():
fig = plt.figure(figsize=(2, 2))
ax = fig.add_subplot(axes_class=AxesZero)
ax.axis["xzero"].set_axisline_style("-|>", size=5, facecolor='g')
ax.axis["xzero"].set_visible(True)
ax.axis["yzero"].set_axisline_style("->, size=8")
ax.axis["yzero"].set_visible(True)

for direction in ("left", "right", "bottom", "top"):
ax.axis[direction].set_visible(False)

fig.tight_layout()


@image_comparison(['subplotzero_ylabel.png'], style='mpl20')
def test_subplotzero_ylabel():
fig = plt.figure()
ax = fig.add_subplot(111, axes_class=SubplotZero)

ax.set(xlim=(-3, 7), ylim=(-3, 7), xlabel="x", ylabel="y")

zero_axis = ax.axis["xzero", "yzero"]
zero_axis.set_visible(True) # they are hidden by default

ax.axis["left", "right", "bottom", "top"].set_visible(False)

zero_axis.set_axisline_style("->")