Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 249
KcFnMi Avatar asked Nov 20 '16 15:11

KcFnMi


People also ask

How can we avoid axis values with 1e7 in pandas and Matplotlib?

If you mean you do not want to see 1e7 at the y-axis, divide your data by 1e7. 1e7 is gone now, but now I have on the y-axis 0.0 to 2.5 which does not represent values in DataFrame.

How do I remove the Y axis label in Seaborn?

To remove X or Y labels from a Seaborn heatmap, we can use yticklabel=False.


1 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

like image 58
wwii Avatar answered Oct 16 '22 19:10

wwii