user1767754
user1767754

Reputation: 25094

matplotlib draw values on top of graph/image

I'm plotting a simple graph but would like to draw all values on top to visualize the values. So how would I go from this:

import matplotlib.pyplot as plt
img = []
for x in range(0, 8):
    row = []
    for y in range(0, 8):
        row.append(x+y)
    img.append(row)
plt.imshow(img)

enter image description here

to this:

enter image description here

Upvotes: 0

Views: 434

Answers (2)

racemaniac
racemaniac

Reputation: 36

plt.text does the job:

import matplotlib.pyplot as plt
img = []
for x in range(0, 8):
    row = []
    for y in range(0, 8):
        row.append(x+y)
        plt.text(x, y, "X", horizontalalignment='center', verticalalignment='center', color="white")
    img.append(row)
plt.imshow(img)

Upvotes: 1

Ananda
Ananda

Reputation: 3272

You can use plt.text

import matplotlib.pyplot as plt
img = []
for x in range(0, 8):
    row = []
    for y in range(0, 8):
        row.append(x+y)
        plt.text(x, y, x+y, c='w')
    img.append(row)


plt.imshow(img)
plt.show()

Upvotes: 1

Related Questions