Show only first column in a correlation matrix in Python

I have created a correlation matrix of a pandas dataframe using seaborn with the following commands:

corrMatrix = df.corr()
#sns.heatmap(corrMatrix, annot=True)
#plt.show()

ax = sns.heatmap(
    corrMatrix, 
    vmin=-1, vmax=1, center=0,
    cmap=sns.diverging_palette(20, 220, n=200),
    square=True, annot=True
)
ax.set_xticklabels(
    ax.get_xticklabels(),
    rotation=45,
    horizontalalignment='right'
);

I get the following matrix plot:

enter image description here

How can you mask the correlation matrix to only show the first column of the matrix? I would also like the legend show along the right side.

enter image description here

Answer

In your case x is corrMatrix[['# of Prophages']]

df = pd.DataFrame({'A': np.random.rand(8), 'B': np.random.rand(8)})
corr = df.corr()

x = corr[['A']]
sns.heatmap(x)

corr:

    A            B
A   1.000000    -0.192375
B   -0.192375   1.000000

sns.heatmap(corr):

enter image description here

sns.heatmap(x):

enter image description here

This might help you:

Credit goes to unutbu

import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import seaborn.matrix as smatrix

sns.set()

flights_long = sns.load_dataset("flights")
flights = flights_long.pivot("month", "year", "passengers")
flights = flights.reindex(flights_long.iloc[:12].month)
sns.heatmap(flights)

Result:

enter image description here


columns = [1953]
myflights = flights.copy()
mask = myflights.columns.isin(columns)
myflights.loc[:, mask] = 0
arr = flights.values
vmin, vmax = arr.min(), arr.max()
sns.heatmap(flights, mask=myflights, annot=True, fmt="d", vmin=vmin, vmax=vmax)

Output:

enter image description here


columns = [1953]
myflights = flights.copy()
mask = myflights.columns.isin(columns)
myflights = myflights.loc[:, mask]
arr = flights.values
vmin, vmax = arr.min(), arr.max()
sns.heatmap(myflights, annot=True, fmt="d", vmin=vmin, vmax=vmax)

Output:

enter image description here