我正在尝试使用 Python 中的 Pandas 和 Matplotlib 来绘制多年来收到的赠款总额。数据类型“奖励生效日期”是收到赠款的日期。我使用以下代码汇总了多年来收到的赠款中每天的金额:
#Displays daily sums of awards from FY12 to FY16
# *Revise ledger so only FY year dates are reported, not days.
df.groupby('Award Effective Date').award_amount.sum().plot(kind='bar',color=['orange'],\
figsize=(25,10),title="Daily Sums of Awards FY12 to FY16",\
grid=True)
这绘制了我需要的内容,但 x 轴标签困惑且难以辨认,因为它在过去几年中每天都会读取。我怎样才能将其更改为仅读取年份而不是每天?我还有另一种数据类型,称为“FY of EffDate”,它记录该日期的财政年度 - 如果可以使用的话。
最佳答案
这样的事情怎么样?需要根据您的具体情况进行修改...
import matplotlib.pyplot as plt
x = range(0,1000)
y = [i*2 for i in x]
plt.plot(x,y)
x_ticks = [day for day in x if day%365 == 0] # Only pull out full years
x_labels = ['Year ' + str(i) for i in range(len(x_ticks))]
plt.xticks(x_ticks, x_labels)
plt.show()
此图: years.py
关于Python - 将 Matplotlib 轴从几天更改为几年,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33504835/