Reputation: 1657
How do I turn off the major x axis grid from this axes?
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')
Upvotes: 2
Views: 35
Reputation: 446
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})
Upvotes: 1