简体   繁体   English

Python中的二维网格数据可视化

[英]2D grid data visualization in Python

I need to visualize some data.我需要可视化一些数据。 It's basic 2D grid, where each cell have float value.它是基本的 2D 网格,其中每个单元格都有浮点值。 I know how to eg assign color to value and paint grid in OpenCV.我知道如何在 OpenCV 中为值分配颜色并绘制网格。 But the point here is that there are so many values so it's nearly impossible to do that.但这里的重点是有太多的值,所以几乎不可能做到这一点。 I am looking for some method, where I could use gradient.我正在寻找一些可以使用渐变的方法。 For example value -5.0 will be represented by blue, 0 - black, and +5.0 as red.例如,值 -5.0 将表示为蓝色,0 - 黑色,+5.0 表示为红色。 Is there any way to do that in Python?有没有办法在 Python 中做到这一点?

Here is sample data I am talking about这是我正在谈论的示例数据

        A       B       C        D
A    -1.045    2.0     3.5    -4.890
B    -5.678    3.2     2.89    5.78

Matplotlib has the imshow method for plotting arrays: Matplotlib具有用于绘制数组的imshow方法:

import matplotlib as mpl
from matplotlib import pyplot
import numpy as np

# make values from -5 to 5, for this example
zvals = np.random.rand(100,100)*10-5

# make a color map of fixed colors
cmap = mpl.colors.ListedColormap(['blue','black','red'])
bounds=[-6,-2,2,6]
norm = mpl.colors.BoundaryNorm(bounds, cmap.N)

# tell imshow about color map so that only set colors are used
img = pyplot.imshow(zvals,interpolation='nearest',
                    cmap = cmap,norm=norm)

# make a color bar
pyplot.colorbar(img,cmap=cmap,
                norm=norm,boundaries=bounds,ticks=[-5,0,5])

pyplot.show()

This is what it looks like:这是它的样子:

在此处输入图片说明

The details for the color bar setup were taken from a matplotlib example: colorbar_only.py.颜色条设置的详细信息取自 matplotlib 示例: colorbar_only.py。 It explains that the number of boundaries need to be one larger then then number of colors.它解释了boundaries的数量需要比颜色的数量大一。

EDIT编辑

You should note , that imshow accepts the origin keyword, which sets the where the first point is assigned.您应该注意imshow接受origin关键字,它设置了第一个点的分配位置。 The default is 'upper left', which is why in my posted plot the y axis has 0 in the upper left and 99 (not shown) in the lower left.默认值为“左上角”,这就是为什么在我发布的图中,y 轴的左上角为 0,左下角为 99(未显示)。 The alternative is to set origin="lower" , so that first point is plotted in the lower left corner.另一种方法是设置origin="lower" ,以便在左下角绘制第一个点。

EDIT 2编辑 2

If you want a gradient and not a discrete color map, make a color map by linearly interpolating through a series of colors:如果您想要渐变而不是离散颜色图,请通过一系列颜色进行线性插值来制作颜色图:

fig = pyplot.figure(2)

cmap2 = mpl.colors.LinearSegmentedColormap.from_list('my_colormap',
                                           ['blue','black','red'],
                                           256)

img2 = pyplot.imshow(zvals,interpolation='nearest',
                    cmap = cmap2,
                    origin='lower')

pyplot.colorbar(img2,cmap=cmap2)

fig.savefig("image2.png")

This produces:这产生:在此处输入图片说明

EDIT 3编辑 3

To add a grid, as shown in this example , use the grid method.要添加网格,如本示例所示,请使用grid方法。 Setting the grid color to 'white' works well with the colors used by the colormap (ie the default black does not show up well).将网格颜色设置为“白色”可以很好地与颜色图使用的颜色配合使用(即默认黑色显示效果不佳)。

pyplot.grid(True,color='white')

Including this before the savefig call produces this plot (made using 11x11 grid for clarity):savefig调用生成此图之前包含此内容(为清晰起见,使用 11x11 网格制作):在此处输入图片说明 There are many options for grid , which are described in the matplotlib documentation . grid有很多选项,在 matplotlib 文档中有描述。 One you might be interested in is linewidth .您可能感兴趣的是linewidth

How about using matplotlib?使用 matplotlib 怎么样?

from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm
from matplotlib.ticker import LinearLocator, FixedLocator, FormatStrFormatter
import matplotlib.pyplot as plt
import numpy as np

fig = plt.figure()
ax = Axes3D(fig)

Z = np.array([[-1.045, 2.0, 3.5, -4.890],
              [-5.678, 3.2, 2.89, 5.78]])

X = np.zeros_like(Z)
X[1,:] = 1
Y = np.zeros_like(Z)
Y[:,1] = 1
Y[:,2] = 2
Y[:,3] = 3

surf = ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=cm.jet,
        linewidth=0, antialiased=False)
ax.set_zlim3d(-10.0, 10.0)

ax.w_zaxis.set_major_locator(LinearLocator(10))
ax.w_zaxis.set_major_formatter(FormatStrFormatter('%.03f'))

m = cm.ScalarMappable(cmap=cm.jet)
m.set_array(Z)
fig.colorbar(m)

plt.show()

This shows:由此可见:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM