修改matplotlib色条图中二维数组的坐标轴
我有一个二维的numpy数组,想把它画成一个带颜色的图表。但是我在调整坐标轴的时候遇到了问题。现在的纵轴是从0到100向下显示,而我希望它是从0.0到0.1向上显示。所以我需要做两件事:
- 使用np.flipud()翻转数组,然后也要“翻转”坐标轴
- 把标签改成从0.0到0.1,而不是从0到100
这是我现在的颜色图表的样子:

这是我的代码:
data = np.load('scorr.npy')
(x,y) = np.unravel_index(data.argmax(), data.shape)
max=data[x][y]
fig = plt.figure()
ax = fig.add_subplot(111)
cax = ax.imshow(data, interpolation='nearest')
cbar = fig.colorbar(cax, ticks=[-max, 0, max])
cbar.ax.set_yticklabels([str(-max), '0', str(max)])
plt.show()
有没有人有什么建议?提前谢谢大家!
2 个回答
0
我知道的唯一一种方法来更改图像图的坐标轴标签,就是手动标记... 如果有人有更简单的方法,我很想学习一下。
ax.yaxis.set_ticks(np.arange(0,100,10))
ax.yaxis.set_ticklabels(['%.2f' % 0.1/100*i for i in np.arange(0,100,10)])
9
你可能想了解一下imshow的“origin”和“extent”这两个选项。
import matplotlib.pyplot as plt
import numpy as np
x,y = np.mgrid[-2:2:0.1, -2:2:0.1]
data = np.sin(x)*(y+1.05**(x*np.floor(y))) + 1/(abs(x-y)+0.01)*0.03
fig = plt.figure()
ax = fig.add_subplot(111)
ticks_at = [-abs(data).max(), 0, abs(data).max()]
cax = ax.imshow(data, interpolation='nearest',
origin='lower', extent=[0.0, 0.1, 0.0, 0.1],
vmin=ticks_at[0], vmax=ticks_at[-1])
cbar = fig.colorbar(cax,ticks=ticks_at,format='%1.2g')
fig.savefig('out.png')