Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit4bee35d

Browse files
committed
Allow sharing locators across axises.
Locally changing the assigned axis seems to be the most practicalsolution, if not the most elegant.
1 parentc010a36 commit4bee35d

File tree

4 files changed

+44
-12
lines changed

4 files changed

+44
-12
lines changed

‎lib/matplotlib/axis.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1527,14 +1527,14 @@ def get_ticklines(self, minor=False):
15271527

15281528
defget_majorticklocs(self):
15291529
"""Return this Axis' major tick locations in data coordinates."""
1530-
returnself.major.locator()
1530+
returnself.major.locator._call_with_axis(self)
15311531

15321532
defget_minorticklocs(self):
15331533
"""Return this Axis' minor tick locations in data coordinates."""
15341534
# Remove minor ticks duplicating major ticks.
1535-
minor_locs=np.asarray(self.minor.locator())
1535+
minor_locs=np.asarray(self.minor.locator._call_with_axis(self))
15361536
ifself.remove_overlapping_locs:
1537-
major_locs=self.major.locator()
1537+
major_locs=self.major.locator._call_with_axis(self)
15381538
transform=self._scale.get_transform()
15391539
tr_minor_locs=transform.transform(minor_locs)
15401540
tr_major_locs=transform.transform(major_locs)

‎lib/matplotlib/tests/test_ticker.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1873,3 +1873,16 @@ def test_minorticks_on_multi_fig():
18731873

18741874
assertax.get_xgridlines()
18751875
assertisinstance(ax.xaxis.get_minor_locator(),mpl.ticker.AutoMinorLocator)
1876+
1877+
1878+
deftest_locator_reuse():
1879+
fig=plt.figure()
1880+
ax=fig.add_subplot(xlim=(.6,.8))
1881+
loc=mticker.AutoLocator()
1882+
ax.xaxis.set_major_locator(loc)
1883+
ax.yaxis.set_major_locator(loc)
1884+
fig.draw_without_rendering()
1885+
xticklabels= [l.get_text()forlinax.get_xticklabels()]
1886+
yticklabels= [l.get_text()forlinax.get_yticklabels()]
1887+
assertxticklabels== ["0.60","0.65","0.70","0.75","0.80"]
1888+
assertyticklabels== ["0.0","0.2","0.4","0.6","0.8","1.0"]

‎lib/matplotlib/ticker.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@
130130
and examples of using date locators and formatters.
131131
"""
132132

133+
importcontextlib
133134
importitertools
134135
importlogging
135136
importlocale
@@ -1629,11 +1630,25 @@ def set_params(self, **kwargs):
16291630
str(type(self)))
16301631

16311632
def__call__(self):
1632-
"""Return the locations of the ticks."""
1633+
"""
1634+
Return the locations of the ticks.
1635+
1636+
Note that the returned ticks depend on the axis assigned to the
1637+
locator. If a given locator is used across multiple axises, make sure
1638+
that
1639+
"""
16331640
# note: some locators return data limits, other return view limits,
16341641
# hence there is no *one* interface to call self.tick_values.
16351642
raiseNotImplementedError('Derived must override')
16361643

1644+
def_call_with_axis(self,axis):
1645+
current=axis
1646+
try:
1647+
self.set_axis(axis)
1648+
returnself()
1649+
finally:
1650+
self.set_axis(current)
1651+
16371652
defraise_if_exceeds(self,locs):
16381653
"""
16391654
Log at WARNING level if *locs* is longer than `Locator.MAXTICKS`.

‎lib/mpl_toolkits/axisartist/axislines.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -185,11 +185,11 @@ def get_tick_iterators(self, axes):
185185
angle_normal,angle_tangent= {0: (90,0),1: (0,90)}[self.nth_coord]
186186

187187
major=self.axis.major
188-
major_locs=major.locator()
188+
major_locs=major.locator._call_with_axis(self.axis)
189189
major_labels=major.formatter.format_ticks(major_locs)
190190

191191
minor=self.axis.minor
192-
minor_locs=minor.locator()
192+
minor_locs=minor.locator._call_with_axis(self.axis)
193193
minor_labels=minor.formatter.format_ticks(minor_locs)
194194

195195
tick_to_axes=self.get_tick_transform(axes)-axes.transAxes
@@ -246,11 +246,11 @@ def get_tick_iterators(self, axes):
246246
angle_normal,angle_tangent= {0: (90,0),1: (0,90)}[self.nth_coord]
247247

248248
major=self.axis.major
249-
major_locs=major.locator()
249+
major_locs=major.locator._call_with_axis(self.axis)
250250
major_labels=major.formatter.format_ticks(major_locs)
251251

252252
minor=self.axis.minor
253-
minor_locs=minor.locator()
253+
minor_locs=minor.locator._call_with_axis(self.axis)
254254
minor_labels=minor.formatter.format_ticks(minor_locs)
255255

256256
data_to_axes=axes.transData-axes.transAxes
@@ -351,18 +351,22 @@ def get_gridlines(self, which="major", axis="both"):
351351
locs= []
352352
y1,y2=self.axes.get_ylim()
353353
ifwhichin ("both","major"):
354-
locs.extend(self.axes.xaxis.major.locator())
354+
locs.extend(
355+
self.axes.xaxis.major.locator._call_with_axis(self.axes.xaxis))
355356
ifwhichin ("both","minor"):
356-
locs.extend(self.axes.xaxis.minor.locator())
357+
locs.extend(
358+
self.axes.xaxis.minor.locator._call_with_axis(self.axes.xaxis))
357359
gridlines.extend([[x,x], [y1,y2]]forxinlocs)
358360

359361
ifaxisin ("both","y"):
360362
x1,x2=self.axes.get_xlim()
361363
locs= []
362364
ifself.axes.yaxis._major_tick_kw["gridOn"]:
363-
locs.extend(self.axes.yaxis.major.locator())
365+
locs.extend(
366+
self.axes.yaxis.major.locator._call_with_axis(self.axes.yaxis))
364367
ifself.axes.yaxis._minor_tick_kw["gridOn"]:
365-
locs.extend(self.axes.yaxis.minor.locator())
368+
locs.extend(
369+
self.axes.yaxis.minor.locator._call_with_axis(self.axis.yaxis))
366370
gridlines.extend([[x1,x2], [y,y]]foryinlocs)
367371

368372
returngridlines

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp