简体   繁体   中英

How can I see the scale bar in matshow?

I want to see the scale bar in matshow, I looked for quite long and didn't find the answer. How do I do that?

The code is very simple:

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

As far as I know the scale bar is not part of the native functions of matplotlib. You can do it, thought, by using matplotlib-scalebar . In the link you'll find a code example:

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

, which should result in this:

来自matplotlib scalebar lib的matplotlib比例尺

I have not tried it (I don't have that lib installed) but it should be easy enough to install from pip:

pip install matplotlib-scalebar

Just in case you are looking for a colorbar (mistakes do happen) you can use this:

plt.colorbar()

, which together with a matshow (example adapted from 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()

, would result in this:

matshow与colorbar

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