CDF, matplotlib - not enough colors for plot, python

后端 未结 3 1106
半阙折子戏
半阙折子戏 2021-01-23 21:11

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

相关标签:
3条回答
  • 2021-01-23 21:48

    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.

    0 讨论(0)
  • 2021-01-23 21:51

    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)
      
    0 讨论(0)
  • 2021-01-23 22:05

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

    ffffd_hist_cent, = plt.plot(h_cent1[1:], hy_cent,label="Scan_Around", c='yellow') 
    
    0 讨论(0)
提交回复
热议问题