绘制一条曲线,并根据每个点的值和定制的颜色图对其进行着色

2024-10-03 02:42:45 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个定制的ROYGBIV彩虹颜色图:

import numpy as np
import matplotlib.pyplot as plt
# dictionary with RGB values
ROYGBIV = {
'blue': ((0.0, 1.0, 1.0),
(0.167, 1.0, 1.0),
(0.333, 1.0, 1.0),
(0.5, 0.0, 0.0),
(0.667, 0.0, 0.0),
(0.833, 0.0, 0.0),
(1.0, 0.0, 0.0)),
'green': ((0.0, 0.0, 0.0),
(0.167, 0.0, 0.0),
(0.333, 0.0, 0.0),
(0.5, 1.0, 1.0),
(0.667, 1.0, 1.0),
(0.833, 0.498, 0.498),
(1.0, 0.0, 0.0)),
'red': ((0.0, 0.5608, 0.5608),
(0.167, 0.4353, 0.4353),
(0.333, 0.0, 0.0),
(0.5, 0.0, 0.0),
(0.667, 1.0, 1.0),
(0.833, 1.0, 1.0),
(1.0, 1.0, 1.0))}
### creates colormap using Matplotlib
mycmap = matplotlib.colors.LinearSegmentedColormap('my_colormap',ROYGBIV,256)

我用这个代码测试了它:

^{pr2}$

L具有颜色贴图的亮度值

L=
[42.853117895072565,
38.896632760964955,
32.299375201436156,
87.73500278716472,
97.13881604341229,
66.66212771669841,
53.23896002513146]

我要做的是绘制L,然后根据其值对其进行着色,并使用mycmap作为颜色贴图。但现在麻烦来了。过去,我成功地将这个伟大的彩色线函数http://nbviewer.ipython.org/github/dpsanders/matplotlib-examples/blob/master/colorline.ipynb用于类似于笔记本中的绘图,但在本例中,使用我的自定义颜色映射,它失败了:

# Interpolating L to add points to make 256_
x = np.arange(7)
from scipy.interpolate import interp1d
import scipy as sp
new_x = np.linspace(x.min(), x.max(), 256)
new_L = sp.interpolate.interp1d(x, L)(new_x)

colorline(new_x,new_L,linewidth=6,cmap="mycmap")
plt.xlim(X.min(), X.max())
plt.ylim(0, 100)
plt.show()

我以为它能工作,但是我得到了一堆错误消息:

ValueError                                Traceback (most recent call last)
<ipython-input-48-ff20dd713b90> in <module>()
----> 4 colorline(new_x,new_L,linewidth=6,cmap="my_cmap")
  5 colorline(X,L,linewidth=6)
  6 plt.xlim(X.min(), X.max())

<ipython-input-3-58e291da3158> in colorline(x, y, z, cmap, norm, linewidth, alpha)
 39 
 40     segments = make_segments(x, y)
---> 41     lc = LineCollection(segments, array=z, cmap=cmap, norm=norm, linewidth=linewidth, alpha=alpha)
 42 
 43     ax = plt.gca()

/Users/.../python2.7/site-packages/matplotlib/collections.pyc in __init__(self, segments,     
linewidths, colors, antialiaseds, linestyles, offsets, transOffset, norm, cmap, 
pickradius, zorder, **kwargs)
1012             pickradius=pickradius,
1013             zorder=zorder,
-> 1014             **kwargs)
1015 
1016         self.set_segments(segments)

/Users/.../python2.7/site-packages/matplotlib/collections.pyc in __init__(self, 
edgecolors, facecolors, linewidths, linestyles, antialiaseds, offsets, transOffset, norm, 
cmap, pickradius, hatch, urls, offset_position, zorder, **kwargs)
101         """
102         artist.Artist.__init__(self)
--> 103         cm.ScalarMappable.__init__(self, norm, cmap)
104 
105         self.set_edgecolor(edgecolors)

/Users/.../python2.7/site-packages/matplotlib/cm.pyc in __init__(self, norm, cmap)
193         self.norm = norm
194         #: The Colormap instance of this ScalarMappable.
--> 195         self.cmap = get_cmap(cmap)
196         #: The last colorbar associated with this ScalarMappable. May be None.
197         self.colorbar = None

/Users/.../python2.7/site-packages/matplotlib/cm.pyc in get_cmap(name, lut)
159             return _generate_cmap(name, lut)
160 
--> 161     raise ValueError("Colormap %s is not recognized" % name)
162 
163 

ValueError: Colormap my_cmap is not recognized

我不理解这些错误消息。我感到困惑的是,这些语句似乎不是指向Colorline而是指向MatplotLib,但是我使用MatplotLib创建了colormap。我也尝试过这个方法来创建colormap,但是最后我得到了类似的错误消息:

my_cmap2 = matplotlib.colors.ListedColormap(rgb, name='roygbiv1')

我的问题分为两个部分:1)有人能建议我的代码发生了什么,或者建议对我的代码进行一些更改吗?2) 如果没有,是否有其他方法根据L的值并使用自定义colormap对其进行着色?谢谢。


Tags: inimportselfnormnewmatplotlibinit颜色
2条回答

Matteo,如果您将第五个代码块的第一行替换为以下代码块,它应该可以工作:

z =  (new_x - np.amin(new_x) )/ new_x.ptp()
colorline(new_x,new_L, z, cmap=mycmap, linewidth=10)

除了@tcaswell的响应之外,这样做可以得到您所需要的。在

您的问题是您创建了颜色映射,但没有注册(doc

mycmap = matplotlib.colors.LinearSegmentedColormap('my_colormap',ROYGBIV,256)
from matplotlib.cm import register_cmap
register_cmap(cmap=mycmap)

colormap机制的工作方式如果您传递一个colormap对象,那么它会很高兴;如果您传递一个字符串,它会使用get_cmap来查找colormap对象。如果您没有注册它,那么当您(通过colorline)要求它给您一个它不知道的颜色映射时,matplotlib会正确地引发一个错误。在

如果您在您发布的链接中查看colorline的代码,您将看到它是一个围绕LineCollection非常薄的包装器。在

相关问题 更多 >