Python 如何将数据值转换为 matplotlib 的颜色信息?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12857925/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 12:01:32  来源:igfitidea点击:

How to convert data values into color information for matplotlib?

pythoncolorsnumpymatplotlibscipy

提问by HyperCube

I am using the class matplotlib.patches.Polygon to draw polygons on a map. In fact, the information about the coordinates of the corners of the polygons and a floating point data value for each "polygon" are given. Now I'd like to convert these data values (ranging from 0 to 3e15) into color information to visualize it nicely. What is the best practice for doing that in Python?

我正在使用类 matplotlib.patches.Polygon 在地图上绘制多边形。事实上,给出了关于多边形角坐标的信息和每个“多边形”的浮点数据值。现在我想将这些数据值(范围从 0 到 3e15)转换为颜色信息以很好地可视化。在 Python 中这样做的最佳实践是什么?

A snippet of my code:

我的代码片段:

poly = Polygon( xy, facecolor=data, edgecolor='none')
plt.gca().add_patch(poly)

采纳答案by Mr. Squig

In the RGB color system two bits of data are used for each color, red, green, and blue. That means that each color runs on a scale from 0 to 255. Black would be 00,00,00, while white would be 255,255,255. Matplotlib has lots of pre-defined colormaps for you to use. They are all normalized to 255, so they run from 0 to 1. So you need only normalize your data, then you can manually select colors from a color map as follows:

在 RGB 颜色系统中,每种颜色使用两位数据,红色、绿色和蓝色。这意味着每种颜色在 0 到 255 的范围内运行。黑色将是 00,00,00,而白色将是 255,255,255。Matplotlib 有许多预定义的颜色图供您使用。它们都归一化为 255,所以它们从 0 到 1 运行。所以你只需要归一化你的数据,然后你可以从颜色图中手动选择颜色,如下所示:

>>> import matplotlib.pyplot as plt

>>> Blues = plt.get_cmap('Blues')
>>> print Blues(0)
(0.9686274528503418, 0.9843137264251709, 1.0, 1.0)
>>> print Blues(0.5)
(0.41708574119736169, 0.68063054575639614, 0.83823145908467911, 1.0)
>>> print Blues(1.0)
(0.96555171293370867, 0.9823452528785257, 0.9990157632266774, 1.0)

Here are all of the predefined colormaps.If you're interested in creating your own color maps, hereis a good example. Also hereis a paper from IBM on choosing colors carefully, something you should be considering if you're using color to visualize data.

这是所有预定义的颜色图。如果您对创建自己的颜色图感兴趣,这里有一个很好的例子。另外这里是IBM在精心选择的颜色,你应该如果你使用彩色可视化的数据来考虑的论文。

回答by juniper-

I think what you are looking for is a ColorMap. See the following post for information about using those provided by matplotlib.

我认为您正在寻找的是 ColorMap。有关使用 matplotlib 提供的那些的信息,请参阅以下帖子。

Using Colormaps to set color of line in matplotlib

使用颜色图在 matplotlib 中设置线条颜色

If you want to make your own, the page below provides a nice example.

如果你想自己制作,下面的页面提供了一个很好的例子。

http://code.activestate.com/recipes/52273-colormap-returns-an-rgb-tuple-on-a-0-to-255-scale-/

http://code.activestate.com/recipes/52273-colormap-returns-an-rgb-tuple-on-a-0-to-255-scale-/

回答by Davoud Taghawi-Nejad

To convert a value from that goes from 0 to + infinity to a value that goes from 0.0 to 1.0

将一个从 0 到 + 无穷大的值转换为从 0.0 到 1.0 的值

import matplotlib.pyplot as plt

price_change =  price_change ** 2 / (1 + price_change ** 2)
Blues = plt.get_cmap('Blues')
print Blues(price_change)

A full list of colormaps is here: http://matplotlib.org/users/colormaps.html

颜色图的完整列表在这里:http: //matplotlib.org/users/colormaps.html