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)

in ()

----> 4 colorline(new_x,new_L,linewidth=6,cmap="my_cmap")

5 colorline(X,L,linewidth=6)

6 plt.xlim(X.min(), X.max())

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对其进行着色?谢谢。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值