Ask Your Question
2

How can axis labels be retained while hiding the values of the tick labels?

asked 2022-03-14 11:00:00 +0000

pufferfish gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
3

answered 2021-07-17 15:00:00 +0000

bukephalos gravatar image

One way to do this is to set the tick labels to an empty string using the set_ticklabels() method and then set the axis label using the set_label() method.

Here's an example:

import matplotlib.pyplot as plt
import numpy as np

# Generate some data
x = np.arange(0, 10, 0.1)
y = np.sin(x)

# Create the plot
fig, ax = plt.subplots()
ax.plot(x, y)

# Hide tick labels but retain the axis label
ax.set_xticklabels(['']*len(ax.get_xticks()))  # hide x-axis tick labels
ax.set_yticklabels(['']*len(ax.get_yticks()))  # hide y-axis tick labels
ax.set_xlabel('x-axis label')
ax.set_ylabel('y-axis label')

# Show the plot
plt.show()

This code will show the plot with axis labels but without tick labels. The set_xticklabels() and set_yticklabels() methods set the tick labels to an empty string using a list with the same length as the number of ticks.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2022-03-14 11:00:00 +0000

Seen: 10 times

Last updated: Jul 17 '21