python - Some values of matrix do not appear in the plot by Matplotlib -
i created empty reference matrix csv, located (x,y) position on matrix (and printed them out), , designated 100 position on matrix. each x value in ref_mass pandas series.
ref_df = pd.read_csv(ref_file) reference = np.zeros(shape=(1201,len(ref_df))) ref_mass = ref_df["mass"] i, mass in enumerate(ref_mass): print ref_mass[i].astype(int) - 300, # print (x,y) reference[(ref_mass[i].astype(int) - 300),i] = 100
every (x,y) printed out correctly. however, there no value in plot of (x,y). what's wrong here? checked reference matrix, has 100 in every column rightly.
the (x,y):
547 0 265 1 124 2 39 3 509 4 # shown 240 5 # shown 105 6 24 7 355 8 137 9 28 10 # shown 394 11 163 12 48 13 347 14 132 15 # shown 24 16
plot code:
if __name__ == '__main__': mpl_toolkits.mplot3d import axes3d import matplotlib.pyplot plt import matplotlib matplotlib.matplotlib_fname() plt.ylabel('m/z') plt.xlabel('peptides') plt.imshow(reference, aspect='auto', cmap='terrain') plt.colorbar() plt.tight_layout() plt.show()
every pixel in final image represents 3 or more data points. renderer has decide color out of 2 times blue, 1 time white map pixel. statistically, blue twice white, such 66% of data points not shown.
the number of 3 pixels comes rough calculation: image has 480 pixels (which can either find out in picture program or calculating figuresize*dpi). have 1200 datapoints (seen axes). have margin of ~10% @ both ends; have 1200/(0.8*480) = 3.1 datapoints per pixel in final image.
imshow interpolation
you can use interpolation on image make pixels appear, e.g.
plt.imshow(..., interpolation="sinc")
the result may not appealing visually.
change resolution
you can make sure final plot comprises 1 pixel per datapoint. i.e. 1200 pixels dpi of 100 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 change data, such 1 pixel becomes 3 pixels along y direction.
import matplotlib.pyplot plt import numpy np; np.random.seed(1) import scipy.ndimage.filters filters = np.zeros((1200, 16)) 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 plt import numpy np = np.zeros((1200, 16)) im = plt.imshow(a, aspect="auto", vmin=0, vmax=1) plt.colorbar(im) in range(16): plt.plot([i-.5, i+.5], [i*70+21,i*70+21], color=im.cmap(1.)) plt.show()
Comments
Post a Comment