The Axes
class doesn't have an override for the set()
method, so matplotlib.axes.Axes.set()
is the same as matplotlib.artist.Artist.set()
which calls matplotlib.artist.Artist.update()
with the the specified keyword argument list (props
). update()
looks like this:
def update(self, props):
ret = []
with cbook._setattr_cm(self, eventson=False):
for k, v in props.items():
# ...
if k == "axes":
ret.append(setattr(self, k, v))
else:
func = getattr(self, f"set_{k}", None)
if not callable(func):
raise AttributeError(f"{type(self).__name__!r} object "
f"has no property {k!r}")
ret.append(func(v))
# ....
return ret
This goes through the props
dict and checks if a setter method of self
(in the case of matplotlib.axes.Axes.set()
the Axes
instance) exists for each key, if it does then it calls it with the associated value, if not it raises an AttributeError
. This means that you can't use the set()
method to change attributes for which there is no defined setter method (the one exception is that you can pass the axes
argument to reset the axes).
Since matplotlib.axes.Axes
does not have a setter for the label rotations and alignments, you can't use ax.set()
to mutate these. What you can do is call .set()
on the text instance instead, i.e.
import matplotlib.pyplot as plt
fig, ax = plt.subplots(1)
ylab = ax.set_ylabel("y-label")
ylab.set(rotation="horizontal", ha="right")
I think this is the closest to your desired usage that is possible with .set()
.