Python 中的散点图和颜色映射 [英] Scatter plot and Color mapping in Python

查看:94
本文介绍了Python 中的散点图和颜色映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我正在使用

绘制散点图

将 matplotlib.pyplot 导入为 pltplt.scatter(x,y)plt.show()

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

最简单的方法是什么?

解决方案

这是一个例子

将 numpy 导入为 np导入 matplotlib.pyplot 作为 pltx = np.random.rand(100)y = np.random.rand(100)t = np.arange(100)plt.scatter(x, y, c=t)plt.show()

这里是根据索引设置颜色,t,它只是一个 [1, 2, ..., 100] 的数组.

也许更容易理解的例子稍微简单一些

将 numpy 导入为 np导入 matplotlib.pyplot 作为 pltx = np.arange(100)y = xt = xplt.scatter(x, y, c=t)plt.show()

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

颜色图

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

将 matplotlib.cm 导入为 cmplt.scatter(x, y, c=t, cmap=cm.cmap_name)

导入 matplotlib.cm 是可选的,因为您也可以将颜色图称为 cmap="cmap_name".有一个颜色图的

颜色条

您可以使用添加颜色条

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

请注意,如果您明确使用图形和子图(例如 fig, ax = plt.subplots()ax = fig.add_subplot(111)),添加颜色条可能会涉及更多.好的例子可以在这里找到一个子图颜色条此处为 2 个子图 1 个颜色条.

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

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)

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].

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, 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 c correspond to the bottom/top of the colormap.

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. 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. 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. Here's an example with the new 1.5 colormap 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 (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 colorbar and here for 2 subplots 1 colorbar.

这篇关于Python 中的散点图和颜色映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆