如何按月绘制日历热图
1 个回答
0
我用一个简单的环境测试了你的用例,使用的是 Python 3.9.12 和 calplot==0.1.7.5。下面是代码。
import pandas as pd
import numpy as np
import calplot
# Generate example data
np.random.seed(0)
dates = pd.date_range(start='2024-01-01', end='2024-12-31', freq='D')
sales = np.random.randint(100, 1000, size=len(dates))
# Create a DataFrame
data = pd.DataFrame({'Date': dates, 'Sales': sales})
# Reshape the data for calplot
data.set_index('Date', inplace=True)
# Plotting
calplot.calplot(data['Sales'], cmap='YlGn', colorbar=True, figsize=(30, 2))
plt.title('Sales in July 2024')
plt.show()
编辑 1:
你也可以使用 july 库 july==0.1.3
和 Python 3.9.12
得到类似的输出。
import july
import numpy
np.random.seed(0)
dates = pd.date_range(start='2024-01-01', end='2024-12-31', freq='D')
sales = np.random.randint(100, 1000, size=len(dates))
# Create a DataFrame
data = pd.DataFrame({'Date': dates, 'Sales': sales})
# Reshape the data for calplot
data.set_index('Date', inplace=True)
july.calendar_plot(dates=data.index, data=data.Sales)
plt.show()