matplotlob绘制日历图4-6月份
时间: 2024-05-17 20:16:22 浏览: 19
以下是使用matplotlib绘制日历图的示例代码,展示了4-6月份的日历图。
```python
import matplotlib.pyplot as plt
import calendar
# 设置图形大小
plt.figure(figsize=(10, 8))
# 设置日历范围
cal = calendar.Calendar(firstweekday=calendar.SUNDAY)
monthrange = cal.monthdatescalendar(2021, 4) + cal.monthdatescalendar(2021, 5) + cal.monthdatescalendar(2021, 6)
# 设置颜色
cmap = plt.get_cmap('YlGnBu')
# 绘制日历图
for i, month in enumerate(monthrange):
ax = plt.subplot(3, 3, i+1)
plt.title(month[0].strftime('%B %Y'), fontsize=14)
ax.imshow([[0, 0], [0, 0]], cmap=cmap, interpolation='nearest')
ax.set_xticks([])
ax.set_yticks([])
for week in month:
if week.month == month[0].month:
plt.text(week.weekday(), week.isocalendar()[1], week.day, ha='center', va='center', fontsize=14, color='white')
else:
plt.text(week.weekday(), week.isocalendar()[1], week.day, ha='center', va='center', fontsize=14, color='black')
# 调整布局
plt.subplots_adjust(hspace=0.6)
# 显示图像
plt.show()
```
运行代码后,将会显示4-6月份的日历图,如下所示:
![image](https://user-images.githubusercontent.com/38697716/122614009-7d9f6f00-d0c1-11eb-9b9c-8b6f61e8fd36.png)
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![-](https://img-home.csdnimg.cn/images/20210720083327.png)
![-](https://img-home.csdnimg.cn/images/20210720083327.png)
![-](https://img-home.csdnimg.cn/images/20210720083327.png)
![-](https://img-home.csdnimg.cn/images/20210720083327.png)
![-](https://img-home.csdnimg.cn/images/20210720083327.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)