【发布时间】:2020-05-22 17:40:36
【问题描述】:
我想创建一个演示日历,它反映特定事件发生的风险一个月中的每一天,以及每个单独的设备
我在网上看到了这篇文章:
# Demo plot
import calendar
import numpy as np
from matplotlib.patches import Rectangle
import matplotlib.pyplot as plt
def plot_calendar(days, months):
plt.figure(figsize=(9, 3))
# non days are grayed
ax = plt.gca().axes
ax.add_patch(Rectangle((29, 2), width=.8, height=.8,
color='gray', alpha=.3))
ax.add_patch(Rectangle((30, 2), width=.8, height=.8,
color='gray', alpha=.5))
ax.add_patch(Rectangle((31, 2), width=.8, height=.8,
color='gray', alpha=.5))
ax.add_patch(Rectangle((31, 4), width=.8, height=.8,
color='gray', alpha=.5))
ax.add_patch(Rectangle((31, 6), width=.8, height=.8,
color='gray', alpha=.5))
ax.add_patch(Rectangle((31, 9), width=.8, height=.8,
color='gray', alpha=.5))
ax.add_patch(Rectangle((31, 11), width=.8, height=.8,
color='gray', alpha=.5))
for d, m in zip(days, months):
ax.add_patch(Rectangle((d, m),
width=.8, height=.8, color='C0'))
plt.yticks(np.arange(1, 13)+.5, list(calendar.month_abbr)[1:])
plt.xticks(np.arange(1,32)+.5, np.arange(1,32))
plt.xlim(1, 32)
plt.ylim(1, 13)
plt.gca().invert_yaxis()
# remove borders and ticks
for spine in plt.gca().spines.values():
spine.set_visible(False)
plt.tick_params(top=False, bottom=False, left=False, right=False)
plt.show()
from datetime import datetime, timedelta
def get_weekends(year):
weekend_day = []
weekend_month = []
start = datetime(year, 1, 1)
for i in range(365):
day_of_the_year = start + timedelta(days=i)
if day_of_the_year.weekday() > 4:
weekend_day.append(day_of_the_year.day)
weekend_month.append(day_of_the_year.month)
return weekend_day, weekend_month
weekend_day, weekend_month = get_weekends(2018)
plot_calendar(weekend_day, weekend_month)
并返回下面的日历:
首先,我想创建一个类似的图表,但不是在Y-axis 上设置月份,而是将device_1 设置为device_100,(即日历将适用于超过 100 个设备月)
其次,我想在网格中使用不同(或仅 2 种)颜色来表示不同的风险级别。
无论如何,主要目的是创建一个演示日历以可视化潜在风险,我不确定如何。
【问题讨论】:
标签: python pandas numpy matplotlib graph