Skip to content

Commit 3fee758

Browse files
committed
Add facecolor to axisline style
1 parent d8bb1a5 commit 3fee758

File tree

2 files changed

+73
-8
lines changed

2 files changed

+73
-8
lines changed

lib/mpl_toolkits/axisartist/axisline_style.py

Lines changed: 45 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1+
"""
2+
Provides classes to style the axis lines.
3+
"""
14
import math
25

36
import numpy as np
47

8+
import matplotlib as mpl
59
from matplotlib.patches import _Style, FancyArrowPatch
6-
from matplotlib.transforms import IdentityTransform
710
from matplotlib.path import Path
11+
from matplotlib.transforms import IdentityTransform
812

913

1014
class _FancyAxislineStyle:
@@ -54,10 +58,10 @@ def set_path(self, path):
5458
def draw(self, renderer):
5559
"""
5660
Draw the axis line.
57-
1) transform the path to the display coordinate.
58-
2) extend the path to make a room for arrow
59-
3) update the path of the FancyArrowPatch.
60-
4) draw
61+
1) Transform the path to the display coordinate.
62+
2) Extend the path to make a room for arrow.
63+
3) Update the path of the FancyArrowPatch.
64+
4) Draw.
6165
"""
6266
path_in_disp = self._line_transform.transform_path(self._line_path)
6367
mutation_size = self.get_mutation_scale() # line_mutation_scale()
@@ -67,9 +71,15 @@ def draw(self, renderer):
6771
FancyArrowPatch.draw(self, renderer)
6872

6973
class FilledArrow(SimpleArrow):
70-
"""The artist class that will be returned for SimpleArrow style."""
74+
"""The artist class that will be returned for FilledArrow style."""
7175
_ARROW_STYLE = "-|>"
7276

77+
def __init__(self, axis_artist, line_path, transform,
78+
line_mutation_scale, facecolor):
79+
super().__init__(axis_artist, line_path, transform,
80+
line_mutation_scale)
81+
self.set_facecolor(facecolor)
82+
7383

7484
class AxislineStyle(_Style):
7585
"""
@@ -131,7 +141,6 @@ def __init__(self, size=1):
131141
super().__init__()
132142

133143
def new_line(self, axis_artist, transform):
134-
135144
linepath = Path([(0, 0), (0, 1)])
136145
axisline = self.ArrowAxisClass(axis_artist, linepath, transform,
137146
line_mutation_scale=self.size)
@@ -140,6 +149,35 @@ def new_line(self, axis_artist, transform):
140149
_style_list["->"] = SimpleArrow
141150

142151
class FilledArrow(SimpleArrow):
152+
"""
153+
An arrow with a filled head.
154+
"""
155+
143156
ArrowAxisClass = _FancyAxislineStyle.FilledArrow
144157

158+
def __init__(self, size=1, facecolor=None):
159+
"""
160+
Parameters
161+
----------
162+
size : float
163+
Size of the arrow as a fraction of the ticklabel size.
164+
facecolor : color, default: :rc:`axes.edgecolor`
165+
Fill color.
166+
167+
.. versionadded:: 3.7
168+
"""
169+
170+
if facecolor is None:
171+
facecolor = mpl.rcParams['axes.edgecolor']
172+
self.size = size
173+
self._facecolor = facecolor
174+
super().__init__(size=size)
175+
176+
def new_line(self, axis_artist, transform):
177+
linepath = Path([(0, 0), (0, 1)])
178+
axisline = self.ArrowAxisClass(axis_artist, linepath, transform,
179+
line_mutation_scale=self.size,
180+
facecolor=self._facecolor)
181+
return axisline
182+
145183
_style_list["-|>"] = FilledArrow

lib/mpl_toolkits/axisartist/tests/test_axislines.py

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from matplotlib.testing.decorators import image_comparison
44
from matplotlib.transforms import IdentityTransform
55

6-
from mpl_toolkits.axisartist.axislines import SubplotZero, Subplot
6+
from mpl_toolkits.axisartist.axislines import AxesZero, SubplotZero, Subplot
77
from mpl_toolkits.axisartist import Axes, SubplotHost
88

99

@@ -90,3 +90,30 @@ def test_ParasiteAxesAuxTrans():
9090
ax1.set_ylim((0, 5))
9191

9292
ax2.contour(xx, yy, data, colors='k')
93+
94+
95+
@image_comparison(['axisline_style.png'], remove_text=True, style='mpl20')
96+
def test_axisline_style():
97+
fig = plt.figure(figsize=(2, 2))
98+
ax = fig.add_subplot(axes_class=AxesZero)
99+
ax.axis["xzero"].set_axisline_style("-|>")
100+
ax.axis["xzero"].set_visible(True)
101+
ax.axis["yzero"].set_axisline_style("->")
102+
ax.axis["yzero"].set_visible(True)
103+
104+
for direction in ("left", "right", "bottom", "top"):
105+
ax.axis[direction].set_visible(False)
106+
107+
108+
@image_comparison(['axisline_style_size_color.png'], remove_text=True,
109+
style='mpl20')
110+
def test_axisline_style_size_color():
111+
fig = plt.figure(figsize=(2, 2))
112+
ax = fig.add_subplot(axes_class=AxesZero)
113+
ax.axis["xzero"].set_axisline_style("-|>", size=2.0, facecolor='r')
114+
ax.axis["xzero"].set_visible(True)
115+
ax.axis["yzero"].set_axisline_style("->, size=1.5")
116+
ax.axis["yzero"].set_visible(True)
117+
118+
for direction in ("left", "right", "bottom", "top"):
119+
ax.axis[direction].set_visible(False)

0 commit comments

Comments
 (0)