CDF, matplotlib - not enough colors for plot, python

帅比萌擦擦* 提交于 2019-12-02 03:51:54

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

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.

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

ok. I got it. In the end of plot I just need to show the color.

ddd_hist_cent, = plt.plot(h_cent1[1:], hy_cent,label="Scan_Around", c='yellow') 

Easiest solution: Give the last curve a different color:

plt.plot(h_sh[1:], hy_1st,label='Hilbert_SbS', color="orange")

Matplotlib version 1.5 or below has 7 different colors in its color cycle, while matplotlib 2.0 has 10 different colors. Hence, updating matplotlib is another option.

In general, you may of course define your own color cycle which has as many colors as you wish.

  • Build a cycler from a colormap, as shown in this question:

    import matplotlib.pyplot as plt
    from cycler import cycler
    import numpy as np
    
    N = 8 # number of colors
    plt.rcParams["axes.prop_cycle"] = cycler('color', plt.cm.jet(np.linspace(0,1,N)) )
    
  • Build a cycler from a list of colors:

    import matplotlib.pyplot as plt
    from cycler import cycler
    
    colors=["aquamarine","crimson","gold","indigo",
            "lime","orange","orchid","sienna"]
    plt.rcParams["axes.prop_cycle"] = cycler('color',colors)
    
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!