Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/348.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在matshow中看到比例尺?_Python_Matplotlib - Fatal编程技术网

Python 如何在matshow中看到比例尺?

Python 如何在matshow中看到比例尺?,python,matplotlib,Python,Matplotlib,我想看看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.arr

我想看看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()
据我所知,它不是matplotlib本机函数的一部分。你可以这样做,思想,通过使用。在链接中,您将看到一个代码示例:

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()
,这将导致:

我没有尝试过(我没有安装该lib),但从pip安装应该很容易:

pip install matplotlib-scalebar
以防万一您正在寻找一个错误(确实会发生错误),您可以使用以下方法:

plt.colorbar()
,连同matshow(示例改编自):

,将导致:

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()