Jack Twain
Jack Twain

Reputation: 6382

Creating a correlation plot with matplotlib

I'm trying to create a correlation matrix plot with matplotlib in python. However I'm not able to make the x and y ticks to be in the middle of each square and also I need the xticks to be vertical. I need them vertical because I will have about 15 variables to plot, so having the ticks in horizontal way won't fit.

Here is my attempt:

import numpy as np
import matplotlib.pyplot as plt

X = np.random.randn(10,3)
X = X.T
R = np.corrcoef(X)
plt.figure()
plt.xticks([0,1,2], ["first", "second", "third"], rotation='vertical')
plt.yticks([0,1,2], ["first", "second", "third"])
plt.pcolor(R)

Result:

enter image description here

Edit: I just figured out that using rotation='vertical' solves the x-ticks demand for me.

Upvotes: 1

Views: 1172

Answers (1)

Lee
Lee

Reputation: 31100

To centralise the ticks you need to add 0.5 to each value:

plt.yticks([0.5,1.5,2.5], ["first", "second", "third"])
plt.xticks([0.5,1.5,2.5], ["first", "second", "third"], rotation='vertical')

Also, you might want to add the following so that the overall figure size is adjusted to take account of the rotated x labels:

plt.tight_layout()

Upvotes: 1

Related Questions