Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting all matplotlib axis labels at once

Since I usually try to label my axes in matplotlib plots, I find that I regularly label the x/y/z axes individually, using something like this:

fig = plt.figure()
ax = fig.add_subplot(1, 1, 1, projection='3d')
# <plot plot plot>
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z')     

Is there a way to reduce the individual axis-label setting to one command, ideally something like ax.set_labels(['x', 'y', 'z'])?

like image 310
Felix Avatar asked Jul 14 '26 06:07

Felix


1 Answers

You can get close using ax.update. For example:

fig = plt.figure()
ax = fig.add_subplot(1, 1, 1, projection='3d')

ax.update({'xlabel':'x', 'ylabel':'y', 'zlabel':'z'})

From the docs for ax.update:

update(props)

Update the properties of this Artist from the dictionary prop.

So, you can update more than just axes labels using ax.update, so this could help reduce you code in other places too. Just pass whichever property to update in the dictionary. A list of available properties can be found using ax.properties()

like image 163
tmdavison Avatar answered Jul 20 '26 12:07

tmdavison



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!