Hide axis label only, not entire axis, in Pandas plot

Question:

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).

Asked By: KcFnMi

||

Answers:

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

Answered By: wwii

You can remove axis labels and ticks using xlabel= or ylabel= arguments in the 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

Answered By: cottontail