简体   繁体   中英

How to assign arbitrary x_tick_labels and y_tick_labels in im.show figure?

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?

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 eg here ); you can use "%d" if you want integers.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM