# Matplotlib draws a table of Spring Festival holidays

I haven't updated the python&gis blog series for a long time. In fact, I have been neglecting python's learning and summary recently.

I just learned Python's matplotlib drawing these days, and it's time for the Spring Festival vacation.In order to let the team members know their vacation time intuitively and facilitate the scheduling and transfer of work, I volunteered to draw a chart.That's the article.(The company belongs to the GIS industry and the implementation method is python, so it's not a mistake.)

## Result Diagram

The effect of the drawing is as follows:

The vertical axis is the name of the team member, the horizontal axis is the time, and the legend on the right shows that the colors in Figure 4 represent four states.The emoji in the diagram also corresponds to the state.Dark blue works in the office, sky blue works remotely, yellow applies for vacation, dark red is public vacation, green is half-day vacation.

## Implementation Steps

Also a beginner of matplotlib, there are many ways to achieve estimates are not very standard, let's take a look first.

### Data preparation

Since there are four states of team vacation, 18 days of statistics and 6 members (secret, so team information is virtual), I am going to use a 6*18 matrix to represent this information.A row represents a team member's vacation.

Date, of course, can also be generated with code.

```    date = ["Jan.19", "Jan.20", "Jan.21", "Jan.22", "Jan.23",
"Jan.24", "Jan.25", "Jan.26", "Jan.27", "Jan.28",
"Jan.29", "Jan.30", "Jan.31", "Feb.01", "Feb.02",
"Feb.03", "Feb.04", "Feb.05"]
```

The name, the list you started with, was later changed to np.array because of slice requirements.

```    name = ["James", "Mary", "Ben", "John", "Linda", "Tonny"]
name = np.array(name)
```

Vacation status:
Initialize the array to 0, first calculate the public holidays, add 3 to the corresponding dates, and then add the holidays everyone sent me.For example, Ben says I take a day off on the 23rd and two days off on the 8th day of the 7th day.I'll add 2 to the 5th, 13th, 14th of Ben's line.

```    # 0: office
# 1: remote
# 2: vacation
# 3: public holiday
# 4: half day
# start from Jan.19, end with Feb.05
teams = np.zeros([len(name), len(date)])
legal_holiday  = [0, 0, 0, 0, 0, 3, 3, 3, 3, 3, 3, 3, 0, 0, 3, 0, 0, 0]
teams += legal_holiday
#                19              24                   31             5
# James
teams[0]      += [1, 1, 1, 2, 2, 0, 0, 0, 0, 0, 0, 0, 2, 2, 0, 0, 0, 0]
# Mary
teams[1]      += [0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 2, 2, 0, 2, 2, 0]
# Ben
teams[2]      += [0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 2, 2, 0, 0, 0, 0]
# John
teams[3]      += [0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 2, 2, 0, 2, 2, 2]
# Linda
teams[4]      += [0, 0, 2, 1, 1, 0, 0, 0, 0, 0, 0, 0, 4, 4, 0, 0, 0, 0]
# Tonny
teams[5]      += [0, 2, 2, 2, 2, 0, 0, 0, 0, 0, 0, 0, 2, 2, 0, 2, 2, 4]
```

If you don't have a lot of team members, you won't have any NLP s, automatic recognition, etc. After all, drawing this chart isn't a job, it doesn't take much time.

Since each group in the team needs to be mapped separately, a sorting, filtering method is also added to generate the data that is developed.

```    # filter by idx
teams_new = np.zeros([len(idx), len(date)])
name_new = np.zeros(len(idx),)

teams_new = teams[idx]
name_new = name[idx]
return teams_new, date, name_new
```

The values in the idx array can be set to any number from 0 to 5.For example: [0, 1, 5, 5, 5].The results are as follows:

### Mapping

With both imshow and matshow, I'll use imshow.Because a legend is required, colormap is used here.

```color_list = ['darkblue', 'skyblue', 'yellow', 'darkred', 'lightgreen']
self_cmap = colors.ListedColormap(color_list)
fig = plt.figure()
h = ax.imshow(teams, cmap=self_cmap)
```

### Legend

Be lazy and use colorbar.However, there are only four discrete values for vacation status, so legends are better.Locations can be set using box_to_anchor and loc.Use mpathces to set colors, text, etc.

```label_list = ['Half day off', 'Public Holiday', 'Vacation',  'Remote', 'Office']
handles = []
for i in range(len(color_list)):
handles.append(mpatches.Patch(color=color_list[i], label=label_list[len(color_list) - i - 1]))

plt.legend(bbox_to_anchor=(1.01, 1), loc='upper left', borderaxespad=0., handles=handles)
```

### Tagging

Is to write up in coordinates

```# emoji annotation
emojis = ['ðŸ˜±', 'ðŸ˜‚', 'ðŸ˜Š',  'ðŸ˜„', 'ðŸ˜›']
for i in range(len(name)):
for j in range(len(date)):
ann = emojis[int(teams[i, j])]
color = 'w'
if int(teams[i, j]) in [1, 2, 4]:
color = 'b'
text = ax.text(j, i, ann, ha='center', va='center', color=color, fontsize='16')
```

### Axis and Grid

(Personally, this part of the content is still more troublesome and needs careful calculation, otherwise it is uneven)

This is part of the auxiliary line.To make the chart clearer, you need to label the date and name on the axis.To make each color block clearer, you need to draw a grid at the scale.In order for the grid to draw correctly, some settings need to be made for the scale position of the axis.Otherwise the grid will be drawn in the middle of the scale.

```ax.set_xticks(np.arange(0, len(date), 1))
ax.set_yticks(np.arange(0, len(name), 1))
ax.set_xticklabels(date)
ax.set_yticklabels(name)
ax.set_xticks(np.arange(-.5, len(date), 1), minor=True)
ax.set_yticks(np.arange(-.5, len(name), 1), minor=True)

ax.grid(color='w', which='minor', linestyle='-', linewidth=2)
```

The date is longer, so tilt a little and draw at the bottom

```ax.xaxis.set_ticks_position('bottom')
plt.xticks(rotation=60)
```

### display

```plt.title('Spring Festival holiday for Dev Team', fontsize=20)
plt.show(
```

## summary

Probably that's it, full code Download Address .Finally, after experiencing matplotlib, plotly, d3.js, echart, g2, it is found that excel is better.

Happy New Year to you all!

151 original articles were published. 161 were praised. 300,000 visits+

Posted on Mon, 20 Jan 2020 22:55:45 -0500 by mattbarber