Python 如何获得主刻度和次刻度标签

Python 如何获得主刻度和次刻度标签,python,matplotlib,axis,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:

通过表示每周的日期总和,我得到了一个组的输出

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\u 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()
这是一个这样的图形:

我已经尝试了各种方式的重新安排,但我无法同时获得日期的次要和主要标签


我想每个月都标70度。如何调整必须执行的操作?

您可以使用
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()
这将显示为:


根据@MartinEvans建议使用
AutoDateLocator()
我查阅了更多
matplotlib
文档,并找到了
WeekdayLocator
。这允许调整主要和次要XTICK,以根据需要更改格式和外观

然后我设置了它们的旋转

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()