如何获得主要和次要刻度线标签

时间:2018-07-04 15:37:28

标签: python matplotlib axis

我有一个分组依据的输出,表示每周的日期总和。

Date
2008-10-28    20.0
2008-11-04    25.0
2008-11-11    20.0
2008-11-18    40.0
2008-11-25    35.0
2008-12-02    35.0
2008-12-09     NaN
2008-12-16     NaN
2008-12-23     NaN
2008-12-30     NaN
Freq: W-TUE, Name: Count, dtype: float64

我正在尝试使用plot_date

绘制这些图
fig, ax = plt.subplots(figsize=(2, 4))
# ax = plt.gca()
line = ax.plot_date(a.index, a.values, '.', label='a', alpha=0.5, linewidth=1)
ax.tick_params('y', colors='k')
ax.set_xlabel('Date')
ax.set_ylabel('Frequency')
ax.set_title('Daily Games')
ax.tick_params('y', colors='k')
ax.grid(b=True, which='major', color='w', linewidth=1.0)
ax.grid(b=True, which='minor', color='w', linewidth=0.5)
ax.yaxis.grid(True)
ax.get_xaxis().set_minor_locator(mpl.ticker.AutoMinorLocator())
ax.set_xticklabels(ax.xaxis.get_majorticklabels(),
                    rotation=70)
ax.set_xticklabels(ax.xaxis.get_minorticklabels(),
                    rotation=70)
plt.xticks(rotation=70)
plt.show()

这将产生如下图:

enter image description here

我已经尝试了所有方式的重新排列,但是我无法同时获得次要标签和主要标签。

我希望每个月都以70度标示。我该如何调整呢?

2 个答案:

答案 0 :(得分:1)

您可以按如下方式使用AutoDateLocator()

import seaborn as sns
import matplotlib as mpl
import matplotlib.pyplot as plt
import pandas as pd

sns.set()

a = pd.DataFrame([
    ('2008-10-28', 20.0), ('2008-11-04', 25.0), ('2008-11-11', 20.0), 
    ('2008-11-18', 40.0), ('2008-11-25', 35.0), ('2008-12-02', 35.0)], columns=['Date', 'Frequency'])

a['Date'] = pd.to_datetime(a['Date'], format='%Y-%m-%d')

fig, ax = plt.subplots(figsize=(5, 5))
# ax = plt.gca()
line = ax.plot_date(a.Date, a.Frequency, '.', label='a', alpha=0.5, linewidth=1)
ax.tick_params('y', colors='k')
ax.set_xlabel('Date')
ax.set_ylabel('Frequency')
ax.set_title('Daily Games')
ax.tick_params('y', colors='k')
ax.grid(b=True, which='major', color='w', linewidth=1.0)
ax.grid(b=True, which='minor', color='w', linewidth=0.5)
ax.yaxis.grid(True)

xtick_locator = mpl.dates.AutoDateLocator()
xtick_formatter = mpl.dates.AutoDateFormatter(xtick_locator)

ax.xaxis.set_major_locator(xtick_locator)
ax.xaxis.set_major_formatter(xtick_formatter)

fig.subplots_adjust(bottom=0.24)
plt.xticks(rotation=70)
plt.show()

这将显示为:

plot with dates displayed

答案 1 :(得分:0)

在@MartinEvans建议使用AutoDateLocator()的情况下,我查找了更多matplotlib文档,并发现matplotlib.dates.MonthLocatorWeekdayLocator。这允许调整主要和次要的ticks,以根据需要更改格式和外观。

然后我用this answer设置它们的旋转角度。

fig, ax = plt.subplots(figsize=(2, 4))
# ax = plt.gca()
line = ax.plot_date(a.Date, a.Frequency, '.', label='a', alpha=0.5, linewidth=1)
ax.tick_params('y', colors='k')
# ax.xticks(rotation=70)
ax.set_xlabel('Date')
# ax.xlabel('Date')
ax.set_ylabel('Frequency')
ax.set_title('Daily Games')
ax.tick_params('y', colors='k')
ax.grid(b=True, which='major', color='w', linewidth=1.0)
ax.grid(b=True, which='minor', color='w', linewidth=0.5)
ax.yaxis.grid(True)

xtick_locator = mpl.dates.MonthLocator(interval=1)
xtick_formatter = mpl.dates.AutoDateFormatter(xtick_locator)
ax.xaxis.set_major_locator(xtick_locator)
ax.xaxis.set_major_formatter(xtick_formatter)

xtick_locator = mpl.dates.WeekdayLocator(byweekday=3)
xtick_formatter = mpl.dates.AutoDateFormatter(xtick_locator)
ax.xaxis.set_minor_locator(xtick_locator)
ax.xaxis.set_minor_formatter(xtick_formatter)

plt.setp(ax.xaxis.get_minorticklabels(), rotation=90, size=10)
plt.setp(ax.xaxis.get_majorticklabels(), rotation=90, size=7)

fig.subplots_adjust(bottom=0.24)


plt.show()

enter image description here