Поскольку они нарисованы внутри области графика, отметки оси скрыты данными на многих графиках matplotlib. Лучшим подходом является рисование тиков, простирающихся от осей наружу , как это используется по умолчанию в ggplot
, системе построения графика R.
Теоретически это можно сделать путем перерисовки линий тиков с помощью линий TICKDOWN
и TICKLEFT
для тиков по осям X и Y соответственно:
import matplotlib.pyplot as plt
import matplotlib.ticker as mplticker
import matplotlib.lines as mpllines
# Create everything, plot some data stored in `x` and `y`
fig = plt.figure()
ax = fig.gca()
plt.plot(x, y)
# Set position and labels of major and minor ticks on the y-axis
# Ignore the details: the point is that there are both major and minor ticks
ax.yaxis.set_major_locator(mplticker.MultipleLocator(1.0))
ax.yaxis.set_minor_locator(mplticker.MultipleLocator(0.5))
ax.xaxis.set_major_locator(mplticker.MultipleLocator(1.0))
ax.xaxis.set_minor_locator(mplticker.MultipleLocator(0.5))
# Try to set the tick markers to extend outward from the axes, R-style
for line in ax.get_xticklines():
line.set_marker(mpllines.TICKDOWN)
for line in ax.get_yticklines():
line.set_marker(mpllines.TICKLEFT)
# In real life, we would now move the tick labels farther from the axes so our
# outward-facing ticks don't cover them up
plt.show()
Но на практике это только половина решения, потому что методы get_xticklines
и get_yticklines
возвращают только основные тиковые линии. Незначительные тики остаются направленными внутрь.
Какой обходной путь для мелких тиков?