首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CDF,matplotlib -没有足够的颜色来绘制情节,python

CDF,matplotlib -没有足够的颜色来绘制情节,python
EN

Stack Overflow用户
提问于 2017-08-10 11:12:34
回答 3查看 2K关注 0票数 1

这里需要在一幅图中绘制8种不同功能的CDF。问题是,它只给出了7种不同的颜色,而8种颜色又给出了第一种蓝色。如何制作8种不同的颜色?

下面是脚本:

代码语言:javascript
复制
locerror_2d=[Scan_Around[1],Triangle_Around[1],M_shape_Around[1],Hilbert_Around[1],Scan_SbS[1],Triangle_SbS[1],M_shape_SbS[1],Hilbert_SbS[1]]


# N = len(locerror_2d[0]) #same for all ( here, I hope so... )
# N1=len(locerror_2d[2])
H_cent,h_cent1 = np.histogram( locerror_2d[0], bins = 10, normed = True ) # Random Walk Centroid
hy_cent = np.cumsum(H_cent)*(h_cent1[1] - h_cent1[0])

H_1st,h_1st = np.histogram( locerror_2d[1], bins = 10, normed = True ) # Random Walk Weighterd
hy_1st = np.cumsum(H_1st)*(h_1st[1] - h_1st[0])

H_2nd,h_2nd = np.histogram( locerror_2d[2], bins = 10, normed = True ) # Circle Walk Centroid
hy_2nd = np.cumsum(H_2nd)*(h_2nd[1] - h_2nd[0])

H_3rd,h_3rd = np.histogram( locerror_2d[3], bins = 10, normed = True ) # Circle Walk Weighterd
hy_3rd = np.cumsum(H_3rd)*(h_3rd[1] - h_3rd[0])

H_mm,h_mm = np.histogram( locerror_2d[4], bins = 10, normed = True ) # G Walk Centroid
hy_mm = np.cumsum(H_mm)*(h_mm[1] - h_mm[0])

H_shr,h_shr = np.histogram( locerror_2d[5], bins = 10, normed = True ) # G Walk Weighterd
hy_shr = np.cumsum(H_shr)*(h_shr[1] - h_shr[0])

H_s,h_s = np.histogram( locerror_2d[6], bins = 10, normed = True ) # G Walk Weighterd
hy_s = np.cumsum(H_s)*(h_s[1] - h_s[0])

H_sh,h_sh = np.histogram( locerror_2d[7], bins = 10, normed = True ) # G Walk Weighterd
hy_sh = np.cumsum(H_sh)*(h_sh[1] - h_sh[0])


plt.hold(True)
ddd_hist_cent, = plt.plot(h_cent1[1:], hy_cent,label="Scan_Around")        # centroid
ddd_hist_1st, = plt.plot(h_1st[1:], hy_1st,label='Triangle_Around')       #Gradient
ddd_circ_cent, = plt.plot(h_2nd[1:], hy_cent,label="M_shape_around")        # centroid
ddd_circ_wei, = plt.plot(h_3rd[1:], hy_1st,label='Hilbert_Around')       #Gradient
ddd_g_cent, = plt.plot(h_mm[1:], hy_cent,label="Scan_SbS")        # centroid
ddd_g_wei, = plt.plot(h_shr[1:], hy_1st,label='Triangle_SbS')       #Gradient
ddd_g_w, = plt.plot(h_s[1:], hy_cent,label='M_shape_SbS')
ddd_g_we, = plt.plot(h_sh[1:], hy_1st,label='Hilbert_SbS')

plt.hold(False)

plt.rc('legend',**{'fontsize':10})
plt.legend(handles=[ddd_hist_cent, ddd_hist_1st, ddd_circ_cent, ddd_circ_wei, ddd_g_cent,ddd_g_wei, ddd_g_w],loc='center left', bbox_to_anchor=(0.75, 0.18)) #no trilateration here
plt.ylabel('Probability')
plt.xlabel('Localization Error, m')
plt.ylim(ymax = 1.1, ymin = 0)
plt.title('Path Planning Algorithms')
plt.grid()
plt.show()

谢谢

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2017-08-10 11:25:20

我喜欢用下面的代码直接从彩色地图上读取我的颜色。

代码语言:javascript
复制
def getColor(c, N, idx):
    import matplotlib as mpl
    cmap = mpl.cm.get_cmap(c)
    norm = mpl.colors.Normalize(vmin=0.0, vmax=N - 1)
    return cmap(norm(idx))

在这里,c是颜色映射的名称(列表见reference.html ),N是您想要的颜色总数,而idx只是一个将产生特定颜色的索引。

然后,在调用绘图函数时,只需添加color=getColor(c, N, idx)选项即可。

票数 5
EN

Stack Overflow用户

发布于 2017-08-10 11:29:03

好的。这样啊,原来是这么回事。在情节结束时,我只需要显示颜色。

代码语言:javascript
复制
ddd_hist_cent, = plt.plot(h_cent1[1:], hy_cent,label="Scan_Around", c='yellow') 
票数 1
EN

Stack Overflow用户

发布于 2017-08-10 11:34:51

最简单的解决方案:给最后一条曲线一个不同的颜色:

代码语言:javascript
复制
plt.plot(h_sh[1:], hy_1st,label='Hilbert_SbS', color="orange")

Matplotlib版本1.5或更低的版本在其颜色周期中有7种不同的颜色,matplotlib 2.0有10种不同的颜色。因此,更新matplotlib是另一个选项。

一般来说,你当然可以定义你自己的颜色周期,它有很多你想要的颜色。

  • 从彩色地图构建自行车,如this question所示: 从matplotlib.pyplot导入循环输入np =8#颜色数plt.rcParams"axes.prop_cycle“= cycler('color',plt.cm.jet(0,1,N))
  • 根据颜色列表生成自行车: 从骑车人进口自行车matplotlib.pyplot colors=“水红色”、“深红色”、“金”、“靛蓝”、“石灰”、“橙色”、“兰花”、“西耶娜”plt.rcParams"axes.prop_cycle“=自行车(‘颜色’,颜色)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45612129

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档