我正在尝试在Matplotlib中进行一些绘图。尽管我能够弄清很多事情,但是有些事情并没有我希望的那样。例如,在下面的代码中,我正在绘制三个图表。在最后的月度图表中,我试图在每个季度(每年的3月,6月,9月,12月)放置主刻度线和网格线,并在每个月放置次刻度线/网格线。虽然未成年人是正确的,但主要不是。 同样,在第二个情节(每周情节)中,我希望每个月都有主要的刻度线/网格,而每个星期都希望有次要的网格线。同样,未成年人似乎是有效的,但不是主要的。 我也想将x轴日期的格式设置为2015年3月。但它一直以'2015-03'的形式出现 我该如何解决? 下面是一些代码摘录。如果有帮助,我可以编辑以发布完整的代码
import pandas as pd
from datetime import datetime
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import itertools
import random
random.seed(30)
np.random.seed(30)
first_names=('John','mary','hillary')
last_names=('Johnson','Smith','Williams')
start_date,end_date = '1/1/2015','12/31/2018'
date_rng = pd.date_range(start= start_date, end=end_date, freq='D')
length_of_field = date_rng.shape[0]
group_3 = [random.choice(first_names)+" "+random.choice(last_names) for _ in range(length_of_field)]
df = pd.DataFrame(date_rng, columns=['date'])
df['user'] = group_3
df['attempts'] = np.random.randint(0,100,size=(len(date_rng)))
df['n_date'] = df['date'].dt.normalize()
df_grp_1 = df.groupby(['user']).sum()
df_grp_1.sort_values(by=['attempts'],ascending=False,inplace=True)
toppers = list(df_grp_1.index.array)
toppers_to_keep =toppers[:6]
agg_df_daily = df.pivot_table(index='n_date',columns = 'user',values='attempts',aggfunc='sum')
print('\n(agg_df_daily:)')
print(agg_df_daily)
agg_df_weekly = agg_df_daily[toppers_to_keep].resample('W').sum()
print('\n(agg_df_weekly:)')
print(agg_df_weekly)
agg_df_monthly = agg_df_daily[toppers_to_keep].resample('M').sum()
print('\n(agg_df_monthly:)')
print(agg_df_monthly)
# PLOTTING...
topper_case =[x.title() for x in toppers_to_keep]
start, end = start_date,end_date
plt.rcParams["figure.figsize"] = (10.0,7.5) #set figure size
marker = itertools.cycle(('*','o','+',','))
years = mdates.YearLocator()
quarters = mdates.MonthLocator(3,6,9,12)
months = mdates.MonthLocator()
weekly = mdates.WeekdayLocator(byweekday=(1),interval=1)
fig = plt.figure()
st = fig.suptitle("Number of Attempts DAILY", fontsize="x-large")
ax1 = plt.subplot(2,1,1)
for i in range(0,3):
ax1.plot(agg_df_daily.loc[start:end,toppers[i]],label=topper_case[i],marker='.')
ax1.xaxis.set_major_locator(quarters)
ax1.xaxis.set_minor_locator(months)
ax1.legend()
ax1.set_title("Top 4 Users")
plt.grid(True,linestyle='--')
plt.ylabel('Number of Attempts per Day')
ax2 = plt.subplot(2,1,2,sharex=ax1)
for i in range(3,6):
plt.plot(agg_df_daily.loc[start:end,toppers[i]],label=topper_case[i])
ax2.legend()
ax2.set_title("Next 4 users")
plt.grid(True,linestyle='--')
plt.ylabel('Number of Attempts per Day')
plt.xlabel('Day')
# WEEKLY PLOT
fig = plt.figure()
st = fig.suptitle("Number of Attempt Over Time By 8 Most Prolific Users WEEKLY", fontsize="x-large")
ax1 = plt.subplot(2,1,1)
for i in range(0,3):
plt.plot(agg_df_weekly.loc[start:end,toppers[i]],label=topper_case[i],marker='.')
ax1.xaxis.set_major_locator(months)
ax1.xaxis.set_minor_locator(weekly)
ax1.legend()
ax1.set_title("Top 4 Most Prolific Users")
plt.grid(True,linestyle='--')
plt.ylabel('Number of Attempts per Week')
ax2 = plt.subplot(2,1,2,sharex=ax1)
for i in range(3,6):
plt.plot(agg_df_weekly.loc[start:end,toppers[i]],label=topper_case[i])
ax2.legend()
ax2.set_title("Next 4 Most Prolific Users")
plt.grid(True,linestyle='--')
plt.ylabel('Number of Attempts per Week')
plt.xlabel('Week')
# MONTHLY PLOT
fig = plt.figure()
st = fig.suptitle("Number of Attempts Over Time By 8 Most Prolific Users MONTHLY", fontsize="x-large")
ax1 = plt.subplot(2,1,1)
for i in range(0,3):
ax1.plot(agg_df_monthly.loc[start:end,toppers[i]],label=topper_case[i],marker='.')
ax1.xaxis.set_major_locator(quarters)
ax1.xaxis.set_major_formatter(mdates.DateFormatter('\n\n\n%b\n%Y'))
ax1.xaxis.set_minor_locator(months)
ax1.legend()
ax1.set_title("Top 4 Most Prolific Users")
plt.grid(True,linestyle='--')
plt.ylabel('Number of Attemps per Month')
ax2 = plt.subplot(2,1,2,sharex=ax1)
for i in range(3,6):
plt.plot(agg_df_monthly.loc[start:end,toppers[i]],label=topper_case[i],marker='o')
ax2.legend()
ax2.set_title("Next 4 Most Prolific Users")
plt.grid(True,linestyle='--')
plt.ylabel('Number of Attempts per Month')
plt.xlabel('Month')
plt.show()