使用 matplotlib 创建自己的颜色图并绘制色标 [英] Create own colormap using matplotlib and plot color scale
问题描述
我有以下问题,我想创建自己的颜色图(红-混合-紫-混合-蓝),映射到 -2 和 +2 之间的值,并想用它来为我的绘图中的点着色.然后该图应该具有右侧的色阶.
到目前为止,这就是我创建地图的方式.但我不确定它是否混合了颜色.
I have the following problem, I want to create my own colormap (red-mix-violet-mix-blue) that maps to values between -2 and +2 and want to use it to color points in my plot.
The plot should then have the colorscale to the right.
That is how I create the map so far. But I am not really sure if it mixes the colors.
cmap = matplotlib.colors.ListedColormap(["red","violet","blue"], name='from_list', N=None)
m = cm.ScalarMappable(norm=norm, cmap=cmap)
那样我将颜色映射到值.
That way I map the colors to the values.
colors = itertools.cycle([m.to_rgba(1.22), ..])
然后我绘制它:
Then I plot it:
for i in range(0, len(array_dg)):
plt.plot(array_dg[i], markers.next(),alpha=alpha[i], c=colors.next())
我的问题是:
1. 我无法绘制色标.
2. 我不完全确定我的色阶是否创造了连续(平滑)的色阶.
My problems are:
1. I can't plot the color scale.
2. I am not completely sure if my scale is creating a continues (smooth) colorscale.
推荐答案
有一个说明性示例 如何在此处创建自定义颜色图.文档字符串对于理解cdict
.一旦你掌握了它,你可以像这样使用 cdict
:
There is an illustrative example of how to create custom colormaps here.
The docstring is essential for understanding the meaning of
cdict
. Once you get that under your belt, you might use a cdict
like this:
cdict = {'red': ((0.0, 1.0, 1.0),
(0.1, 1.0, 1.0), # red
(0.4, 1.0, 1.0), # violet
(1.0, 0.0, 0.0)), # blue
'green': ((0.0, 0.0, 0.0),
(1.0, 0.0, 0.0)),
'blue': ((0.0, 0.0, 0.0),
(0.1, 0.0, 0.0), # red
(0.4, 1.0, 1.0), # violet
(1.0, 1.0, 0.0)) # blue
}
尽管 cdict
格式为您提供了很大的灵活性,但我发现对于简单的渐变 其格式相当不直观.这是一个实用函数来帮助生成简单的 LinearSegmentedColormaps:
Although the cdict
format gives you a lot of flexibility, I find for simple
gradients its format is rather unintuitive. Here is a utility function to help
generate simple LinearSegmentedColormaps:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.colors as mcolors
def make_colormap(seq):
"""Return a LinearSegmentedColormap
seq: a sequence of floats and RGB-tuples. The floats should be increasing
and in the interval (0,1).
"""
seq = [(None,) * 3, 0.0] + list(seq) + [1.0, (None,) * 3]
cdict = {'red': [], 'green': [], 'blue': []}
for i, item in enumerate(seq):
if isinstance(item, float):
r1, g1, b1 = seq[i - 1]
r2, g2, b2 = seq[i + 1]
cdict['red'].append([item, r1, r2])
cdict['green'].append([item, g1, g2])
cdict['blue'].append([item, b1, b2])
return mcolors.LinearSegmentedColormap('CustomMap', cdict)
c = mcolors.ColorConverter().to_rgb
rvb = make_colormap(
[c('red'), c('violet'), 0.33, c('violet'), c('blue'), 0.66, c('blue')])
N = 1000
array_dg = np.random.uniform(0, 10, size=(N, 2))
colors = np.random.uniform(-2, 2, size=(N,))
plt.scatter(array_dg[:, 0], array_dg[:, 1], c=colors, cmap=rvb)
plt.colorbar()
plt.show()
顺便说一下,for循环
for i in range(0, len(array_dg)):
plt.plot(array_dg[i], markers.next(),alpha=alpha[i], c=colors.next())
为每次调用 plt.plot
绘制一个点.这将适用于少数点,但对于许多点会变得非常慢.plt.plot
只能绘制一种颜色,但 plt.scatter
可以为每个点指定不同的颜色.因此,plt.scatter
是要走的路.
plots one point for every call to plt.plot
. This will work for a small number of points, but will become extremely slow for many points. plt.plot
can only draw in one color, but plt.scatter
can assign a different color to each dot. Thus, plt.scatter
is the way to go.
这篇关于使用 matplotlib 创建自己的颜色图并绘制色标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!