Reputation: 1479
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 value in the ref_mass pandas Series.
ref_df = pd.read_csv(ref_file)
reference = np.zeros(shape=(1201,len(ref_df)))
ref_mass = ref_df["mass"]
for i, mass in enumerate(ref_mass):
print ref_mass[i].astype(int) - 300, i # print (x,y)
reference[(ref_mass[i].astype(int) - 300),i] = 100
Every (x,y) was printed out correctly. However, there is no value in the plot of some (x,y). What's wrong here? I checked the reference matrix, it 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__':
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as 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()
Upvotes: 2
Views: 136
Reputation: 339220
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.
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.
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(...)
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()
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()
Upvotes: 2