Skip to content

Make Spines accessable by the attributes. #17099

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

Closed
wants to merge 4 commits 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
10 changes: 6 additions & 4 deletions examples/specialty_plots/skewt.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,12 @@ def _init_axis(self):
self.spines['right'].register_axis(self.yaxis)

def _gen_axes_spines(self):
spines = {'top': SkewSpine.linear_spine(self, 'top'),
'bottom': mspines.Spine.linear_spine(self, 'bottom'),
'left': mspines.Spine.linear_spine(self, 'left'),
'right': mspines.Spine.linear_spine(self, 'right')}
spines = mspines._Spines({
'top': SkewSpine.linear_spine(self, 'top'),
'bottom': mspines.Spine.linear_spine(self, 'bottom'),
'left': mspines.Spine.linear_spine(self, 'left'),
'right': mspines.Spine.linear_spine(self, 'right')
})
return spines

def _set_lim_and_transforms(self):
Expand Down
7 changes: 4 additions & 3 deletions lib/matplotlib/axes/_base.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from collections import OrderedDict
from contextlib import ExitStack
import inspect
import itertools
Expand Down Expand Up @@ -1002,8 +1001,10 @@ def _gen_axes_spines(self, locations=None, offset=0.0, units='inches'):
-----
Intended to be overridden by new projection types.
"""
return OrderedDict((side, mspines.Spine.linear_spine(self, side))
for side in ['left', 'right', 'bottom', 'top'])
return mspines._Spines(
(side, mspines.Spine.linear_spine(self, side))
for side in ['left', 'right', 'bottom', 'top']
)

def sharex(self, other):
"""
Expand Down
3 changes: 1 addition & 2 deletions lib/matplotlib/projections/polar.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from collections import OrderedDict
import types

import numpy as np
Expand Down Expand Up @@ -996,7 +995,7 @@ def _gen_axes_patch(self):
return mpatches.Wedge((0.5, 0.5), 0.5, 0.0, 360.0)

def _gen_axes_spines(self):
spines = OrderedDict([
spines = mspines._Spines([
('polar', mspines.Spine.arc_spine(self, 'top',
(0.5, 0.5), 0.5, 0.0, 360.0)),
('start', mspines.Spine.linear_spine(self, 'left')),
Expand Down
18 changes: 18 additions & 0 deletions lib/matplotlib/spines.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import numpy as np
from collections import OrderedDict

import matplotlib
from matplotlib import cbook, docstring, rcParams
Expand Down Expand Up @@ -534,3 +535,20 @@ def set_color(self, c):
"""
self.set_edgecolor(c)
self.stale = True


class _Spines(OrderedDict):
"""
A collection of multiple axis spines.

_Spines can be accessed by both key and attribute.

"""
def __dir__(self):
return super().__dir__() + list(self.keys())

def __getattr__(self, key):
return self[key]

def __setattr__(self, key, value):
self[key] = value
10 changes: 6 additions & 4 deletions lib/matplotlib/tests/test_skew.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,12 @@ def _init_axis(self):
self.spines['right'].register_axis(self.yaxis)

def _gen_axes_spines(self):
spines = {'top': SkewSpine.linear_spine(self, 'top'),
'bottom': mspines.Spine.linear_spine(self, 'bottom'),
'left': mspines.Spine.linear_spine(self, 'left'),
'right': mspines.Spine.linear_spine(self, 'right')}
spines = mspines._Spines({
'top': SkewSpine.linear_spine(self, 'top'),
'bottom': mspines.Spine.linear_spine(self, 'bottom'),
'left': mspines.Spine.linear_spine(self, 'left'),
'right': mspines.Spine.linear_spine(self, 'right')
})
return spines

def _set_lim_and_transforms(self):
Expand Down