2

I have a DataFrame that resembles the following:

Name V1 V2 V3
A    1  0  0
B    1  0  1
C    1  1  0

etc.

I would like to visualize it as a 2-d matrix with labeled rows and columns and with cells colored based on whether the value is 1 or 0.

In other words, I would like to do something like this: Conditional coloring of a binary data frame in R

but in Python.

Any help is appreciated!

1
  • You can go check heatmap from seaborn Commented Nov 18, 2017 at 18:59

2 Answers 2

4

Fleshing out Wen's comment into an answer - use seaborn.heatmap:

import matplotlib.pyplot as plt
import seaborn as sns

sns.set()

df = df.set_index('Name')
df

      V1  V2  V3
Name            
A      1   0   0
B      1   0   1
C      1   1   0
sns.heatmap(df)
plt.show()

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

@cs96: Great, how to change the color legend tick labels? I want to display simply 0 and 1.
1

Could plot them as an image with various cmap alternatives: https://matplotlib.org/examples/color/colormaps_reference.html

But from here you would need more formatting...

import matplotlib.pyplot as plt
plt.imshow(df.values, interpolation="nearest", cmap='Blues')
plt.show()

enter image description here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.