【问题标题】:How can I disable the label when plotting pandas data?绘制熊猫数据时如何禁用标签?
【发布时间】:2016-03-23 02:39:05
【问题描述】:

我尝试在 plot 命令中使用label=None,在这种情况下matplotlib 选择了数据的键作为标签。我发现这种行为不直观,并且当我明确设置标签时,我期望它可以完全控制它。 如何禁用涉及来自 pandas 数据帧的数据的绘图标签?

这是一个小例子,它显示了行为:

import matplotlib.pyplot as plt
import pandas as pd

df = pd.DataFrame({'x': [0, 1], 'y': [0, 1]})
plt.plot(df['x'], df['y'], label=None)
plt.legend(loc='best')

这将导致以下情节:

如果重要的话,我目前在从 macports 安装的以下 python 中使用 matplotlib 1.5.1 版和 pandas 0.18.0 版: Python 2.7.11(默认,2016 年 3 月 1 日,18:40:10) [GCC 4.2.1 兼容 Apple LLVM 7.0.2 (clang-700.1.81)] 达尔文

【问题讨论】:

  • 可以传入空字符串:plt.plot(df['x'], df['y'], label='')
  • 你能把这个作为答案发给我吗?

标签: python pandas matplotlib


【解决方案1】:

如果你传递一个空字符串,那么它会导致这个情节:

import matplotlib.pyplot as plt
import pandas as pd

df = pd.DataFrame({'x': [0, 1], 'y': [0, 1]})
plt.plot(df['x'], df['y'], label='')
plt.legend(loc='best')

【讨论】:

    【解决方案2】:

    只需传递一个空标签。在这种情况下,Matplotlib 不会费心制作图例条目。 label=None 参数是默认值,绘图函数会检测到该参数,并告诉它组成一个标签。

    (当然,在这种情况下,你不能使用plt.legend...)

    【讨论】:

      猜你喜欢
      • 2015-06-10
      • 1970-01-01
      • 2016-04-10
      • 2018-02-10
      • 2015-03-29
      • 2017-10-10
      • 2020-05-31
      • 2017-05-28
      • 2022-01-08
      相关资源
      最近更新 更多