Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to customize a scatter matrix to see all titles?

I'm running this code to build a scatter matrix. The problem is that the plot looks like a mess, because it's impossible to see the names of variables (see image below). Is there any way to change the orientation of titles and switch off the ticks with numbers?

import pandas as pd
import matplotlib.pyplot as plt

train = pd.read_csv('data/train.csv', parse_dates=[0])

plt.figure()
a = pd.scatter_matrix(train, alpha=0.05, figsize=(10,10), diagonal='hist')
plt.show()

enter image description here

like image 366
Klausos Klausos Avatar asked Sep 14 '15 09:09

Klausos Klausos


People also ask

What is scatter plot matrix?

A scatter plot matrix is a grid (or matrix) of scatter plots used to visualize bivariate relationships between combinations of variables. Each scatter plot in the matrix visualizes the relationship between a pair of variables, allowing many relationships to be explored in one chart.


2 Answers

As a minimal scatter_matrix example to switch off axis ticks and rotate the labels,

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
try:
    from pandas.tools.plotting import scatter_matrix
except ImportError:
    #Fix suggested by @Raimundo Jimenez as tools is deprecated
    from pandas.plotting import scatter_matrix

df = pd.DataFrame(np.random.randn(1000, 4), columns=['long label', 'testing', 'another label', 'something else'])

sm = scatter_matrix(df, alpha=0.2, figsize=(6, 6), diagonal='kde')

#Change label rotation
[s.xaxis.label.set_rotation(45) for s in sm.reshape(-1)]
[s.yaxis.label.set_rotation(0) for s in sm.reshape(-1)]

#May need to offset label when rotating to prevent overlap of figure
[s.get_yaxis().set_label_coords(-0.3,0.5) for s in sm.reshape(-1)]

#Hide all ticks
[s.set_xticks(()) for s in sm.reshape(-1)]
[s.set_yticks(()) for s in sm.reshape(-1)]

plt.show()

and similarly, you can adjust labels, resize, etc with any of the axis objects contained in the returned handle from scatter_matrix. This results in,

enter image description here

like image 115
Ed Smith Avatar answered Oct 29 '22 15:10

Ed Smith


pandas.tools.plotting.scatter_matrix is now deprecated. Use pandas.plotting.scatter_matrix instead.

Updated code from the one proposed by Ed Smith (@ed-smith):

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

df = pd.DataFrame(np.random.randn(1000, 4), columns=['long label', 'testing', 'another label', 'something else'])

sm = pd.plotting.scatter_matrix(df, alpha=0.2, figsize=(6, 6), diagonal='kde')

#Change label rotation
[s.xaxis.label.set_rotation(45) for s in sm.reshape(-1)]
[s.yaxis.label.set_rotation(0) for s in sm.reshape(-1)]

#May need to offset label when rotating to prevent overlap of figure
[s.get_yaxis().set_label_coords(-0.3,0.5) for s in sm.reshape(-1)]

#Hide all ticks
[s.set_xticks(()) for s in sm.reshape(-1)]
[s.set_yticks(()) for s in sm.reshape(-1)]

plt.show()
like image 31
Raimundo Jimenez Avatar answered Oct 29 '22 17:10

Raimundo Jimenez