Python 中的散点图和颜色映射

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

Scatter plot and Color mapping in Python

pythonmatplotlib

提问by Vincent

I have a range of points x and y stored in numpy arrays. Those represent x(t) and y(t) where t=0...T-1

我有一系列点 x 和 y 存储在 numpy 数组中。那些代表 x(t) 和 y(t) 其中 t=0...T-1

I am plotting a scatter plot using

我正在绘制散点图

import matplotlib.pyplot as plt

plt.scatter(x,y)
plt.show()

I would like to have a colormap representing the time (therefore coloring the points depending on the index in the numpy arrays)

我想要一个表示时间的颜色图(因此根据 numpy 数组中的索引为点着色)

What is the easiest way to do so?

最简单的方法是什么?

采纳答案by wflynny

Here is an example

这是一个例子

import numpy as np
import matplotlib.pyplot as plt

x = np.random.rand(100)
y = np.random.rand(100)
t = np.arange(100)

plt.scatter(x, y, c=t)
plt.show()

Here you are setting the color based on the index, t, which is just an array of [1, 2, ..., 100]. enter image description here

在这里,您将根据索引设置颜色,t,它只是[1, 2, ..., 100]. 在此处输入图片说明

Perhaps an easier-to-understand example is the slightly simpler

也许一个更容易理解的例子是稍微简单一点的

import numpy as np
import matplotlib.pyplot as plt

x = np.arange(100)
y = x
t = x
plt.scatter(x, y, c=t)
plt.show()

enter image description here

在此处输入图片说明

Note that the array you pass as cdoesn't need to have any particular order or type, i.e. it doesn't need to be sorted or integers as in these examples. The plotting routine will scale the colormap such that the minimum/maximum values in ccorrespond to the bottom/top of the colormap.

请注意,您传递的数组c不需要具有任何特定的顺序或类型,即不需要像这些示例中那样进行排序或整数。绘图例程将缩放颜色图,使得最小值/最大值c对应于颜色图的底部/顶部。

Colormaps

颜色图

You can change the colormap by adding

您可以通过添加更改颜色图

import matplotlib.cm as cm
plt.scatter(x, y, c=t, cmap=cm.cmap_name)

Importing matplotlib.cmis optional as you can call colormaps as cmap="cmap_name"just as well. There is a reference pageof colormaps showing what each looks like. Also know that you can reverse a colormap by simply calling it as cmap_name_r. So either

导入matplotlib.cm是可选的,因为您也可以调用颜色图cmap="cmap_name"。有一个颜色图的参考页面,显示了每个颜色图的样子。还知道您可以通过简单地将颜色图调用为cmap_name_r. 所以要么

plt.scatter(x, y, c=t, cmap=cm.cmap_name_r)
# or
plt.scatter(x, y, c=t, cmap="cmap_name_r")

will work. Examples are "jet_r"or cm.plasma_r. Here's an example with the new 1.5 colormap viridis:

将工作。例子是"jet_r"cm.plasma_r。这是新的 1.5 颜色图 viridis 的示例:

import numpy as np
import matplotlib.pyplot as plt

x = np.arange(100)
y = x
t = x
fig, (ax1, ax2) = plt.subplots(1, 2)
ax1.scatter(x, y, c=t, cmap='viridis')
ax2.scatter(x, y, c=t, cmap='viridis_r')
plt.show()

enter image description here

在此处输入图片说明

Colorbars

颜色条

You can add a colorbar by using

您可以使用添加颜色条

plt.scatter(x, y, c=t, cmap='viridis')
plt.colorbar()
plt.show()

enter image description here

在此处输入图片说明

Note that if you are using figures and subplots explicitly (e.g. fig, ax = plt.subplots()or ax = fig.add_subplot(111)), adding a colorbar can be a bit more involved. Good examples can be found here for a single subplot colorbarand here for 2 subplots 1 colorbar.

请注意,如果您明确使用图形和子图(例如fig, ax = plt.subplots()ax = fig.add_subplot(111)),则添加颜色条可能会更复杂一些。可以在此处找到单个子图 colorbar2 个子图 1 colorbar 的好示例。

回答by Nathan

To add to wflynny's answer above, you can find the available colormaps here

要添加到上面 wflynny 的答案,您可以在此处找到可用的颜色图

Example:

例子:

import matplotlib.cm as cm
plt.scatter(x, y, c=t, cmap=cm.jet)

or alternatively,

或者,

plt.scatter(x, y, c=t, cmap='jet')

回答by Audelia

Subplot Colorbar

子图颜色条

For subplots with scatter, you can trick a colorbar onto your axes by building the "mappable" with the help of a secondary figure and then adding it to your original plot.

对于散点图,您可以在辅助图形的帮助下构建“可映射”,然后将其添加到原始图中,从而将颜色条欺骗到轴上。

As a continuation of the above example:

作为上面例子的延续:

import numpy as np
import matplotlib.pyplot as plt

x = np.arange(10)
y = x
t = x
fig, (ax1, ax2) = plt.subplots(1, 2)
ax1.scatter(x, y, c=t, cmap='viridis')
ax2.scatter(x, y, c=t, cmap='viridis_r')


# Build your secondary mirror axes:
fig2, (ax3, ax4) = plt.subplots(1, 2)

# Build maps that parallel the color-coded data
# NOTE 1: imshow requires a 2-D array as input
# NOTE 2: You must use the same cmap tag as above for it match
map1 = ax3.imshow(np.stack([t, t]),cmap='viridis')
map2 = ax4.imshow(np.stack([t, t]),cmap='viridis_r')

# Add your maps onto your original figure/axes
fig.colorbar(map1, ax=ax1)
fig.colorbar(map2, ax=ax2)
plt.show()

Scatter subplots with COLORBAR

使用 COLORBAR 散布子图

Note that you will also output a secondary figure that you can ignore.

请注意,您还将输出一个可以忽略的辅助数字。