CDF、matplotlib - 绘图、python 的颜色不足 [英] CDF, matplotlib - not enough colors for plot, python

查看:64
本文介绍了CDF、matplotlib - 绘图、python 的颜色不足的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Here is needed to plot CDF for 8 different functions in one plot. The problem that it gives just 7 different colors and the 8 one gives just first blue color again. How to make 8 different colors?

脚本如下:

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()

谢谢

推荐答案

我喜欢使用此代码直接从颜色图中读取我的颜色

I love to read my colors directly from a colormap with this code

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 是颜色图的名称(参见 https://matplotlib.org/examples/color/colormaps_reference.html 为列表),N 是你想要的颜色总数,idx只是一个会产生特定颜色的索引.

Here, c is the name of the colormap (see https://matplotlib.org/examples/color/colormaps_reference.html for a list), N is the number of colors you want in total, and idx is just an index that will yield the specific color.

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

Then when calling the plot function, just add the color=getColor(c, N, idx) option.

这篇关于CDF、matplotlib - 绘图、python 的颜色不足的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆