2015-03-02 105 views
2

我當前的代碼如下所示:把刻度值

import matplotlib.pyplot as plt 
import numpy as np 

s = 5 
x = np.linspace(-10,10,100) 
y = np.linspace(-10,10,100) 
xg = np.exp(-(x**2)/(s**2)) 
yg = np.exp(-(y**2)/(s**2)) 

vals = np.meshgrid(xg,yg) 

fig, ax = plt.subplots() 

cax = ax.imshow(vals[0]*vals[1]) 
ax.axis("off") 
fig.colorbar(cax,ticks=[0.0,1.0],label="Values (0-1)") 

將會產生以下的輸出:

enter image description here

我想刻度值是在顏色條的頂部和底部(而不是在側面),如下所示:

enter image description here

目前,我已經使用Illustrator完成了這項工作,但我計劃生成很多像這樣的圖像(對於視頻),並且希望自動繪製這些圖。

回答

2

如果你想讓它們很好地集中在顏色條上,那麼你不能使用yticklabels來實現這一點。但是,您可以手動添加兩個文本字段如下:

cb = plt.colorbar(im, ticks=[], label="Values (0-1)") 
cb.ax.text(0.5, -0.01, '0', transform=cb.ax.transAxes, 
    va='top', ha='center') 
cb.ax.text(0.5, 1.0, '1', transform=cb.ax.transAxes, 
    va='bottom', ha='center') 

結果: enter image description here

+0

這不正是我之後。非常感謝! – Matt 2015-03-02 20:02:14