I want to make the ticks on the right and upper axis invisible and am not sure what the third line should be:
import matplotlib.pyplot as plt
plt.plot(X,Y)
#plt.upper_right_axis_ticks_off()
By using the method xticks() and yticks() you can disable the ticks and tick labels from both the x-axis and y-axis. In the above example, we use plt. xticks([]) method to invisible both the ticks and labels on the x-axis and set the ticks empty.
Direct link to this answerTickLength = [0 0]; This will allow you to keep the labels but remove the tick marks on only the x-axis.
A tick is a short line on an axis. For category axes, ticks separate each category. For value axes, ticks mark the major divisions and show the exact point on an axis that the axis label defines. Ticks are always the same color and line style as the axis.
As pointed by @imsc, You can tweak the visibility of the tick marks by setting the position of the ticks to the bottom and left (if you don't want them on top and right) using the ax.xaxis.set_ticks_position
and ax.yaxis.set_ticks_position
methods.
If you also want to set the axis itself invisible, check out the matplotlib.spines class. You just have to set the color of the spines to none
using the ax.spines[spine].set_color
method.
If you have a lot of plots to display, and don't want to turn manually the axis/ticks off each time, below is a function that will do the job for you.
Basically, you define for each axis the color you want (in this case none
will render it invisible), and the function will also "turn off" the ticks marks for all the axis invisible. I have also added options to define the linewidth
of the axis lines, as well as the fontsize
of the ticklabels and the pad
between the ticklabels and the ticks.
def customaxis(ax, c_left='k', c_bottom='k', c_right='none', c_top='none',
lw=3, size=12, pad=8):
for c_spine, spine in zip([c_left, c_bottom, c_right, c_top],
['left', 'bottom', 'right', 'top']):
if c_spine != 'none':
ax.spines[spine].set_color(c_spine)
ax.spines[spine].set_linewidth(lw)
else:
ax.spines[spine].set_color('none')
if (c_bottom == 'none') & (c_top == 'none'): # no bottom and no top
ax.xaxis.set_ticks_position('none')
elif (c_bottom != 'none') & (c_top != 'none'): # bottom and top
ax.tick_params(axis='x', direction='out', width=lw, length=7,
color=c_bottom, labelsize=size, pad=pad)
elif (c_bottom != 'none') & (c_top == 'none'): # bottom but not top
ax.xaxis.set_ticks_position('bottom')
ax.tick_params(axis='x', direction='out', width=lw, length=7,
color=c_bottom, labelsize=size, pad=pad)
elif (c_bottom == 'none') & (c_top != 'none'): # no bottom but top
ax.xaxis.set_ticks_position('top')
ax.tick_params(axis='x', direction='out', width=lw, length=7,
color=c_top, labelsize=size, pad=pad)
if (c_left == 'none') & (c_right == 'none'): # no left and no right
ax.yaxis.set_ticks_position('none')
elif (c_left != 'none') & (c_right != 'none'): # left and right
ax.tick_params(axis='y', direction='out', width=lw, length=7,
color=c_left, labelsize=size, pad=pad)
elif (c_left != 'none') & (c_right == 'none'): # left but not right
ax.yaxis.set_ticks_position('left')
ax.tick_params(axis='y', direction='out', width=lw, length=7,
color=c_left, labelsize=size, pad=pad)
elif (c_left == 'none') & (c_right != 'none'): # no left but right
ax.yaxis.set_ticks_position('right')
ax.tick_params(axis='y', direction='out', width=lw, length=7,
color=c_right, labelsize=size, pad=pad)
Below is an example of how to use it:
import numpy as np
import matplotlib.pyplot as plt
fig, ([ax1, ax2], [ax3, ax4]) = plt.subplots(nrows=2, ncols=2)
for ax in [ax1, ax2, ax3, ax4]:
ax.plot(np.random.randn(10), lw=2)
customaxis(ax1) #default: no right and top axis
customaxis(ax2, c_left='none', c_bottom='none', c_right='k', c_top='k')
customaxis(ax3, c_left='none', c_bottom='k', c_right='k', c_top='none')
customaxis(ax4, c_left='k', c_bottom='none', c_right='none', c_top='k')
plt.show()
Have a look at http://matplotlib.sourceforge.net/examples/pylab_examples/spine_placement_demo.html
import pylab as p
t = p.arange(0.0, 2.0, 0.01)
ax=p.subplot(111)
s = p.sin(2*p.pi*t)
ax.plot(t, s, color='r',linewidth=1.0)
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')
p.show()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With