Python matplotlib中每个月的主要滴答声和每周的次要滴答声

Python matplotlib中每个月的主要滴答声和每周的次要滴答声,python,matplotlib,seaborn,Python,Matplotlib,Seaborn,我有一幅图,显示了一年中每一天每小时的温度 这是我写的代码: mydateparser = lambda x: datetime.strptime(x, "%Y-%m-%d") df = pd.read_csv("Vaderdata.csv", usecols=['Date','Time','Temp'], parse_dates=['Date'], date_parser=mydatepa

我有一幅图,显示了一年中每一天每小时的温度

这是我写的代码:

mydateparser = lambda x: datetime.strptime(x, "%Y-%m-%d")
df = pd.read_csv("Vaderdata.csv",
           usecols=['Date','Time','Temp'],
           parse_dates=['Date'],
           date_parser=mydateparser)

pivot = pd.pivot_table(df, values='Temp',columns='Date',index='Time')
fig, ax = plt.subplots(figsize = (12,6)) 

clr = sns.color_palette("coolwarm", as_cmap=True)
fig = sns.heatmap(pivot, center = 0,cmap = clr )

plt.show()
正如你所看到的,x轴不是很具有描述性。 我希望每个月都有一个带标签的大刻度,每个星期都有一个小刻度。
我找到了一些将日期时间格式化为字符串的示例,这样x轴至少可以显示一些内容,而不是仅显示零,但我无法找到如何执行我刚才描述的操作。

月显示由MonthLocator设置为一个月,并带有月缩写。几周来,我们在DayLocator中有7天的间隔数据,并设置原始标签。使用
ax.xaxis.set\U minor\U格式化程序(“%U”)
本来很容易,但是

import pandas as pd
import numpy as np
import random

random.seed(202012)

date_rng = pd.date_range('2019/01/01', '2019/12/31', freq='1H')
temp = np.random.randint(-10,35, size=8737)
df = pd.DataFrame({'date':pd.to_datetime(date_rng),'Temp':temp})

df['Time'] = df['date'].dt.hour
df['Date'] = df['date'].dt.date
df['Week'] = df['date'].dt.week
df = df[['Date','Week','Time','Temp']]
pivot = pd.pivot_table(df, values='Temp',columns='Date',index='Time')

# week num create
weeks = df[['Date','Week']]
ww = weeks.groupby('Week').first().reset_index()

import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import matplotlib.ticker as ticker
import seaborn as sns

fig, ax = plt.subplots(figsize = (24,6)) 

clr = sns.color_palette("coolwarm", as_cmap=True)
fig = sns.heatmap(pivot, center = 0,cmap = clr )

months = mdates.MonthLocator(interval=1)
months_fmt = mdates.DateFormatter('%b')
ax.xaxis.set_major_locator(months)
ax.xaxis.set_major_formatter(months_fmt)

days = mdates.DayLocator(interval=7)
ax.xaxis.set_minor_locator(days)
ax.xaxis.set_minor_formatter(ticker.FixedFormatter(ww.Week))
# ax.xaxis.set_minor_formatter('%U') # Not displayed correctly

plt.show()

月显示由MonthLocator设置为带有月缩写的一个月。几周来,我们在DayLocator中有7天的间隔数据,并设置原始标签。使用
ax.xaxis.set\U minor\U格式化程序(“%U”)
本来很容易,但是

import pandas as pd
import numpy as np
import random

random.seed(202012)

date_rng = pd.date_range('2019/01/01', '2019/12/31', freq='1H')
temp = np.random.randint(-10,35, size=8737)
df = pd.DataFrame({'date':pd.to_datetime(date_rng),'Temp':temp})

df['Time'] = df['date'].dt.hour
df['Date'] = df['date'].dt.date
df['Week'] = df['date'].dt.week
df = df[['Date','Week','Time','Temp']]
pivot = pd.pivot_table(df, values='Temp',columns='Date',index='Time')

# week num create
weeks = df[['Date','Week']]
ww = weeks.groupby('Week').first().reset_index()

import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import matplotlib.ticker as ticker
import seaborn as sns

fig, ax = plt.subplots(figsize = (24,6)) 

clr = sns.color_palette("coolwarm", as_cmap=True)
fig = sns.heatmap(pivot, center = 0,cmap = clr )

months = mdates.MonthLocator(interval=1)
months_fmt = mdates.DateFormatter('%b')
ax.xaxis.set_major_locator(months)
ax.xaxis.set_major_formatter(months_fmt)

days = mdates.DayLocator(interval=7)
ax.xaxis.set_minor_locator(days)
ax.xaxis.set_minor_formatter(ticker.FixedFormatter(ww.Week))
# ax.xaxis.set_minor_formatter('%U') # Not displayed correctly

plt.show()

谢谢!这正是我想要的。谢谢!这正是我想要的方式。