Some values of matrix do not appear in the plot by Matplotlib

前端 未结 1 1340
抹茶落季
抹茶落季 2020-12-11 11:20

I created an empty reference matrix from CSV, located (x,y) as a position on matrix (and printed them out), and designated 100 to that position on matrix. Each x is the valu

相关标签:
1条回答
  • 2020-12-11 11:38

    Every pixel in the final image represents 3 or more data points. The renderer then has to decide which color out of 2 times blue, 1 time white to map to that pixel. Statistically, this will be blue twice as often as white, such that 66% of the data points are not shown.

    The number of 3 pixels comes from a rough calculation: You image has 480 pixels (which you can either find out in an picture program or by calculating figuresize*dpi). You have 1200 datapoints (seen from the axes). You have margin of ~10% at both ends; so you have roughly 1200/(0.8*480) = 3.1 datapoints per pixel in the final image.

    imshow interpolation

    You can use an interpolation on the image to make those pixels appear, e.g.

    plt.imshow(..., interpolation="sinc")
    

    The result may however not be very appealing visually.

    change resolution

    You can also make sure your final plot comprises exactly one pixel per datapoint. I.e. for 1200 pixels with a dpi of 100 you can do

    m = 0.1
    plt.figure(figsize=(8, (1+2.*m)*1200./dpi ))
    plt.subplots_adjust(bottom=m, top=1.-m)
    plt.imshow(...)
    

    filter data

    Another option is to change the data, such that one pixel becomes three pixels along the y direction.

    import matplotlib.pyplot as plt
    import numpy as np; np.random.seed(1)
    import scipy.ndimage.filters as filters
    
    a = np.zeros((1200, 16))
    for i in range(16):
        a[i*70+21, i] = 1
    
    kernel = np.array([[0.,1.,0.],
                       [0.,1.,0.],
                       [0.,1.,0.]])
    anew = filters.convolve(a,kernel,mode='constant', cval=0)
    
    im = plt.imshow(anew, aspect="auto")
    plt.colorbar(im)
    
    plt.show()
    

    drawing lines

    import matplotlib.pyplot as plt
    import numpy as np
    
    a = np.zeros((1200, 16))
    
    im = plt.imshow(a, aspect="auto", vmin=0, vmax=1)
    plt.colorbar(im)
    
    for i in range(16):
        plt.plot([i-.5, i+.5], [i*70+21,i*70+21], color=im.cmap(1.))
    
    plt.show()
    
    0 讨论(0)
提交回复
热议问题