Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何使用matplotlib修补程序打印日历_Python_Pandas_Numpy_Matplotlib_Graph - Fatal编程技术网

Python 如何使用matplotlib修补程序打印日历

Python 如何使用matplotlib修补程序打印日历,python,pandas,numpy,matplotlib,graph,Python,Pandas,Numpy,Matplotlib,Graph,我想创建一个演示日历,它反映了一个月内每天以及每个设备发生特定事件的风险 我在网上看到了这篇文章: # 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

我想创建一个演示日历,它反映了一个月内每天以及每个设备发生特定事件的风险

我在网上看到了这篇文章:

# 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轴上设置月份,而是将
device_1
设置为
device_100
,(即日历将在一个月内设置为100台设备)

第二,我想使用不同的(或仅2)颜色来表示网格中的不同风险级别


无论如何,主要目的是创建一个演示日历来可视化潜在的风险,我不确定如何实现。

这可以通过以下代码的调整来实现。新函数采用一个参数
days
,该参数应该是要为每个要打印的设备标记的
(day,color)
元组列表。它还需要一个可选参数
days\u in_month
,以与少于31天的月份兼容

代码:

def plot_device_calendar(days, days_in_month=31):
    plt.figure(figsize=(.3*days_in_month, .3*len(days)))
    ax = plt.gca().axes
    for device, days_to_mark in enumerate(days, start=1):
        for d in days_to_mark:
            ax.add_patch(Rectangle((d[0], device), 
                                   width=.8, height=.8, color=d[1]))
    plt.yticks(np.arange(1, len(days)+1)+.5, [f"device_{i}" for i in range(1, len(days)+1)])
    plt.xticks(np.arange(1,days_in_month+1)+.5, np.arange(1,days_in_month+1))
    plt.xlim(1, days_in_month+1)
    plt.ylim(1, len(days)+1)
    plt.gca().invert_yaxis()
    for spine in plt.gca().spines.values():
        spine.set_visible(False)
    plt.tick_params(top=False, bottom=False, left=False, right=False)
    plt.show()
>>> colors = ['b','g','r','c','m','y','k','w']
>>> l = [random.sample(range(1,32), 10) for i in range(20)]
>>> l = [[(x,random.choice(colors)) for x in sl] for sl in l]
>>> plot_device_calendar(l)
输入示例:(3台设备,10个随机天数和颜色)

用法:

def plot_device_calendar(days, days_in_month=31):
    plt.figure(figsize=(.3*days_in_month, .3*len(days)))
    ax = plt.gca().axes
    for device, days_to_mark in enumerate(days, start=1):
        for d in days_to_mark:
            ax.add_patch(Rectangle((d[0], device), 
                                   width=.8, height=.8, color=d[1]))
    plt.yticks(np.arange(1, len(days)+1)+.5, [f"device_{i}" for i in range(1, len(days)+1)])
    plt.xticks(np.arange(1,days_in_month+1)+.5, np.arange(1,days_in_month+1))
    plt.xlim(1, days_in_month+1)
    plt.ylim(1, len(days)+1)
    plt.gca().invert_yaxis()
    for spine in plt.gca().spines.values():
        spine.set_visible(False)
    plt.tick_params(top=False, bottom=False, left=False, right=False)
    plt.show()
>>> colors = ['b','g','r','c','m','y','k','w']
>>> l = [random.sample(range(1,32), 10) for i in range(20)]
>>> l = [[(x,random.choice(colors)) for x in sl] for sl in l]
>>> plot_device_calendar(l)
输出:

def plot_device_calendar(days, days_in_month=31):
    plt.figure(figsize=(.3*days_in_month, .3*len(days)))
    ax = plt.gca().axes
    for device, days_to_mark in enumerate(days, start=1):
        for d in days_to_mark:
            ax.add_patch(Rectangle((d[0], device), 
                                   width=.8, height=.8, color=d[1]))
    plt.yticks(np.arange(1, len(days)+1)+.5, [f"device_{i}" for i in range(1, len(days)+1)])
    plt.xticks(np.arange(1,days_in_month+1)+.5, np.arange(1,days_in_month+1))
    plt.xlim(1, days_in_month+1)
    plt.ylim(1, len(days)+1)
    plt.gca().invert_yaxis()
    for spine in plt.gca().spines.values():
        spine.set_visible(False)
    plt.tick_params(top=False, bottom=False, left=False, right=False)
    plt.show()
>>> colors = ['b','g','r','c','m','y','k','w']
>>> l = [random.sample(range(1,32), 10) for i in range(20)]
>>> l = [[(x,random.choice(colors)) for x in sl] for sl in l]
>>> plot_device_calendar(l)

漂亮的代码和绘图。我想知道是否所有颜色都可以遵循相同的配色方案,只是为了显示相关性?Thanks@nilsinelabore您可以将任何需要的颜色作为颜色传递-matplotlib颜色:如['r'、'b'、'g'、'c']、matplotlib颜色方案:如['C0'、'C1'、…]甚至RGBA元组:[(1,0,0,1),(0,1,0,1),(0,0,1,1),…]有关详细信息,请参阅。