简体   繁体   English

Python 中的散点图和颜色映射

[英]Scatter plot and Color mapping in Python

I have a range of points x and y stored in numpy arrays.我有一系列点 x 和 y 存储在 numpy 数组中。 Those represent x(t) and y(t) where t=0...T-1那些代表 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?最简单的方法是什么?

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] .在这里,您将根据索引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()

在此处输入图片说明

Note that the array you pass as c doesn't need to have any particular order or type, ie it doesn't need to be sorted or integers as in these examples.请注意,您作为c传递的数组不需要具有任何特定的顺序或类型,即不需要像这些示例中那样排序或整数。 The plotting routine will scale the colormap such that the minimum/maximum values in c correspond to the bottom/top of the colormap.绘图例程将缩放颜色图,以便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.cm is optional as you can call colormaps as cmap="cmap_name" just as well.导入matplotlib.cm是可选的,因为您也可以将颜色图称为cmap="cmap_name" There is a reference page of colormaps showing what each looks like.有一个颜色图的 参考页面,显示了每个颜色图的样子。 Also know that you can reverse a colormap by simply calling it as cmap_name_r .还知道您可以通过简单地将其称为cmap_name_r来反转颜色cmap_name_r So either所以要么

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 .示例是"jet_r"cm.plasma_r Here's an example with the new 1.5 colormap viridis:这是新的 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()

在此处输入图片说明

Colorbars颜色条

You can add a colorbar by using您可以使用添加颜色条

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

在此处输入图片说明

Note that if you are using figures and subplots explicitly (eg fig, ax = plt.subplots() or ax = fig.add_subplot(111) ), adding a colorbar can be a bit more involved.请注意,如果您明确使用图形和子图(例如fig, ax = plt.subplots()ax = fig.add_subplot(111) ),添加ax = fig.add_subplot(111)可能会更复杂一些。 Good examples can be found here for a single subplot colorbar and here for 2 subplots 1 colorbar .可以在此处找到单个子图 colorbar2 个子图 1 colorbar 的好示例。

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')

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()

使用 COLORBAR 散布子图

Note that you will also output a secondary figure that you can ignore.请注意,您还将输出一个可以忽略的辅助数字。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM