2

How do I turn off the major x axis grid from this axes? matplotlib 3d plot with major x axis gridline showing

This code below works on 2d plots to hide the major x gridlines. On 3d plots it doesn't seem to do anything.

I prefer to use general axes methods like grid() or tick_params() that use keyword arguments since they are more flexible than calling out specific axis methods (i.e. xaxis, yaxis, etc.)

I've searched some answers and there's not much out there regarding 3d plots and this, the AI responses match what I already have and the human answer I found references [this] but it's not how I want to do it unless there are no other reasonable options.2

Update: The creativity of @Márton Horváth's answer inspired me to try the following things: test_ax.tick_params(axis='x', which='major', grid_alpha=0.0) and test_ax.tick_params(axis='x', which='major', grid_linewidth=0.0) but they did not work. I'm hesitant on the answer proposed that starts with the hidden methods that start with _ since this may turn into production code.

What should I try next?

from matplotlib.figure import Figure
import numpy as np      
test_fig = Figure()
test_ax = test_fig.add_subplot(111, projection='3d')
# fake data
_x = np.arange(4)
_y = np.arange(5)
_xx, _yy = np.meshgrid(_x, _y)
x, y = _xx.ravel(), _yy.ravel()
top = x + y
bottom = np.zeros_like(top)
width = depth = 1
test_ax.bar3d(x, y, bottom, width, depth, top, shade=True)
not_vis_kwargs = {'which': 'major', 'axis': 'x', 'visible': False}
test_ax.grid(**not_vis_kwargs)
test_fig.savefig('test_figure.jpg')

2 Answers 2

1

You can hide any of the respective gridlines using the following lines:

test_ax.xaxis._axinfo['grid'].update({'linewidth': 0}) 
test_ax.yaxis._axinfo['grid'].update({'linewidth': 0})
test_ax.zaxis._axinfo['grid'].update({'linewidth': 0})
3
  • Creative suggestion, it inspired me to try another method - please see the update in my question. Commented Mar 2 at 23:39
  • This is the best answer and I anticipate accepting it, do you know how to apply this to minor axis as well? I tried test_ax.xaxis._axinfo['grid'].update({'which': 'minor', 'linewidth': 5}) and test_ax.xaxis._axinfo['grid'].update({'minor': True, 'linewidth': 5}), it ignored the extra keyword I added and made the linewidth of the xaxis 5. Commented Mar 4 at 9:23
  • Had the answer in my code already, for future readers: from matplotlib.ticker import AutoMinorLocator then use test_ax.xaxis.set_minor_locator(AutoMinorLocator()), works on the zaxis too. Commented Mar 4 at 9:33
0

only this

test_ax.axis("off")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.