Plotting with more colors in matplotlib

2020-04-17 03:58发布

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.pyplot as plt
import matplotlib
import numpy as np
import time
import itertools

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}}
colors = list("rgbcmyk")


for data_dict in d.values():
   x = data_dict.keys()
   #print x
   #X= time.asctime(time.localtime(x))
   y = data_dict.values()
   #plt.scatter(x,y,color=colors.pop(),s = 60)
   plt.scatter(x,y,color=colors.pop(),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")
   plt.gca().get_xaxis().get_major_formatter().set_useOffset(False)
   plt.xticks(rotation='vertical') 
   #plt.ylim(min(y),max(y))
   #plt.grid()

#for x, y in dict(itertools.chain(*[item.items() for item in d.values()])).items():
#    plt.text(x, y, time.strftime("%m/%d/%y, %H:%M:%S", time.localtime(x*3600)),     ha='center', va='top', rotation='vertical', fontsize = '11', fontstyle = 'italic', color = '#844d4d')

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())
mng = plt.get_current_fig_manager()
mng.resize(*mng.window.maxsize())
plt.subplots_adjust(bottom=.24,right=.98,left=0.03,top=.89)
plt.grid()
plt.show()

I have several data sets for d, and d is a dictionary. when the data set is smaller, it works without any errors. When the data set is large, it runs out of collars. How do I add more colors to the list so every key in "d" gets its own color.

Feel free to edit my code and make suggestions.

1条回答
神经病院院长
2楼-- · 2020-04-17 04:28

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

查看更多
登录 后发表回答