Matplotlib不会在图中显示某些矩阵值 [英] Some values of matrix do not appear in the plot by Matplotlib

查看:312
本文介绍了Matplotlib不会在图中显示某些矩阵值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从CSV创建了一个空的参考矩阵,将(x,y)定位为矩阵上的一个位置(并打印出来),并在矩阵上的该位置指定了100.每个x都是ref_mass pandas系列中的值.

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

每个(x,y)均已正确打印.但是,在某些(x,y)的图中没有任何值.怎么了我检查了参考矩阵,正确地在每列中有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.

(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

剧情:

地块代码:

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

推荐答案

最终图像中的每个像素代表3个或更多数据点.然后,渲染器必须确定2倍于蓝色,1倍于白色的颜色才能映射到该像素.从统计上讲,它是蓝色的两倍,是白色的两倍,因此没有显示66%的数据点.

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.

3个像素的数量来自一个粗略的计算:您的图像有480个像素(您可以在图片程序中找到它,也可以通过计算数字大小* dpi).您有1200个数据点(从轴上看到).您两端的保证金约为10%;因此您在最终图像中每像素大约有1200/(0.8 * 480)= 3.1个数据点.

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.

您还可以确保您的最终绘图每个数据点正好包含一个像素. IE.可以实现1200 dpi(dpi为100)

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

过滤数据

另一种选择是更改数据,以使一个像素沿y方向变为三个像素.

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

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

这篇关于Matplotlib不会在图中显示某些矩阵值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆