【问题标题】:Lines not showing up on Matplotlib graphMatplotlib 图表上未显示的线条
【发布时间】:2015-08-21 17:34:30
【问题描述】:

我正在尝试在 Matplotlib 的同一图上绘制三条线。它们是 InvoicesThisYear、DisputesThisYear 和 PercentThisYear(即 Disputes/Invoices)

原始输入是两列日期——一列用于记录争议的日期,另一列用于记录发票的日期。

我使用日期来计算某年每月的争议和发票数量。

然后我尝试绘制它,但它显示为空。我开始尝试打印 PercentThisYear 和 InvoicesThisYear。

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

【问题讨论】:

    标签: python matplotlib graph visualization subplot


    【解决方案1】:

    Matplotlib 无法知道哪些日期与哪些数据点相关联。当您调用plot 时只使用一个参数y,Matplotlib 会自动假定x 值为range(len(y))。您需要将日期作为第一个参数提供给plot。假设 InvoicesThisYear 是每月发票数量的计数,从 1 开始到 8 结束,您可以执行类似

    的操作
    import datetime
    import matplotlib.pyplot as plt
    import pandas as pd
    InvoicesFYThisYear = pd.DataFrame([0, 0, 0, 0, 5511, 4934, 7529, 3552])
    Disputes = pd.DataFrame([98, 211, 32, 43, 21, 29, 54, 8])
    PercentThisYear = (Disputes / InvoicesFYThisYear)
    datenow = datetime.date.today()
    ax = plt.subplot(111)  
    dates = [datetime.date(2015,i,1) for i in xrange(1, 9, 1)]
    plt.plot(dates, PercentThisYear)
    ax2 = ax.twinx()
    ax2.plot(dates, InvoicesFYThisYear)
    dstart = datetime.datetime(2015,4,1)
    plt.xlim(dstart, datenow)
    plt.xticks(dates, dates)
    plt.show()
    

    如果您的数据在 Pandas 系列中并且索引是表示月份的整数,那么您只需将索引更改为 datetime 对象即可。 pandas.Series 的 plot 方法将从那里自动处理事情。您可以这样做:

    Invoices = pd.Series((211, 98, 54, 43, 32, 29, 21, 8), index = (2, 1, 7, 4, 3, 6, 5, 8))
    dates = [datetime.date(2015, month, 1) for month in Invoices.index]
    Invoices.index = dates
    Invoices.plot()
    

    【讨论】:

    • 感谢您的回答。您是否知道使用系列进行绘图的任何方法,因为我的左列与右侧的计数相关联?或者也许将系列切片到列表中?
    • @jenryb,我已经编辑了我的答案以解决使用系列的问题。
    • 有一个开放的 PR,它增加了从传入的 Series 对象中提取索引的功能。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-14
    • 2023-01-18
    • 2018-11-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多