【问题标题】:X axis label and minor tick labels do not show on Pandas scatter plot [duplicate]X 轴标签和次要刻度标签不显示在 Pandas 散点图上 [重复]
【发布时间】:2021-12-08 03:59:32
【问题描述】:

我用 Python 3.8.12 创建了一个新环境并安装了以下包:

  • matplotlib (3.4.3)
  • pandas (1.3.3)
  • scikit-learn (0.24.2)

我在 Jupyter 笔记本中运行代码。

我正在使用来自pandas.DataFrame.plot.scatter 文档中两个示例的代码:

第一个例子的输出

df = pd.DataFrame([[5.1, 3.5, 0], [4.9, 3.0, 0], [7.0, 3.2, 1],
                   [6.4, 3.2, 1], [5.9, 3.0, 2]],
                  columns=['length', 'width', 'species'])

ax1 = df.plot.scatter(x='length',
                      y='width',
                      c='DarkBlue')

确实与文档中的情节相匹配。

但是:第二个示例的输出

ax2 = df.plot.scatter(x='length',
                      y='width',
                      c='species',
                      colormap='viridis')

与文档中的绘图匹配:X 轴标签和次要刻度标签未显示在绘图(底部)上。

Plot from documentation

我的阴谋

我试过ax2.set_xlabel("length")(这应该是必要的),它没有效果。

这是一个错误吗?有人可以尝试复制吗?

【问题讨论】:

    标签: python pandas plot


    【解决方案1】:

    在 Jupyter(笔记本)中使用 matplotlib 后端时,这是 Pandas 的一个已知且未解决的问题。

    此问题已在 #10611 中报告并解决,但返回 0.24.0。

    BUG: Scatterplot x-axis label disappears with colorscale when using matplotlib backend #36064

    最简单的解决方法是将sharex=False 传递给pandas.DataFrame.plot.scatter。

    感谢@rftr 为我指明了正确的方向!

    【讨论】:

      猜你喜欢
      • 2021-06-01
      • 1970-01-01
      • 2012-03-14
      • 1970-01-01
      • 1970-01-01
      • 2013-06-22
      • 2017-08-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多