Python 在 Pandas 图中仅隐藏轴标签,而不是整个轴

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40705614/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 23:52:38  来源:igfitidea点击:

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

pythonpython-2.7python-3.xpandas

提问by KcFnMi

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

我可以使用以下命令清除 Pandas 图中 xlabel 的文本:

plt.xlabel("")

Instead, is it possible to hide the label?

相反,是否可以隐藏标签?

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

可能是类似的东西.xaxis.label.set_visible(False)

回答by wwii

From the Pandas docs-

来自 Pandas 文档-

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

Series 和 DataFrame 上的 plot 方法只是对 plt.plot() 的简单包装:

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

这意味着你可以用 matplolib 做的任何事情,你都可以用 Pandas DataFrame 图来做。

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

pyplot 有一种axis()方法可以让您设置轴属性。在调用plt.axis('off')之前调用plt.show()将关闭两个轴。

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

要控制单个轴,您需要通过绘图的Axes设置其属性。对于 x 轴 - (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

您也可以像这样到达 x 轴

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

数据框.plot

returns a matplotlib.axes.Axesor numpy.ndarray of them

返回它们的matplotlib.axes.Axes或 numpy.ndarray

so you can getit/them when you call it.

这样你就可以在打电话时得到它/它们。

axs = df.plot()


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

.set_visible()是一种艺术家方法。轴和它们的标签是 Artists 因此它们具有Artist 方法/属性以及它们自己的. 有很多方法可以自定义您的绘图。有时您可以在浏览图库示例时找到您想要的功能