0
import numpy as np
import matplotlib.pyplot as plt
np.random.seed(123)
data=np.random.rand(100).reshape(10,10)
fig,ax=plt.subplots(figsize=(15,12))
im = ax.imshow(data, vmin=0, vmax=1,cmap='Blues')

Here is an example, where the x and y tick labels are the grid numbers. But how can I get x_tick_labels from 0 to 100 or from 0 to 1?

Ivy Gao
  • 111
  • 1
  • 8
  • `data=np.random.rand(100).reshape(1,-1) ` hope this is what you are asking for.. – EMKAY Sep 29 '18 at 09:16
  • Set a locator and formatter: https://matplotlib.org/api/ticker_api.html and https://matplotlib.org/gallery/ticks_and_spines/tick-formatters.html – Mad Physicist Sep 29 '18 at 09:16
  • @EMKAY. Not even close. Why are you reshaping when you can just pass on the desired shape? – Mad Physicist Sep 29 '18 at 09:17
  • Just to be sure, you want the tick under the first square to be 0 and that under the last square to be 100 or do you want a tick zero on the left of the first square and a ticks 100 on the right of the last square? – Silmathoron Sep 29 '18 at 09:26

1 Answers1

1

I've always found the matplotlib locators and formatters to be uneasy to use for many practical purposes, so if the links given by Mad Physicist do not suit you, you can just set them manually:

n, m = data.shape
ax.set_xticks(np.linspace(0, n-1, n))
ax.set_xticklabels(["%.1f" % i for i in np.linspace(0, 100, n)])

to format the xaxis from 0 to 100 with ticks on the middle of the squares, or

n, m = data.shape
ax.set_xticks(np.linspace(-0.5, n-0.5, n+1))
ax.set_xticklabels(["%.1f" % i for i in np.linspace(0, 100, n+1)])

for ticks on the left/right sides of the squares.

Note that the "%.1f" is used to format the string to a float with 1 zero after the comma (see e.g. here); you can use "%d" if you want integers.

Silmathoron
  • 1,781
  • 1
  • 15
  • 32