1

The color of the axis (x, y, z) in a 3d plot using matplotlib is black by default. How do you change the color of the axis? Or better yet, how do you make them invisible?

%matplotlib inline
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.xaxis.set_visible(False)  # doesn't do anything

And there doesn't seem to be a ax.xaxis.set_color function. Any thoughts on how to make the axis invisible or change the color?

Mark Bakker
  • 799
  • 1
  • 6
  • 15

1 Answers1

1

You can combine your method with the approach provided here. I am showing an example that affects all three axes. In Jupyter Notebook, using tab completion after ax.w_xaxis.line., you can discover other possible options

ax.w_xaxis.line.set_visible(False)
ax.w_yaxis.line.set_color("red")
ax.w_zaxis.line.set_color("blue")

To change the tick colors, you can use

ax.xaxis._axinfo['tick']['color']='r'
ax.yaxis._axinfo['tick']['color']='g'
ax.zaxis._axinfo['tick']['color']='b' 

To hide the ticks

for line in ax.xaxis.get_ticklines():
    line.set_visible(False)

enter image description here

Sheldore
  • 37,862
  • 7
  • 57
  • 71
  • Works great. Is there a similar functionality for the color/visibility of the ticks? – Mark Bakker Jun 08 '20 at 16:08
  • Great again! Can I go for three in a row? Howbout making the ticks invisible? I tried `t = ax.w_xaxis.get_ticklines()`, then loop `for a in t: a.set_visible(False)`, no error message, but also no invisible ticks. – Mark Bakker Jun 08 '20 at 16:40
  • I got it to work doing `ax.xaxis._axinfo['tick']['inward_factor'] = 0` and `ax.xaxis._axinfo['tick']['outward_factor'] = 0`. Kinda ugly really. But it works. Better suggestions are appreciated! – Mark Bakker Jun 08 '20 at 16:44
  • I also don't understand why the simple `ax.w_xaxis.set_visible(False)` doesn't do anything. But the suggested fix work great. – Mark Bakker Jun 08 '20 at 16:45
  • Sorry, @Sheldore, that last edit with the for loop to make the ticks invisible doesn't work (in mpl 3.1.1). Besides, it drives me bunkers to have `ax.xaxis` and `ax.w_xaxis`. What's the diff? – Mark Bakker Jun 08 '20 at 17:16