使用matplotlib创建自己的颜色图并绘制颜色比例 [英] Create own colormap using matplotlib and plot color scale
问题描述
我遇到以下问题,我想创建自己的颜色图(red-mix-violet-mix-blue),将其映射到-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-loop
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屋!