49

I can clear the text of the xlabel in a Pandas plot with:

plt.xlabel("")

Instead, is it possible to hide the label?

May be something like .xaxis.label.set_visible(False).

0

2 Answers 2

87

From the Pandas docs -

The plot method on Series and DataFrame is just a simple wrapper around plt.plot():

This means that anything you can do with matplolib, you can do with a Pandas DataFrame plot.

pyplot has an axis() method that lets you set axis properties. Calling plt.axis('off') before calling plt.show() will turn off both axes.

df.plot()
plt.axis('off')
plt.show()
plt.close()

To control a single axis, you need to set its properties via the plot's Axes. For the x axis - (pyplot.axes().get_xaxis().....)

df.plot()
ax1 = plt.axes()
x_axis = ax1.axes.get_xaxis()
x_axis.set_visible(False)
plt.show()
plt.close()

Similarly to control an axis label, get the label and turn it off.

df.plot()
ax1 = plt.axes()
x_axis = ax1.axes.get_xaxis()
x_axis.set_label_text('foo')
x_label = x_axis.get_label()
##print isinstance(x_label, matplotlib.artist.Artist)
x_label.set_visible(False)
plt.show()
plt.close()

You can also get to the x axis like this

ax1 = plt.axes()
x_axis = ax1.xaxis
x_axis.set_label_text('foo')
x_axis.label.set_visible(False)

Or this

ax1 = plt.axes()
ax1.xaxis.set_label_text('foo')
ax1.xaxis.label.set_visible(False)

DataFrame.plot

returns a matplotlib.axes.Axes or numpy.ndarray of them

so you can get it/them when you call it.

axs = df.plot()

.set_visible() is an Artist method. The axes and their labels are Artists so they have Artist methods/attributes as well as their own. There are many ways to customize your plots. Sometimes you can find the feature you want browsing the Gallery and Examples

Sign up to request clarification or add additional context in comments.

Comments

16

Pandas >= 1.1.0 answer

You can remove axis labels and ticks using xlabel= or ylabel= arguments in the DataFrame.plot() call. For example, to remove the xlabel, use xlabel='':

df.plot(xlabel='');

res1

To remove the x-axis ticks, use xticks=[] (for y-axis ticks, use yticks=):

df.plot(xticks=[]);

res2

To remove both:

df.plot(xticks=[], xlabel='');

res3

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.