1

let's assume that I have this dataframe df transfered from a matrix:

    1   2   3   4   5   6   7   8

1   1399    17  4   3   0   0   0   0
2   11  374     2   3   1   4   0   1
3   7   0   187     4   0   0   1   1
4   2   3   4   308     0   0   0   3
5   2   0   0   0   280     3   0   1
6   0   2   0   0   2   81  0   3
7   1   0   2   0   2   0   154     4
8   0   0   1   2   1   1   8   552

I would like to plot this as a table in which the higher values are colored darker whereas the smaller values are colored lighter depending on their scalar value. But I am not sure if it is possible. All the plotting techniques seem to not include tables with colors. Any ideas?

Thanks

2

2 Answers 2

3

Use matplotlib.pyplot.subplots:

import matplotlib.pyplot as plt

fig, ax = plt.subplots()
ax.matshow(df, cmap=plt.cm.Greys)

Output:

enter image description here

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

1 Comment

What about the content of the grids? How do I make it print the values as well ?
1

Here is another way:

import matplotlib.pyplot as plt
import numpy as np

a = df.to_numpy()
fig, ax = plt.subplots()

for i in range(df.shape[0]):
    for j in range(df.shape[1]):
         text = ax.text(j, i, a[i, j],
                        ha="center", va="center", color="w")

ax.set_title("Your title")
plt.imshow(a, cmap='hot', interpolation='nearest')
plt.show()

enter image description here

For more details you may check this URL.

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.