python - 在 matplotlib 中使用更多颜色进行绘图

标签 python matplotlib colormap

我正在尝试使用 matplotlib 绘制散点图,但收到“IndexError:从空列表中弹出”错误,并且我不知道如何修复它。

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

我有d的几个数据集,d是一个字典。当数据集较小时,它可以正常工作而不会出现任何错误。当数据集很大时,就会出现项圈不足的情况。如何向列表中添加更多颜色,以便“d”中的每个键都有自己的颜色。

请随意编辑我的代码并提出建议。

最佳答案

Colormaps 是可调用的。当传递 0 到 1 之间的 float 时,它返回 RGBA 颜色:

In [73]: jet = plt.cm.jet

In [74]: jet(0.5)
Out[74]: (0.49019607843137247, 1.0, 0.47754585705249841, 1.0)

因此,您可以通过将 NumPy 数组 np.linspace(0, 1, len(d)) 传递给颜色图来生成 len(d) 种颜色:

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

所选颜色将沿着颜色图渐变均匀分布。


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

关于python - 在 matplotlib 中使用更多颜色进行绘图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22995352/

相关文章:

matplotlib - matplotlib —以交互方式选择点或位置?

python - matplotlib - 无法无错误地绘制

Matlab:明确指定饼图切片颜色

python - 将多级多索引名称转换为 DataFrame 列,Python 3.6

python - 命名序列

Python - ElementTree - 不能在元素上使用绝对路径

python - 使用 matplotlib 绘制感知器算法

python - 使用 tab* 内置元素在 matplotlib 中组成颜色图

使用绝对值的 python networkx 颜色图

python - 我该如何解决这个问题 : ERROR: Command errored out with exit status 1 : when trying to "pip install psycopg2"