如何在海底点图的y轴上正确格式化日期?

时间:2019-07-13 16:31:28

标签: python pandas datetime matplotlib seaborn

我有一个温度数据集,我正试图格式化y轴日期,以使它们不重叠且不那么频繁。

我尝试了多种方法(如您将在代码中看到的那样-我尝试过的几种方法已被剔除),包括day / month / yearLocator,set_majorLocator和格式化程序,AutoDateLocator。

# Import required python packages and matplotlib modules
import pandas as pd

import seaborn as sns
import matplotlib.pyplot as plt
from matplotlib.pyplot import figure
import matplotlib.dates as mdates
from matplotlib.dates import DayLocator, MonthLocator, YearLocator, DateFormatter

df_snowfall = pd.read_csv('racmosnowfall_dates.csv')
df_snowfall['date'] = pd.to_datetime(df_snowfall['date'], format="%d/%m/%Y")

figure(num=None, figsize=(8, 8), dpi=300, facecolor='w', edgecolor='k')

sns.set()
# White grid background, width of grid, line and series line
sns.set(style="whitegrid", rc={"grid.linewidth": 0.2, "lines.linewidth": 0.5})
sns.set_context(font_scale = 0.5) # Scale of font

df_snowfall['snowfallmm2'] = ((df_snowfall['snowfall'] / 70) * 86400)

snowfall_plot = sns.pointplot(x='date', y='snowfallmm2', data=df_snowfall)

snowfall_plot.xaxis_date()

#days = DayLocator(interval=1)
#months = MonthLocator(interval=1)
#daysFmt = DateFormatter('%d%m')

#plt.gca().xaxis.set_major_locator(months)
#plt.gca().xaxis.set_major_formatter(daysFmt)
#plt.gca().xaxis.set_minor_locator(days)

snowfall_plot.xaxis.set_major_locator(mdates.AutoDateLocator())

#snowfall_plot.autofmt_xdate()
#snowfall_plot.fmt_xdata = mdates.DateFormatter('%d/%m/%Y')

plt.setp(snowfall_plot.get_xticklabels(), rotation=90)

snowfall_plot.set(xlabel='', ylabel='Snowfall (mm)')

snowfall_plot.tick_params(labelsize=8) # Control the label size

plt.savefig('snowfall_plot.png', bbox_inches='tight')

我正在寻找一个月标签,其中一个标签显示年末岁月(日期范围为01/11/2017至30/03/2018)。

我反复收到此错误消息:

ValueError: view limit minimum -0.5 is less than 1 and is an invalid Matplotlib date value. This often happens if you pass a non-datetime value to an axis that has datetime units

尽管将“ date”列转换为datetime值,并明确说明了格式。

谢谢。

0 个答案:

没有答案