5

I create a pandas scatter-matrix usng the following code:

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

This result in the following scatter-matrix: enter image description here

The first row has no ytick labels, the 3th column no xtick labels, the 3th item 'C' is not labeled.

Any idea how to complete this plot with the missing labels ?

4

2 回答 2

5

Access the subplot in question and change its settings like so.

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

You can inspect how the scatter_matrix function goes about labeling at the link below. If you find yourself doing this over and over, consider copying the code into file and creating your own custom scatter_matrix function.

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

Edit, in response to a rejected comment:

The obvious extensions of this, doing ax[0, 0].xaxis.set_visible(True) and so forth, do not work. For some reason, scatter_matrix seems to set up ticks and labels for axes[2, 2] without making them visible, but it does not set up ticks and labels for the rest. If you decide that it is necessary to display ticks and labels on other subplots, you'll have to dig deeper into the code linked above.

Specifically, change the conditions on the if statements to:

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

respectively. I haven't tested that, but I think it will do the trick.

于 2013-01-24T23:09:21.387 回答
1

Since I can't reply above, the non-changing source code version for anybody googling is:

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 is the list of column names

于 2013-09-25T00:35:02.743 回答