2

I am trying to visualize a random array with the square shape of (10, 10) in Python using matplotlib (3.5 version). I am also including the xaxis and yaxis ticks, but the ticks for 10 show empty data. Does anyone know how to sort it out?

Here's my code:

import numpy as np

from matplotlib import pyplot as plt
import matplotlib.pylab as pylab
params = {'legend.fontsize': 'medium',
          'figure.figsize': (15, 5),
         'axes.labelsize': 'x-large',
         'axes.titlesize':'x-large',
         'xtick.labelsize':'x-large',
         'ytick.labelsize':'x-large'}
pylab.rcParams.update(params)

arr = np.random.rand(10, 10)
plt.imshow(arr)
plt.ylim(0, 10)
plt.xlim(0, 10)

plt.xticks(np.arange(0.0, 11.0, 1.0))
plt.yticks(np.arange(0.0, 11.0, 1.0))

plt.show()

This is the produced image:

enter image description here

2
  • 1
    Because arrays are zero indexed in python, and you are asking for an array of size 10, so 0-9? Commented Nov 25, 2021 at 14:40
  • 1
    The pyhton arrays starts it's indexin in 0. So you will have values between 0-9 in both axis, note that also every tick points to the center of "pixel". You could modify your lims plt.xlim((-0.5,9.5)) and plt.ylim((-0.5,9.5)). Last if you really need the numbers to be between 1 and 10 you could use xticks and yticks: plt.xticks(np.arange(10), np.arange(1,11)) and plt.yticks(np.arange(10), np.arange(1,11)) Note that if you want the tick to start at left-down corner of pixel you can play with the first arange in ticks (i think it must be np.arange(-0.5,9.5,1) but im not sure) Commented Nov 25, 2021 at 14:46

1 Answer 1

1

As other users pointed, Python arrays start indexing in '0'. You could trick the ticks to show the values you want:

  1. create data to plot

    import numpy as np
    
    from matplotlib import pyplot as plt
    import matplotlib.pylab as pylab
    params = {'legend.fontsize': 'medium',
              'figure.figsize': (15, 5),
             'axes.labelsize': 'x-large',
             'axes.titlesize':'x-large',
             'xtick.labelsize':'x-large',
             'ytick.labelsize':'x-large'}
    pylab.rcParams.update(params)
    
    arr = np.random.rand(10, 10)
    
    
    
  2. Then you can use plt.xticks(tickPosition,tickValues) and same with yticks. Note that every number point to the center of every value in your image so also you have to change your lim positions:

    plt.figure()
    plt.imshow(arr)
    plt.ylim(-0.5, 9.5) #to show no blank spaces
    plt.xlim(-0.5, 9.5) #to show no blank spaces
    
    plt.xticks(np.arange(0, 10),np.arange(1, 11)) #trick the ticks
    plt.yticks(np.arange(0, 10),np.arange(1, 11))

This will give you the next figure

Tricked points

  1. Also you can set values at start (left-down corner) just tricking a little more:
    plt.xticks(np.arange(-0.5, 9.5),np.arange(1, 11))
    plt.yticks(np.arange(-0.5, 9.5),np.arange(1, 11))

this will give you this result:

with ticks at "start" of pixel

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.