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

    <small id='8xUNV'></small><noframes id='8xUNV'>

        <bdo id='8xUNV'></bdo><ul id='8xUNV'></ul>

        <tfoot id='8xUNV'></tfoot>
      1. <legend id='8xUNV'><style id='8xUNV'><dir id='8xUNV'><q id='8xUNV'></q></dir></style></legend>

        Python - 根据值绘制彩色网格

        时间:2023-07-24

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

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

            • <legend id='WjXB5'><style id='WjXB5'><dir id='WjXB5'><q id='WjXB5'></q></dir></style></legend>

            • <tfoot id='WjXB5'></tfoot>

                1. 本文介绍了Python - 根据值绘制彩色网格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我一直在这里和网上搜索.我以某种方式找到了我想要的问题/答案,但仍然无法达到我正在寻找的东西.

                  我有一个数组,例如 100 个值.值在 0 到 100 的范围内.我想将此数组绘制为网格,根据数组中的值填充正方形.

                  目前我找到的解决方案如下:

                  有关更多信息,您可以查看此 matplotlib 示例.

                  I have been searching here and on the net. I found somehow close questions/answers to what I want, but still couldn't reach to what I'm looking for.

                  I have an array of for example, 100 values. The values are in the range from 0 to 100. I want to plot this array as a grid, filling the squares according to the values in the array.

                  The solutions I found so far are like the followings:

                  Drawing grid pattern in matplotlib

                  and

                  custom matplotlib plot : chess board like table with colored cells

                  In the examples I mentioned, the ranges of the colors vary and are not fixed.

                  However, what I am wondering about, is whether I can set the ranges for specific values and colors. For example, if the values are between 10 and 20, let the color of the grid square be red. else if the values are between 20 and 30, let the color be blue. etc.

                  How this could be achieved in python?

                  解决方案

                  You can create a ListedColormap for your custom colors and color BoundaryNorms to threshold the values.

                  import matplotlib.pyplot as plt
                  from matplotlib import colors
                  import numpy as np
                  
                  data = np.random.rand(10, 10) * 20
                  
                  # create discrete colormap
                  cmap = colors.ListedColormap(['red', 'blue'])
                  bounds = [0,10,20]
                  norm = colors.BoundaryNorm(bounds, cmap.N)
                  
                  fig, ax = plt.subplots()
                  ax.imshow(data, cmap=cmap, norm=norm)
                  
                  # draw gridlines
                  ax.grid(which='major', axis='both', linestyle='-', color='k', linewidth=2)
                  ax.set_xticks(np.arange(-.5, 10, 1));
                  ax.set_yticks(np.arange(-.5, 10, 1));
                  
                  plt.show()
                  

                  Resulting in;

                  For more, you can check this matplotlib example.

                  这篇关于Python - 根据值绘制彩色网格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:是否有 Tkinter/网格几何图形的 GUI 设计应用程序? 下一篇:远程执行任意 python 代码 - 可以吗?

                  相关文章

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

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

                      <legend id='cA9RQ'><style id='cA9RQ'><dir id='cA9RQ'><q id='cA9RQ'></q></dir></style></legend>