0

I have the following script to generate a heatmap of NxN cores :

import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
import numpy as np

matrix = np.random.randint(300,high=400, size=(4, 4))

heatmap = plt.pcolor(np.array(matrix))

for y in range(len(matrix)):
    for x in range(len(matrix[0])):
        plt.text(x + 0.5, y + 0.5, '%.4f' % matrix[y][x],
                 horizontalalignment='center',
                 verticalalignment='center',
                 )

plt.colorbar(heatmap)
plt.xlabel ("Cores")
plt.ylabel ("Cores")
plt.title ("Temperature Map of the NxN Cores")
plt.savefig ("xyz"+".png")
plt.show()
plt.clf()

It produces this :

As each of squares depict a core, the values of 0.5, 1.5 etc. don't make any sense.

I want the axis values to be integers only. I looked up the docs for this but could not find a way to achieve this.

1 Answer 1

1

You can explicitly set where you want to tick marks with plt.xticks and plt.yticks.

For your example:

plt.xticks(range(min(x), max(x)+1))
plt.yticks(range(min(y), max(y)+1))
plt.show()

If your data in not integers, you could use:

import numpy as np
plt.xticks(np.arange(min(x), max(x)+1, 1.0))
plt.yticks(np.arange(min(y), max(y)+1, 1.0))
plt.show()
Sign up to request clarification or add additional context in comments.

3 Comments

That works. I t would be nice to include where you find such information, so that others may also use the source.
I knew it because I found it previously in stack overflow.
But here is the formal page: matplotlib.org/api/…

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.