大熊猫3x3散射矩阵缺少标签

时间:2013-01-24 22:12:05

标签: python matplotlib pandas

我使用以下代码创建一个pandas scatter-matrix:

import numpy as np
import pandas as pd

a = np.random.normal(1, 3, 100)
b = np.random.normal(3, 1, 100)
c = np.random.normal(2, 2, 100)

df = pd.DataFrame({'A':a,'B':b,'C':c})
pd.scatter_matrix(df, diagonal='kde')

这导致以下散布矩阵: enter image description here

第一行没有ytick标签,第3行没有xtick标签,第3项“C”没有标记。

知道如何用缺少的标签完成这个情节吗?

2 个答案:

答案 0 :(得分:5)

访问相关的子图并更改其设置。

axes = pd.scatter_matrix(df, diagonal='kde')
ax = axes[2, 2] # your bottom-right subplot
ax.xaxis.set_visible(True)
draw()

您可以在下面的链接中检查scatter_matrix函数的标记方式。如果您发现自己一遍又一遍地这样做,请考虑将代码复制到文件中并创建自己的自定义scatter_matrix函数。

https://github.com/pydata/pandas/blob/master/pandas/tools/plotting.py#L160

编辑,以回复被拒绝的评论:

明显的扩展,执行ax[0, 0].xaxis.set_visible(True)等等,不起作用。出于某种原因,scatter_matrix似乎为轴[2,2]设置了刻度和标签而没有使它们可见,但它没有为其余部分设置刻度和标签。如果您认为有必要在其他子图上显示刻度和标签,则必须深入研究上面链接的代码。

具体而言,将if语句的条件更改为:

if i == 0
if i == n-1
if j == 0
if j == n-1

分别。我没有测试过,但我认为它会起作用。

答案 1 :(得分:1)

由于我无法回复上述内容,因此任何人用谷歌搜索的不变的源代码版本是:

n = len(features)

for x in range(n):
    for y in range(n):
        sax = axes[x, y]
        if ((x%2)==0) and (y==0):
            if not sax.get_ylabel():
                sax.set_ylabel(features[-1])       
            sax.yaxis.set_visible(True)

        if (x==(n-1)) and ((y%2)==0):
            sax.xaxis.set_visible(True)

        if ((x%2)==1) and (y==(n-1)):
            if not sax.get_ylabel():
                sax.set_ylabel(features[-1])       
            sax.yaxis.set_visible(True)

        if (x==0) and ((y%2)==1):
            sax.xaxis.set_visible(True)

features是列名列表

相关问题