2016-05-15 117 views
2

我想在matshow中看到比例尺,我查了很久,沒有找到答案。我怎麼做?如何在matshow中看到比例尺?

的代碼非常簡單:

def analyze_results(): 
    l_points = [np.array([10, 9, -1]), np.array([-4, 4, 1]), np.array([-6, 2, -1]), np.array([ 7, -2, 1]), np.array([-3, 2, -1]), np.array([ 3, -5, -1]), np.array([-5, 10, 1]), np.array([-10, 9, -1]), np.array([ 4, -4, 1]), np.array([-4, 7, 1])] 
    num_elemnts = 2 * const_limit + 1 
    loss = np.zeros((num_elemnts, num_elemnts)) 
    for i in range(-const_limit, const_limit + 1): 
     for j in range(-const_limit, const_limit + 1): 
      if ((i == 0) & (j == 0)): 
       continue 
      w = (i, j) 
      loss[i, j] , _ = gradient_hinge_loss(l_points, w) 

    return loss 

if __name__ == '__main__': 
    loss_hinge_debugger = analyze_results() 
    plt.matshow(loss_hinge_debugger) 
    plt.show() 

回答

3

據我所知scale bar不是matplotlib的原生功能的一部分。你可以通過使用matplotlib-scalebar來做到這一點。在鏈接,你會發現一個代碼示例:

import matplotlib.pyplot as plt 
import matplotlib.cbook as cbook 
from matplotlib_scalebar.scalebar import ScaleBar 
plt.figure() 
image = plt.imread(cbook.get_sample_data('grace_hopper.png')) 
plt.imshow(image) 
scalebar = ScaleBar(0.2) # 1 pixel = 0.2 meter 
plt.gca().add_artist(scalebar) 
plt.show() 

,這將導致在此:

matplotlib scale bar from matplotlib scalebar lib

我還沒有嘗試過(我沒有安裝LIB),但它應該是很容易從PIP安裝:

pip install matplotlib-scalebar 

萬一你正在尋找一個colorbar(錯誤■不要發生),你可以使用這個:

plt.colorbar() 

,具有matshow(例如改編自here),它們一起:

import matplotlib.pyplot as plt 

def samplemat(dims): 
    """Make a matrix with all zeros and increasing elements on the diagonal""" 
    aa = np.zeros(dims) 
    for i in range(min(dims)): 
     aa[i, i] = i 
    return aa 

# Display 2 matrices of different sizes 
dimlist = [(12, 12), (15, 35)] 
#for d in dimlist: 
plt.matshow(samplemat(dimlist[0])) 
plt.colorbar() 

plt.show() 

,會導致這樣的:

matshow with colorbar

+0

這就是10倍 –