绘制每日水平数据,但只有每月水平标签

时间:2020-12-18 23:43:51

标签: python seaborn

给定以这种格式出现的数据:

d = {'date_utc': [01 01, 01 02, 02 03, 02 22, 03 05, 04 20], 'total_green': [3.0, 7.0, 10.0, 14.0, 2.0, 8.0]}
dfTime = pd.DataFrame(data=d)
dfTime

date_utc      total_green
01 01         3.0
01 02         7.0
02 03         10.0
02 22         14.0
03 05          2.0
04 20          8.0

我想随着时间的推移绘制此图,图表上的分析单位是每天,但 x 轴的标签是月份“Jan”、“Feb”等。

当前代码:

fig, ax = plt.subplots(figsize = (40, 15))
sns.lineplot(data=dfTime, linewidth=2.5)


# Customize the axes and title
ax.set_title("Green Words used on Instagram over Time", fontsize = 26)
ax.set_xlabel("Time", fontsize = 18)
ax.set_ylabel("Green Words", fontsize = 18)



plt.show()

我最终想要这样的东西:enter image description here

这可能吗?如果是这样,我该怎么做??

1 个答案:

答案 0 :(得分:1)

您需要在绘图前将您的 date_utc 转换为 datetime

import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import seaborn as sns
import numpy as np

current_year = '2020'
d = {'date_utc': ['01 01', '01 02', '02 03', '02 22', '03 05', '04 20'], 'total_green': [3.0, 7.0, 10.0, 14.0, 2.0, 8.0]}
d['date_utc'] = [np.datetime64('-'.join((current_year + ' ' + m).split(' '))) for m in d['date_utc']]
#d = {'date_utc': ['2020-01-01', '2020-01-02', '2020-02-03', '2020-02-22', '2020-03-05', '2020-04-20'], 'total_green': [3.0, 7.0, 10.0, 14.0, 2.0, 8.0]}
#d['date_utc'] = [np.datetime64(m) for m in d['date_utc']]

dfTime = pd.DataFrame(data=d)

months = mdates.MonthLocator()
days = mdates.DayLocator()
months_fmt = mdates.DateFormatter('%b')

fig, ax = plt.subplots(figsize = (40, 15))
sns.lineplot(data=dfTime, linewidth=2.5, x="date_utc", y="total_green")

# format the ticks
ax.xaxis.set_major_locator(months)
ax.xaxis.set_major_formatter(months_fmt)
ax.xaxis.set_minor_locator(days)

# Customize the axes and title
ax.set_title("Green Words used on Instagram over Time", fontsize = 26)
ax.set_xlabel("Time", fontsize = 18)
ax.set_ylabel("Green Words", fontsize = 18)

plt.show()

参考:

Date tick labels

strftime() and strptime() Format Codes

enter image description here