8000 Merge pull request #24085 from oscargus/testaxislinestyle · matplotlib/matplotlib@5ead278 · GitHub
[go: up one dir, main page]

Skip to content

Commit 5ead278

Browse files
authored
Merge pull request #24085 from oscargus/testaxislinestyle
Set facecolor of FilledArrow axisline style and fix tight layout
2 parents e4c1d70 + 5d163cd commit 5ead278

File tree

7 files changed

+119
-7
lines changed

7 files changed

+119
-7
lines changed

lib/mpl_toolkits/axisartist/axis_artist.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1044,11 +1044,17 @@ def get_tightbbox(self, renderer=None):
10441044
self._axis_artist_helper.update_lim(self.axes)
10451045
self._update_ticks(renderer)
10461046
self._update_label(renderer)
1047+
1048+
self.line.set_path(self._axis_artist_helper.get_line(self.axes))
1049+
if self.get_axisline_style() is not None:
1050+
self.line.set_line_mutation_scale(self.major_ticklabels.get_size())
1051+
10471052
bb = [
10481053
*self.major_ticklabels.get_window_extents(renderer),
10491054
*self.minor_ticklabels.get_window_extents(renderer),
10501055
self.label.get_window_extent(renderer),
10511056
self.offsetText.get_window_extent(renderer),
1057+
self.line.get_window_extent(renderer),
10521058
]
10531059
bb = [b for b in bb if b and (b.width != 0 or b.height != 0)]
10541060
if bb:

lib/mpl_toolkits/axisartist/axisline_style.py

Lines changed: 54 additions & 6 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()
@@ -66,10 +70,25 @@ def draw(self, renderer):
6670
self._path_original = extended_path
6771
FancyArrowPatch.draw(self, renderer)
6872

73+
def get_window_extent(self, renderer=None):
74+
75+
path_in_disp = self._line_transform.transform_path(self._line_path)
76+
mutation_size = self.get_mutation_scale() # line_mutation_scale()
77+
extended_path = self._extend_path(path_in_disp,
78+
mutation_size=mutation_size)
79+
self._path_original = extended_path
80+
return FancyArrowPatch.get_window_extent(self, renderer)
81+
6982
class FilledArrow(SimpleArrow):
70-
"""The artist class that will be returned for SimpleArrow style."""
83+
"""The artist class that will be returned for FilledArrow style."""
7184
_ARROW_STYLE = "-|>"
7285

86+
def __init__(self, axis_artist, line_path, transform,
87+
line_mutation_scale, facecolor):
88+
super().__init__(axis_artist, line_path, transform,
89+
line_mutation_scale)
90+
self.set_facecolor(facecolor)
91+
7392

7493
class AxislineStyle(_Style):
7594
"""
@@ -140,6 +159,35 @@ def new_line(self, axis_artist, transform):
140159
_style_list["->"] = SimpleArrow
141160

142161
class FilledArrow(SimpleArrow):
162+
"""
163+
An arrow with a filled head.
164+
"""
165+
143166
ArrowAxisClass = _FancyAxislineStyle.FilledArrow
144167

168+
def __init__(self, size=1, facecolor=None):
169+
"""
170+
Parameters
171+
----------
172+
size : float
173+
Size of the arrow as a fraction of the ticklabel size.
174+
facecolor : color, default: :rc:`axes.edgecolor`
175+
Fill color.
176+
177+
.. versionadded:: 3.7
178+
"""
179+
180+
if facecolor is None:
181+
facecolor = mpl.rcParams['axes.edgecolor']
182+
self.size = size
183+
self._facecolor = facecolor
184+
super().__init__(size=size)
185+
186+
def new_line(self, axis_artist, transform):
187+
linepath = Path([(0, 0), (0, 1)])
188+
axisline = self.ArrowAxisClass(axis_artist, linepath, transform,
189+
line_mutation_scale=self.size,
190+
facecolor=self._facecolor)
191+
return axisline
192+
145193
_style_list["-|>"] = FilledArrow
Loading
Loading
Loading

lib/mpl_toolkits/axisartist/tests/test_axislines.py

Lines changed: 59 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,61 @@ 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)
120+
121+
122+
@image_comparison(['axisline_style_tight.png'], remove_text=True,
123+
style='mpl20')
124+
def test_axisline_style_tight():
125+
fig = plt.figure(figsize=(2, 2))
126+
ax = fig.add_subplot(axes_class=AxesZero)
127+
ax.axis["xzero"].set_axisline_style("-|>", size=5, facecolor='g')
128+
ax.axis["xzero"].set_visible(True)
129+
ax.axis["yzero"].set_axisline_style("->, size=8")
130+
ax.axis["yzero"].set_visible(True)
131+
132+
for direction in ("left", "right", "bottom", "top"):
133+
ax.axis[direction].set_visible(False)
134+
135+
fig.tight_layout()
136+
137+
138+
@image_comparison(['subplotzero_ylabel.png'], style='mpl20')
139+
def test_subplotzero_ylabel():
140+
fig = plt.figure()
141+
ax = fig.add_subplot(111, axes_class=SubplotZero)
142+
143+
ax.set(xlim=(-3, 7), ylim=(-3, 7), xlabel="x", ylabel="y")
144+
145+
zero_axis = ax.axis["xzero", "yzero"]
146+
zero_axis.set_visible(True) # they are hidden by default
147+
148+
ax.axis["left", "right", "bottom", "top"].set_visible(False)
149+
150+
zero_axis.set_axisline_style("->")

0 commit comments

Comments
 (0)
0