在 pandas 图中仅隐藏轴标签,而不是整个轴 [英] Hide axis label only, not entire axis, in Pandas plot

查看:70
本文介绍了在 pandas 图中仅隐藏轴标签,而不是整个轴的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

plt.xlabel("")

相反,可以隐藏标签吗?

Instead, is it possible to hide the label?

可能类似于.xaxis.label.set_visible(False).

推荐答案

来自熊猫文档-

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

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

这意味着您可以使用matplolib进行任何操作,也可以使用Pandas DataFrame图进行操作.

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

pyplot具有axis() 方法,可用于设置轴属性.在调用plt.show()之前先调用plt.axis('off')将会关闭两个轴.

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

要控制单个轴,您需要通过图的.对于x轴-(pyplot.axes().get_xaxis(). .... )

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

您也可以像这样到达x轴

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)

或者这个

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


DataFrame.plot

返回一个 matplotlib.axes.Axes 或其中的numpy.ndarray

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() Artist 方法.轴及其标签是艺术家,因此它们具有艺术家方法/属性以及他们自己的.有很多方法可以自定义图.有时,您可以找到想要浏览的功能图库


.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

这篇关于在 pandas 图中仅隐藏轴标签,而不是整个轴的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆