DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

Plotting a Calendar in Matplotlib

We quickly go through the code you'll need to get started working with this popular and open source Python library and plotting your data!

By 
Giuseppe Vettigli user avatar
Giuseppe Vettigli
·
Apr. 15, 19 · Tutorial
Likes (3)
Comment (0)

Save
Tweet
Share
12.19K Views

Join the DZone community and get the full member experience.

Join For Free

And here's a function to plot a compact calendar with matplotlib:

​x
1
import calendar
2
import numpy as np
3
from matplotlib.patches import Rectangle
4
import matplotlib.pyplot as plt
5
​
6
def plot_calendar(days, months):
7
    plt.figure(figsize=(9, 3))
8
    # non days are grayed
9
    ax = plt.gca().axes
10
    ax.add_patch(Rectangle((29, 2), width=.8, height=.8, 
11
                           color='gray', alpha=.3))
12
    ax.add_patch(Rectangle((30, 2), width=.8, height=.8,
13
                           color='gray', alpha=.5))
14
    ax.add_patch(Rectangle((31, 2), width=.8, height=.8,
15
                           color='gray', alpha=.5))
16
    ax.add_patch(Rectangle((31, 4), width=.8, height=.8,
17
                           color='gray', alpha=.5))
18
    ax.add_patch(Rectangle((31, 6), width=.8, height=.8,
19
                           color='gray', alpha=.5))
20
    ax.add_patch(Rectangle((31, 9), width=.8, height=.8,
21
                           color='gray', alpha=.5))
22
    ax.add_patch(Rectangle((31, 11), width=.8, height=.8,
23
                           color='gray', alpha=.5))
24
    for d, m in zip(days, months):
25
        ax.add_patch(Rectangle((d, m), 
26
                               width=.8, height=.8, color='C0'))
27
    plt.yticks(np.arange(1, 13)+.5, list(calendar.month_abbr)[1:])
28
    plt.xticks(np.arange(1,32)+.5, np.arange(1,32))
29
    plt.xlim(1, 32)
30
    plt.ylim(1, 13)
31
    plt.gca().invert_yaxis()
32
    # remove borders and ticks
33
    for spine in plt.gca().spines.values():
34
        spine.set_visible(False)
35
    plt.tick_params(top=False, bottom=False, left=False, right=False)
36
    plt.show()

You can use it to highlight the days with full moon in 2018:

3
1
full_moon_day = [2, 31, 2, 31, 30, 29, 28, 27, 26, 25, 24, 23, 22]
2
full_moon_month = [1, 1, 3, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
3
plot_calendar(full_moon_day, full_moon_month)

Matplotlib

Or just highlight all the weekends:

15
1
from datetime import datetime, timedelta
2
​
3
def get_weekends(year):
4
    weekend_day = []
5
    weekend_month = [] 
6
    start = datetime(year, 1, 1)
7
    for i in range(365):
8
        day_of_the_year = start + timedelta(days=i)
9
        if day_of_the_year.weekday() > 4:
10
            weekend_day.append(day_of_the_year.day)
11
            weekend_month.append(day_of_the_year.month)
12
    return weekend_day, weekend_month
13
​
14
weekend_day, weekend_month = get_weekends(2018)
15
plot_calendar(weekend_day, weekend_month)

Matplotlib

Calendar (Apple) Matplotlib

Published at DZone with permission of Giuseppe Vettigli, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Partner Resources

×

    Big Data Spotlight 4/25/19

  • Plotting a Calendar in Matplotlib

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!