Как убрать подписи к осям matplotlib
The Matplotlib library by default shows the axis ticks and tick labels. Sometimes it is necessary to hide these axis ticks and tick labels. This article discusses some methods by which this can be done.
- Ticks: The axes’ points are marked with ticks, which are also known as little geometrical scale lines.
- Tick labels: They are the name given to the ticks. Alternately, we may say that tick labels are text-filled ticks known as Text Ticks.
- Axes: The X-axis and Y-axis are referred to as axis labels.
Functions used:
- xticks(ticks=None, labels=None, **kwargs): used to get and set the current tick locations and labels of the x-axis.
- yticks(ticks=None, labels=None, **kwargs): used to get and set the current tick locations and labels of the y-axis.
- set_visible(boolean)- sets visibility
Method 1: Select all columns except one by setting the tick labels to be empty
The functions xticks() and yticks() are used to denote positions using which a data point is supposed to be displayed. They take a list as an argument. Thus, axis text ticks or tick labels can be disabled by setting the xticks and yticks to an empty list as shown below:
Как спрятать текстовые метки оси и / или галочки в Matplotlib
График в Matplotlib по умолчанию показывает ticks и ticklabels двух axes , как показано на примере рисунка.
Please enable JavaScript
Он имеет различные методы для сокрытия текста осей, такие как xaxis.set_visible(False) , xaxis.set_ticks([]) и xaxis.set_ticklabels([]) . Если цвет тиков установлен как белый, то это также может сделать текст оси невидимым, только если цвет переднего плана фигуры Matplotlib белый.
xaxis.set_visible(False) / yaxis.set_visible(False) Скрыть оси Включая ось label
Как следует из названия, она делает невидимой всю ось, включая тики осей, тиковые метки осей и ось label .
xaxis.set_ticks([]) / yaxis.set_ticks([]) чтобы скрыть ось в Matplotlib
x/yaxis.set_ticks([]) устанавливает тики пустыми и делает тики и их метки невидимыми. Но на метку оси это не влияет.
xaxis.set_ticklabels([]) / yaxis.set_ticklabels([]) чтобы скрыть осевую метку /текст в Matplotlib
x/yaxis.set_ticklabels([]) устанавливает, что тиковые метки пусты, так что текст оси (тиковые метки) становится невидимым, но оставляет тики видимыми.
xticks(color=’w’) / yticks(color=’w’) чтобы скрыть метку / текст оси в Matplotlib
Этот хитрый метод не делает тиковые метки или тики невидимыми, а устанавливает цвет тиков белым так, чтобы текст оси был действительно невидимым, если фон графика белый (также цвет по умолчанию).
Hiding axis text in matplotlib plots
I’m trying to plot a figure without tickmarks or numbers on either of the axes (I use axes in the traditional sense, not the matplotlib nomenclature!). An issue I have come across is where matplotlib adjusts the x(y)ticklabels by subtracting a value N, then adds N at the end of the axis.
This may be vague, but the following simplified example highlights the issue, with ‘6.18’ being the offending value of N:
The three things I would like to know are:
How to turn off this behaviour in the first place (although in most cases it is useful, it is not always!) I have looked through matplotlib.axis.XAxis and cannot find anything appropriate
How can I make N disappear (i.e. X.set_visible(False) )
Is there a better way to do the above anyway? My final plot would be 4×4 subplots in a figure, if that is relevant.
Matplotlib: Turn Off Axis (Spines, Tick Labels, Axis Labels and Grid)

Matplotlib is one of the most widely used data visualization libraries in Python. Much of Matplotlib's popularity comes from its customization options — you can tweak just about any element from its hierarchy of objects.
In this tutorial, we'll take a look at how to turn off a Matplotlib plot's axis. That is to say — how to turn off individual elements, such as tick labels, grid, ticks and individual spines, but also how to turn them all off as a group, collectively referred to as the axis.
Most of the approaches we'll be covering work the same for 2D and 3D plots — both the regular Axes and Axes3D classes have the same methods for this. For some, since 3D plots have more than two spines, they won't be the same.
Note: For the remainder of this guide, we'll be using a 3D plot, as this is a more common case where you might want to turn off the spines and ticks — and let the data speak for itself, as well as a 2D plot on the same dataset.
We'll specifically be using a recreation of the CP1919 pulsar data, as discovered by Jocelyn Bell Burnell in 1967, visualized by by Harold D. Craft, Jr. in his PhD Thesis in 1970 and popularized by Peter Saville in 1979, when the English band Joy Division was on the lookout to release a new album — "Unknown Pleasures":
Which results in:
If you'd like to learn more about Ridge Plots (shown above), build a foundation in Matplotlib and explore advanced usage of the library, make sure to check out our Data Visualization in Python eBoook Bundle. This specific visualization is taken straight out of the book, from a longer section dedicated to this dataset and the history of the plot.
Now, let's take a look at how to remove the spines, tick labels, ticks, grid and axis labels.
Turning off the Axis with ax.axis('off')
The easiest way to turn off everything axis-related is via a convenience function — axis('off') on your Axes or Axes3D instance:
Note: You can also use the axis('off') function on the plt instance if you're plotting using the MATLAB-style API. Though, this approach is discouraged if you're plotting using the OOP API like we are currently.
This change to the code results in a plot without the spines, axis lines, ticks, tick labels, axis labels and grid:
The axis() function is a convenience function to access various properties.
Turning off the Axis with ax.set_axis_off()
Alternatively, you can use the ax.set_axis_off() function, in conjecture with the ax.set_axis_on() function, which reverses the former's effects.
This is a very useful set of functions to use when updating plots, such as when animating them, or whenever you might want to turn the axis off and on, rather than just disabling it:\
This results in the exact same plot:
Turning off Ticks and Tick Labels on Axes
Instead of turning all of these elements off, and making them invisible, you can also turn them off individually if you'd like to keep some.
Free eBook: Git Essentials
Check out our hands-on, practical guide to learning Git, with best-practices, industry-accepted standards, and included cheat sheet. Stop Googling Git commands and actually learn it!
This is where we'll be switching back to a 2D plot, since some of these functions don't work with 3D plots, given the fact that they've got more than just the X-axis and Y-axis.
For example, you can turn off individual axes (ticks and tick labels). You can use the get_yaxis() or get_xaxis() to get the respective axes and turn them off by using set_visible(False) :
This turns off the tick labels and ticks themselves, but leaves the frame (spines) on:
Turning off Individual Spines on an Axes
To turn the spines off — you can access them via the ax.spines dictionary. Using their keys, top , bottom , left , and right , you can select each one, and using the set_visible() function, turn them off.
Let's turn of the top and right spine:
This turns off the top and right spine, but leaves the bottom and left intact — as well as the ticks and their labels:
You can also iterate through them, and turn them all off:
Conclusion
In this tutorial, we've gone over several ways to turn off the axis, as well as individual axis components in Matplotlib.
If you're interested in Data Visualization and don't know where to start, make sure to check out our bundle of books on Data Visualization in Python:
Data Visualization in Python
Become dangerous with Data Visualization
✅ 30-day no-question money-back guarantee
✅ Beginner to Advanced
✅ Updated regularly for free (latest update in April 2021)
✅ Updated with bonus resources and guides
Data Visualization in Python with Matplotlib and Pandas is a book designed to take absolute beginners to Pandas and Matplotlib, with basic Python knowledge, and allow them to build a strong foundation for advanced work with these libraries — from simple plots to animated 3D plots with interactive buttons.
It serves as an in-depth guide that'll teach you everything you need to know about Pandas and Matplotlib, including how to construct plot types that aren't built into the library itself.
Data Visualization in Python, a book for beginner to intermediate Python developers, guides you through simple data manipulation with Pandas, covers core plotting libraries like Matplotlib and Seaborn, and shows you how to take advantage of declarative and experimental libraries like Altair. More specifically, over the span of 11 chapters this book covers 9 Python libraries: Pandas, Matplotlib, Seaborn, Bokeh, Altair, Plotly, GGPlot, GeoPandas, and VisPy.
It serves as a unique, practical guide to Data Visualization, in a plethora of tools you might use in your career.