0

I am trying to plot cartesian axes like here:

enter image description here

Because of overlapping yticks I want to reduce their number. I tried this approach.

for n, label in enumerate(ax.axis[direction].get_ticklabels()):
    if n % 5 != 0:
        label.set_visible(False)

But it gave an error:

'AxisArtist' object has no attribute 'get_ticklabels'

How can I overcome it?

Kenenbek Arzymatov
  • 8,439
  • 19
  • 58
  • 109
  • 1
    In general, check the examples from the current version, rather than old ones. E.g. [the spine placement](https://matplotlib.org/gallery/ticks_and_spines/spine_placement_demo.html), or [axisline style](https://matplotlib.org/gallery/axisartist/demo_axisline_style.html). For ticking, consider using locators as in [tick-locators](https://matplotlib.org/gallery/ticks_and_spines/tick-locators.html), e.g. a `MultipleLocator` or a `MaxNLocator`. – ImportanceOfBeingErnest Mar 28 '19 at 23:36

1 Answers1

1

mpl_toolkits.axes_grid.axislines is deprecated; import from mpl_toolkits.axisartist instead.

Adding ax.set_yticks(np.linspace(-1, 1, 5)) will do.

If you want to dynamically space out ticks depending on axis extents, you can use ax.set_yticks(np.arange(*ax.axis()[2:], 0.5)) (to set distance between ticks) or ax.set_yticks(np.linspace(*ax.axis()[2:], 5)) (to set number of ticks).

Example result:

Example result

gmds
  • 19,325
  • 4
  • 32
  • 58