<tfoot id='Dmri5'></tfoot>

<small id='Dmri5'></small><noframes id='Dmri5'>

  • <i id='Dmri5'><tr id='Dmri5'><dt id='Dmri5'><q id='Dmri5'><span id='Dmri5'><b id='Dmri5'><form id='Dmri5'><ins id='Dmri5'></ins><ul id='Dmri5'></ul><sub id='Dmri5'></sub></form><legend id='Dmri5'></legend><bdo id='Dmri5'><pre id='Dmri5'><center id='Dmri5'></center></pre></bdo></b><th id='Dmri5'></th></span></q></dt></tr></i><div id='Dmri5'><tfoot id='Dmri5'></tfoot><dl id='Dmri5'><fieldset id='Dmri5'></fieldset></dl></div>
    <legend id='Dmri5'><style id='Dmri5'><dir id='Dmri5'><q id='Dmri5'></q></dir></style></legend>
        <bdo id='Dmri5'></bdo><ul id='Dmri5'></ul>

        Python 中的 2D 网格数据可视化

        时间:2023-07-24

          <i id='4bgiR'><tr id='4bgiR'><dt id='4bgiR'><q id='4bgiR'><span id='4bgiR'><b id='4bgiR'><form id='4bgiR'><ins id='4bgiR'></ins><ul id='4bgiR'></ul><sub id='4bgiR'></sub></form><legend id='4bgiR'></legend><bdo id='4bgiR'><pre id='4bgiR'><center id='4bgiR'></center></pre></bdo></b><th id='4bgiR'></th></span></q></dt></tr></i><div id='4bgiR'><tfoot id='4bgiR'></tfoot><dl id='4bgiR'><fieldset id='4bgiR'></fieldset></dl></div>

          <tfoot id='4bgiR'></tfoot>

          • <small id='4bgiR'></small><noframes id='4bgiR'>

              <tbody id='4bgiR'></tbody>
            <legend id='4bgiR'><style id='4bgiR'><dir id='4bgiR'><q id='4bgiR'></q></dir></style></legend>
            • <bdo id='4bgiR'></bdo><ul id='4bgiR'></ul>

                  本文介绍了Python 中的 2D 网格数据可视化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我需要可视化一些数据.这是基本的 2D 网格,其中每个单元格都有浮点值.我知道如何在 OpenCV 中为值分配颜色并绘制网格.但这里的重点是有这么多的值,所以几乎不可能做到这一点.我正在寻找一些可以使用渐变的方法.例如,值 -5.0 将表示为蓝色,0 - 黑色,+5.0 表示为红色.有没有办法在 Python 中做到这一点?

                  这是我正在谈论的示例数据

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

                  解决方案

                  它解释了 boundaries 的数量需要比颜色的数量大一.

                  编辑

                  您应该所示,请使用 <代码>网格方法.将网格颜色设置为白色"适用于颜色图使用的颜色(即默认的黑色不能很好地显示).

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

                  savefig 调用之前包含此图会生成此图(为清楚起见,使用 11x11 网格制作):grid 有很多选项,在 matplotlib 文档.您可能感兴趣的是 linewidth.

                  I need to visualize some data. It's basic 2D grid, where each cell have float value. I know how to e.g. assign color to value and paint grid in 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. Is there any way to do that in 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:

                  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. It explains that the number of boundaries need to be one larger then then number of colors.

                  EDIT

                  You should note, that imshow accepts the origin keyword, which sets the where the first point is assigned. 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. The alternative is to set origin="lower", so that first point is plotted in the lower left corner.

                  EDIT 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

                  To add a grid, as shown in this example, use the grid method. 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): There are many options for grid, which are described in the matplotlib documentation. One you might be interested in is linewidth.

                  这篇关于Python 中的 2D 网格数据可视化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:忽略 Python 中的 SonarQube 警告 下一篇:如何在 tkinter 中创建一个自调整大小的按钮网格?

                  相关文章

                1. <small id='GpKLe'></small><noframes id='GpKLe'>

                      <legend id='GpKLe'><style id='GpKLe'><dir id='GpKLe'><q id='GpKLe'></q></dir></style></legend>
                      <i id='GpKLe'><tr id='GpKLe'><dt id='GpKLe'><q id='GpKLe'><span id='GpKLe'><b id='GpKLe'><form id='GpKLe'><ins id='GpKLe'></ins><ul id='GpKLe'></ul><sub id='GpKLe'></sub></form><legend id='GpKLe'></legend><bdo id='GpKLe'><pre id='GpKLe'><center id='GpKLe'></center></pre></bdo></b><th id='GpKLe'></th></span></q></dt></tr></i><div id='GpKLe'><tfoot id='GpKLe'></tfoot><dl id='GpKLe'><fieldset id='GpKLe'></fieldset></dl></div>
                    1. <tfoot id='GpKLe'></tfoot>
                        <bdo id='GpKLe'></bdo><ul id='GpKLe'></ul>