Felix
Felix

Reputation: 2154

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'])?

Upvotes: 3

Views: 1335

Answers (1)

tmdavison
tmdavison

Reputation: 69116

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()

Upvotes: 7

Related Questions