Python - 基于值绘制彩色网格
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43971138/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python - Plotting colored grid based on values
提问by philippos
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.
我有一个数组,例如 100 个值。值在 0 到 100 的范围内。我想将此数组绘制为网格,根据数组中的值填充正方形。
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
自定义 matplotlib 图:象棋盘一样带有彩色单元格的表格
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.
但是,我想知道的是,我是否可以设置特定值和颜色的范围。例如,如果值在 10 到 20 之间,则让网格方块的颜色为红色。否则,如果值在 20 到 30 之间,则让颜色为蓝色。等等。
How this could be achieved in python?
这如何在python中实现?
回答by umutto
You can create a ListedColormapfor your custom colors and color BoundaryNormsto threshold the values.
您可以为您的自定义颜色和颜色BoundaryNorms创建一个ListedColormap来设置这些值的阈值。
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()
For more, you can check this matplotlib example.
有关更多信息,您可以查看此matplotlib 示例。
回答by Jordan Gleeson
It depends on what units you need your colours to be in, but just a simple if statement should do the trick.
这取决于您需要颜色的单位,但只需一个简单的 if 语句即可解决问题。
def find_colour(_val):
# Colour value constants
_colours = {"blue": [0.0, 0.0, 1.0],
"green": [0.0, 1.0, 0.00],
"yellow": [1.0, 1.0, 0.0],
"red": [1.0, 0.0, 0.0]}
# Map the value to a colour
_colour = [0, 0, 0]
if _val > 30:
_colour = _colours["red"]
elif _val > 20:
_colour = _colours["blue"]
elif _val > 10:
_colour = _colours["green"]
elif _val > 0:
_colour = _colours["yellow"]
return tuple(_colour)
And just convert that tuple to whatever units you need e.g. RGBA(..). You can then implement the methods it looks like you have already found to achieve the grid.
只需将该元组转换为您需要的任何单位,例如 RGBA(..)。然后,您可以实现看起来已经找到的方法来实现网格。