Plotting with more colors in matplotlib

前端 未结 1 828
野的像风
野的像风 2021-01-23 03:13

I am trying to plot a scatter plot using matplotlib, i am getting \" IndexError: pop from empty list\" error and I am not sure how to fix it.

import matplotlib.p         


        
相关标签:
1条回答
  • 2021-01-23 03:52

    Colormaps are callable. When passed a float between 0 and 1, it returns an RGBA color:

    In [73]: jet = plt.cm.jet
    
    In [74]: jet(0.5)
    Out[74]: (0.49019607843137247, 1.0, 0.47754585705249841, 1.0)
    

    So, you could generate len(d) number of colors by passing the NumPy array np.linspace(0, 1, len(d)) to the colormap:

    jet = plt.cm.jet
    colors = jet(np.linspace(0, 1, len(d)))
    

    The colors selected will then be equally spaced along the colormap gradient.


    import matplotlib.pyplot as plt
    import numpy as np
    import time
    
    d = {'5000cca229d10d09': {374851: 1}, '5000cca229cf3f8f': {372496:3},'5000cca229d106f9': {372496: 3, 372455: 2}, '5000cca229d0b3e4': {380904: 2, 380905: 1, 380906: 1, 386569: 1}, '5000cca229d098f8': {379296: 2, 379297: 2, 379299: 2, 379303: 1, 379306: 1, 379469: 1, 379471: 1, 379459: 1, 379476: 1, 379456: 4, 379609: 4}, '5000cca229d03957': {380160: 3, 380736: 3, 380162: 1, 380174: 1, 381072: 2, 379608: 2, 380568: 3, 380569: 1, 380570: 1, 379296: 3, 379300: 1, 380328: 3, 379306: 1, 380331: 1, 379824: 2, 379825: 1, 379827: 1, 380344: 1, 379836: 1, 379456: 3, 380737: 1, 380739: 1, 379462: 1, 379476: 1, 379992: 3, 379609: 1, 379994: 1, 379611: 1, 379621: 1, 380006: 1, 380904: 3, 380905: 1, 380907: 1, 380535: 3, 380536: 1, 380538: 1}, '5000cca229cf6d0b': {372768: 10, 372550: 15, 372616: 14, 372617: 20, 372653: 3, 372505: 2}, '5000cca229cec4f1': {372510: 132}}
    
    jet = plt.cm.jet
    colors = jet(np.linspace(0, 1, len(d)))
    
    fig, ax = plt.subplots()
    for color, data_dict in zip(colors, d.values()):
       x = data_dict.keys()
       y = data_dict.values()
       ax.scatter(x,y,color=color, s = 90, marker='^')
       plt.ylabel("Errors" , fontsize=18, color="Green")
       plt.xlabel("Occured on",fontsize=18, color="Green")
       plt.title("DDN23b", fontsize=25, color="Blue")
       ax.get_xaxis().get_major_formatter().set_useOffset(False)
       plt.xticks(rotation='vertical') 
    
    
    plt.xticks(plt.xticks()[0],
               [time.strftime("%m/%d/%y, %H:%M:%S", time.localtime(item))
                for item in plt.xticks()[0]*3600]) 
    plt.legend(d.keys())
    plt.subplots_adjust(bottom=.24,right=.98,left=0.03,top=.89)
    plt.grid()
    plt.show()
    

    enter image description here

    0 讨论(0)
提交回复
热议问题