MEP13: Use properties for Artists#
Status#
Discussion
Branches and Pull requests#
None
Abstract#
Wrap all of the matplotlib getter and setter methods with pythonproperties, allowingthem to be read and written like class attributes.
Detailed description#
Currently matplotlib uses getter and setter functions (usuallyprefixed with get_ and set_, respectively) for reading and writingdata related to classes. However, since 2.6 python supportsproperties, which allow such setter and getter functions to beaccessed as though they were attributes. This proposal wouldimplement all existing setter and getter methods as properties.
Implementation#
All existing getter and setter methods will need to have twoaliases, one with the get_ or set_ prefix and one without.Getter methods that currently lack prefixes should be recording ina text file.
Classes should be reorganized so setter and getter methods aresequential in the code, with getter methods first.
Getter and setter methods that provide additional optional arguments shouldhave those arguments accessible in another manner, either as additionalgetter or setter methods or attributes of other classes. If those classesare not accessible, getters for them should be added.
Property decorators will be added to the setter and getter methodswithout the prefix. Those with the prefix will be marked asdeprecated.
Docstrings will need to be rewritten so the getter with the prefixhas the current docstring and the getter without the prefix has ageneric docstring appropriate for an attribute.
Automatic alias generation will need to be modified so it will alsocreate aliases for the properties.
All instances of getter and setter method calls will need to bechanged to attribute access.
All setter and getter aliases with prefixes will be removed
The following steps can be done simultaneously: 1, 2, and 3; 4 and 5;6 and 7.
Only the following steps must be done in the same release: 4, 5,and 6. All other changes can be done in separate releases. 8 shouldbe done several macro releases after everything else.
Backward compatibility#
All existing getter methods that do not have a prefix (such as get_)will need to be changed from function calls to attribute access. Inmost cases this will only require removing the parenthesis.
setter and getter methods that have additional optional arguments willneed to have those arguments implemented in another way, either as aseparate property in the same class or as attributes or properties ofanother class.
Cases where the setter returns a value will need to be changed tousing the setter followed by the getter.
Cases where there are set_ATTR_on() and set_ATTR_off() methods will bechanged to ATTR_on properties.
Examples#
axes.Axes.set_axis_off/set_axis_on#
Current implementation:
axes.Axes.set_axis_off()axes.Axes.set_axis_on()
New implementation:
True=axes.Axes.axis_onFalse=axes.Axes.axis_onaxes.Axes.axis_on=Trueaxes.Axes.axis_on=False
axes.Axes.get_xlim/set_xlim and get_autoscalex_on/set_autoscalex_on#
Current implementation:
[left,right]=axes.Axes.get_xlim()auto=axes.Axes.get_autoscalex_on()[left,right]=axes.Axes.set_xlim(left=left,right=right,emit=emit,auto=auto)[left,right]=axes.Axes.set_xlim(left=left,right=None,emit=emit,auto=auto)[left,right]=axes.Axes.set_xlim(left=None,right=right,emit=emit,auto=auto)[left,right]=axes.Axes.set_xlim(left=left,emit=emit,auto=auto)[left,right]=axes.Axes.set_xlim(right=right,emit=emit,auto=auto)axes.Axes.set_autoscalex_on(auto)
New implementation:
[left,right]=axes.Axes.axes_xlimauto=axes.Axes.autoscalex_onaxes.Axes.axes_xlim=[left,right]axes.Axes.axes_xlim=[left,None]axes.Axes.axes_xlim=[None,right]axes.Axes.axes_xlim[0]=leftaxes.Axes.axes_xlim[1]=rightaxes.Axes.autoscalex_on=autoaxes.Axes.emit_xlim=emit
axes.Axes.get_title/set_title#
Current implementation:
string=axes.Axes.get_title()axes.Axes.set_title(string,fontdict=fontdict,**kwargs)
New implementation:
string=axes.Axes.titlestring=axes.Axes.title_text.texttext.Text=axes.Axes.title_texttext.Text.<attribute>=attributetext.Text.fontdict=fontdictaxes.Axes.title=stringaxes.Axes.title=text.Textaxes.Axes.title_text=stringaxes.Axes.title_text=text.Text
axes.Axes.get_xticklabels/set_xticklabels#
Current implementation:
[text.Text]=axes.Axes.get_xticklabels()[text.Text]=axes.Axes.get_xticklabels(minor=False)[text.Text]=axes.Axes.get_xticklabels(minor=True)[text.Text]=axes.Axes.([string],fontdict=None,**kwargs)[text.Text]=axes.Axes.([string],fontdict=None,minor=False,**kwargs)[text.Text]=axes.Axes.([string],fontdict=None,minor=True,**kwargs)
New implementation:
[text.Text]=axes.Axes.xticklabels[text.Text]=axes.Axes.xminorticklabelsaxes.Axes.xticklabels=[string]axes.Axes.xminorticklabels=[string]axes.Axes.xticklabels=[text.Text]axes.Axes.xminorticklabels=[text.Text]
Alternatives#
Instead of using decorators, it is also possible to use the propertyfunction. This would change the procedure so that all getter methodsthat lack a prefix will need to be renamed or removed. This makeshandling docstrings more difficult and harder to read.
It is not necessary to deprecate the setter and getter methods, butleaving them in will complicate the code.
This could also serve as an opportunity to rewrite or even removeautomatic alias generation.
Another alternate proposal:
Convertset_xlim
,set_xlabel
,set_title
, etc. toxlim
,xlabel
,title
,... to make the transition fromplt
functions toaxes
methods significantly simpler. These would stillbe methods, not properties, but it's still a great usabilityenhancement while retaining the interface.