Hide tick label values but keep axis labels

PythonMatplotlib

Python Problem Overview


I have this image:

plt.plot(sim_1['t'],sim_1['V'],'k')
plt.ylabel('V')
plt.xlabel('t')
plt.show()

enter image description here

I want to hide the numbers; if I use:

plt.axis('off')

...I get this image:

enter image description here

It also hide the labels, V and t. How can I keep the labels while hiding the values?

Python Solutions


Solution 1 - Python

If you use the matplotlib object-oriented approach, this is a simple task using ax.set_xticklabels() and ax.set_yticklabels(). Here we can just set them to an empty list to remove any labels:

import matplotlib.pyplot as plt

# Create Figure and Axes instances
fig,ax = plt.subplots(1)

# Make your plot, set your axes labels
ax.plot(sim_1['t'],sim_1['V'],'k')
ax.set_ylabel('V')
ax.set_xlabel('t')

# Turn off tick labels
ax.set_yticklabels([])
ax.set_xticklabels([])

plt.show()

If you also want to remove the tick marks as well as the labels, you can use ax.set_xticks() and ax.set_yticks() and set those to an empty list as well:

ax.set_xticks([])
ax.set_yticks([])

Solution 2 - Python

Without a subplots, you can universally remove the ticks like this:

plt.xticks([])
plt.yticks([])

Solution 3 - Python

This works great. Just paste this before plt.show():

plt.gca().axes.get_yaxis().set_visible(False)

Boom.

Solution 4 - Python

to remove tickmarks entirely use:

ax.set_yticks([])
ax.set_xticks([])

otherwise ax.set_yticklabels([]) and ax.set_xticklabels([]) will keep tickmarks.

Solution 5 - Python

plt.gca().axes.yaxis.set_ticklabels([])

enter image description here

Solution 6 - Python

Not sure this is the best way, but you can certainly replace the tick labels like this:

import matplotlib.pyplot as plt
x = range(10)
y = range(10)
plt.plot(x,y)
plt.xticks(x," ")
plt.show()

In Python 3.4 this generates a simple line plot with no tick labels on the x-axis. A simple example is here: <http://matplotlib.org/examples/ticks_and_spines/ticklabels_demo_rotation.html>

This related question also has some better suggestions: <https://stackoverflow.com/questions/2176424/hiding-axis-text-in-matplotlib-plots?rq=1>

I'm new to python. Your mileage may vary in earlier versions. Maybe others can help?

Solution 7 - Python

This also works.

fig, ax = plt.subplots()
ax.yaxis.set_major_locator(plt.NullLocator())
ax.xaxis.set_major_formatter(plt.NullFormatter())

see this book for nice tips for customizing ticks https://jakevdp.github.io/PythonDataScienceHandbook/04.10-customizing-ticks.html

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionLuis Ramon Ramirez RodriguezView Question on Stackoverflow
Solution 1 - PythontmdavisonView Answer on Stackoverflow
Solution 2 - PythonNoel EvansView Answer on Stackoverflow
Solution 3 - PythonMRTView Answer on Stackoverflow
Solution 4 - PythonPoe DatorView Answer on Stackoverflow
Solution 5 - PythonNic ScozzaroView Answer on Stackoverflow
Solution 6 - PythonJoe GavinView Answer on Stackoverflow
Solution 7 - PythonMauricio ArboledaView Answer on Stackoverflow