我正试图在Matplotlib的同一绘图上绘制三条线。它们是本年度的发票、本年度的争议和本年度的百分比(即争议/发票)
原始输入是两列日期,一列是记录争议的日期,一列是记录发票的日期。
我用这些日期来统计某一年内每月的纠纷和发票数量。
然后我试着把它画出来,但它是空的。我开始只是想打印今年的百分比和今年的发票。
PercentThisYear = (DisputesFYThisYear/InvoicesFYThisYear).fillna(0.0)
#Percent_ThisYear.plot(kind = 'line')
#InvoicesFYThisYear.plot(kind = 'line')
plt.plot(PercentThisYear)
plt.xlabel('Date')
plt.ylabel('Percent')
plt.title('Customer Disputes')
# Remove the plot frame lines. They are unnecessary chartjunk.
ax = plt.subplot(111)
ax.spines["top"].set_visible(False)
ax.spines["bottom"].set_visible(False)
ax.spines["right"].set_visible(False)
ax.spines["left"].set_visible(False)
ax2 = ax.twinx()
ax2.plot(InvoicesFYThisYear)
# Ensure that the axis ticks only show up on the bottom and left of the plot.
# Ticks on the right and top of the plot are generally unnecessary chartjunk.
ax.get_xaxis().tick_bottom()
#ax.get_yaxis().tick_left()
# Limit the range of the plot to only where the data is.
# Avoid unnecessary whitespace.
datenow = datetime.datetime.now()
dstart = datetime.datetime(2015,4,1)
print datenow
#plt.ylim(0, .14)
plt.xlim(dstart, datenow)
firsts=[]
for i in range(dstart.month, datenow.month+1):
firsts.append(datetime.datetime(2015,i,1))
plt.xticks(firsts)
plt.show()
这是输出。。。日期搞砸了,没有指纹。但轴上的刻度看起来是正确的。我做错什么了?
如果有帮助的话,这是指向图表的设置
输入如下:
InvoicesThisYear
Out[82]:
7 7529
5 5511
6 4934
8 3552
dtype: int64
DisputesThisYear
Out[83]:
2 211
1 98
7 54
4 43
3 32
6 29
5 21
8 8
dtype: int64
PercentThisYear
Out[84]:
1 0.000000
2 0.000000
3 0.000000
4 0.000000
5 0.003810
6 0.005877
7 0.007172
8 0.002252
dtype: float64
Matplotlib无法知道哪些日期与哪些数据点关联。当您仅用一个参数
y
调用plot
时,Matplotlib自动假定x值为range(len(y))
。您需要提供日期作为plot
的第一个参数。假设今年的发票是每月发票数量的一个计数,从1开始到8结束,您可以做如下操作如果您的数据在Pandas系列中,并且索引是表示月份的整数,那么您只需将索引改为datetime对象。pandas.Series的
plot
方法将从那里自动处理事情。你可以这样做:相关问题 更多 >
编程相关推荐