I am trying to map colors to cells in a table. However, I want this to be called from values in a separate column. Specifically, for the exmaple below, I'm plotting time values from Place. However, I have a separate column named Code that is a reference for these time values. I'm hoping to map a separate color for each unique value in Code to differentiate between time values.
At the moment, I'm manually inserting a separate color for appropriate time values. I'm hoping to generate a more flexible function that uses a colormap and can handle varying values in Code.
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import six
df = pd.DataFrame({
'Place' : ['Johnathon Santiago-Guillermo','Alan','Cory','Jim','Johnathon Santiago-Guillermo','Alan','Cory','Jim'],
'Number' : ['1','3','5','6','2','4','6','7'],
'Code' : ['1','2','3','4','1','2','3','4'],
'Time' : ['1904-01-01 08:00:00','1904-01-01 09:00:00','1904-01-02 01:00:00','1904-01-02 02:00:00','1904-01-01 08:10:00','1904-01-01 09:10:00','1904-01-02 01:10:00','1904-01-02 02:10:00'],
})
df['Time'] = pd.to_datetime(df['Time'])
df = df.sort_values('Time')
df['Time'] = pd.DatetimeIndex(df['Time']).time
df1 = df.pivot_table(index = 'Number', columns = 'Place', values = 'Time',
aggfunc = 'first').fillna('')
df1 = df1.reindex(columns = df['Place'].unique())
fig, ax = plt.subplots(figsize = (20, 20))
def Sheet(ax1):
ax1.axis('off')
ax1.axis('tight')
Times = ax1.table(cellText = df1.values, colLabels = df1.columns, cellLoc='center',
bbox = [0,0,1,1])
Times.auto_set_font_size(False)
Times.set_fontsize(5)
ax1 = plt.subplot2grid((2,3), (0,0), colspan = 3)
Sheet(ax1)
plt.show()
I played with your function a bit and pass a
cmap='tab10'. But first, you probably want to make sure that the color code is of the same shape as the time:Now the change in the function:
Output