0

I want to apply custom ticks to my plot with matplotlib to offset the labels of a 3D bar graph by half the column width, similar to this post. Here is my code:

from matplotlib import pyplot as plt
from numpy import arange, meshgrid, abs, zeros_like
import mpl_toolkits.mplot3d

def barplot3d(x, y, z, centerLabels = True):
    x0 = x[:, 0]
    y0 = y[0, :]
    x = x.flatten()
    y = y.flatten()
    z = z.flatten()
    z0 = zeros_like(z)
    fig = plt.figure()
    ax = fig.add_subplot(111, projection = "3d")
    ax.set_xticks(x0 + 0.5)
    ax.set_xticklabels(x0)
    ax.set_yticks(y0 + 0.5)
    ax.set_yticklabels(y0)
    ax.bar3d(x, y, z0, 1, 1, z)
    plt.show()

x = arange(-10, 10)
y = arange(10)
mx, my = meshgrid(x, y, indexing = "ij")
z = abs(mx * my - (mx + 2 * my))

barplot3d(mx, my, z)

It works fine. However, this will always plot all ticks, one for each row/column in z which results in crowded x and y axes. See the following image of the plot created usign the above code:

How can I adjust this so that it is as close as possible to the result I would obtain from using the automatically created labels? That is, how can I decide which labels to take and which to discard? Can I still somehow use the automated "system" that decides this while specifying the label positions?

HerpDerpington
  • 3,751
  • 4
  • 27
  • 43
  • 1
    Apparently you should modify `ax.set_xticks(x0+ 0.5)` to something similar to `ax.set_xticks(x0[::x_step] + 0.5)`? Or you can use [`MultipleLocator`](https://matplotlib.org/stable/api/ticker_api.html#matplotlib.ticker.MultipleLocator). – Quang Hoang Mar 05 '21 at 20:52
  • @QuangHoang In principle, yes. But my question was more about how to properly choose `x_step` or rather how to find the `x_step` `matplotlib` would have used had I not set the ticks manually. – HerpDerpington Mar 05 '21 at 21:43

0 Answers0