我試圖根據我使用的每日時間跟蹤文件進行時間跟蹤圖。我編寫了通過我的文件搜索並生成一些列表的代碼。在Python中使用顏色條和顏色映射創建顏色編碼的時間圖
endTimes是一個特別的活動分鐘從0午夜月的第一天要然而,許多分鐘都在一個月內結束的時間列表。
標籤是在endTimes中列出的時間的標籤列表。由於追蹤者在0分鐘之前沒有任何數據,因此比尾聲短。大多數標籤都是重複的。
類別包含標籤的每個唯一值,以便我認爲時間的順序。
我想創建一個彩條或colorbars的堆棧(1 eachday),將描繪我怎麼把我的時間了一個月,把與每個標籤相關的顏色。類別中的每個值都會有一個關聯的顏色。更多藍色更好。更紅更糟糕。這已經是爲了讓jet colormap正確,但是我需要爲類別中的每個值均勻地分配褻瀆顏色值。然後,我想下一步將是將其轉換爲列出的色彩圖,以便根據與類別關聯的標籤如何用於色條。
我認爲這是正確的做法,但我不確定。我不確定如何將標籤與顏色值相關聯。
到目前爲止,這是我的代碼的最後一部分。我發現了一個功能來製作一個離散的色彩地圖。它的確如此,但這不是我正在尋找的,我不確定發生了什麼。
感謝您的幫助!
# now I need to develop the graph
import numpy as np
from matplotlib import pyplot,mpl
import matplotlib
from scipy import interpolate
from scipy import *
def contains(thelist,name):
# checks if the current list of categories contains the one just read
for val in thelist:
if val == name:
return True
return False
def getCategories(lastFile):
'''
must determine the colors to use
I would like to make a gradient so that the better the task, the closer to blue
bad labels will recieve colors closer to blue
read the last file given for the information on how I feel the order should be
then just keep them in the order of how good they are in the tracker
use a color range and develop discrete values for each category by evenly spacing them out
any time not found should assume to be sleep
sleep should be white
'''
tracker = open(lastFile+'.txt') # open the last file
# find all the categories
categories = []
for line in tracker:
pos = line.find(':') # does it have a : or a ?
if pos==-1: pos=line.find('?')
if pos != -1: # ignore if no : or ?
name = line[0:pos].strip() # split at the : or ?
if contains(categories,name)==False: # if the category is new
categories.append(name) # make a new one
return categories
# find good values in order of last day
newlabels=[]
for val in getCategories(lastDay):
if contains(labels,val):
newlabels.append(val)
categories=newlabels
# convert discrete colormap to listed colormap python
for ii,val in enumerate(labels):
if contains(categories,val)==False:
labels[ii]='sleep'
# create a figure
fig = pyplot.figure()
axes = []
for x in range(endTimes[-1]%(24*60)):
ax = fig.add_axes([0.05, 0.65, 0.9, 0.15])
axes.append(ax)
# figure out the colors to use
# stole this function to make a discrete colormap
# http://www.scipy.org/Cookbook/Matplotlib/ColormapTransformations
def cmap_discretize(cmap, N):
"""Return a discrete colormap from the continuous colormap cmap.
cmap: colormap instance, eg. cm.jet.
N: Number of colors.
Example
x = resize(arange(100), (5,100))
djet = cmap_discretize(cm.jet, 5)
imshow(x, cmap=djet)
"""
cdict = cmap._segmentdata.copy()
# N colors
colors_i = np.linspace(0,1.,N)
# N+1 indices
indices = np.linspace(0,1.,N+1)
for key in ('red','green','blue'):
# Find the N colors
D = np.array(cdict[key])
I = interpolate.interp1d(D[:,0], D[:,1])
colors = I(colors_i)
# Place these colors at the correct indices.
A = zeros((N+1,3), float)
A[:,0] = indices
A[1:,1] = colors
A[:-1,2] = colors
# Create a tuple for the dictionary.
L = []
for l in A:
L.append(tuple(l))
cdict[key] = tuple(L)
# Return colormap object.
return matplotlib.colors.LinearSegmentedColormap('colormap',cdict,1024)
# jet colormap goes from blue to red (good to bad)
cmap = cmap_discretize(mpl.cm.jet, len(categories))
cmap.set_over('0.25')
cmap.set_under('0.75')
#norm = mpl.colors.Normalize(endTimes,cmap.N)
print endTimes
print labels
# make a color list by matching labels to a picture
#norm = mpl.colors.ListedColormap(colorList)
cb1 = mpl.colorbar.ColorbarBase(axes[0],cmap=cmap
,orientation='horizontal'
,boundaries=endTimes
,ticks=endTimes
,spacing='proportional')
pyplot.show()
僅供參考,您的「包含」功能等同於'在thelist'名。否則,我不太清楚你問的是什麼,但從你的描述來看,你不需要離散的彩色地圖。你只是想要一個堆積的條形圖。 –