如何在Python中绘制函数的输出?

2024-09-23 06:27:41 发布

您现在位置:Python中文网/ 问答频道 /正文

这三个函数为我提供了从0状态到接下来365个状态(或天数)的客户数量和订单的进度。在function state_evolution中,我想绘制第行的输出

custA = float(custA*1.09**(1.0/365))

与第行的输出相对应

^{pr2}$

custB执行相同的操作,这样我就可以以图形方式比较它们的输出。在

def get_state0():
    """ functions gets four columns from base data and finds their state 0"""
    statetype0 = {'custt':{'typeA':100,'typeB':200}}
    orderstype0 = {'orders':{'typeA':1095, 'typeB':4380}}
    return  {'custtypeA' : int(statetype0['custt']['typeA']),
             'custtypeB' : int(statetype0['custt']['typeB']),
             'ordstypeA': orderstype0['orders']['typeA'],'A':1095, 'B':4380,
             'ordstypeB':orderstype0['orders']['typeB'], 
             'day':0 }  


def state_evolution(state):
    """function takes state 0 and predicts state evolution """
    custA = state['custtypeA']
    custB = state['custtypeB']
    ordsA = state['ordstypeA']
    ordsB = state['ordstypeB']
    A = state['A']
    B = state['B']
    day = state['day']   
    # evolve day
    day += 1
    #evolve cust typea
    custA = float(custA*1.09**(1.0/365))
    #evolve cust typeb
    custB = float (custB*1.063**(1.0/365))
    # evolve orders cust type A 
    ordsA += int(custA * order_rateA(day))
    A = sum(80 + random.random() * 50 for i in range(ordsA))
    # evolve orders cust type B 
    ordsB += int(custB * order_rateB(day))
    B = sum(70 + random.random() * 40 for i in range(ordsB))
    return {'custtypeA':custA ,'ordstypeA':ordsA, 'A':A, 'B':B,
            'custtypeB':custB, 'ordstypeB':ordsB, 'day': day}


def show_all_states():
    """ function runs state evolution function to find other states"""
    s  = get_state0() 
    for day in range(365):
        s = state_evolution(s)

        print day, s

Tags: functionrandomintstateordersdayevolutionevolve
1条回答
网友
1楼 · 发布于 2024-09-23 06:27:41

您应该执行以下操作:

  1. 修改您的custA函数,使它返回一个序列(list,tuple),比如365个条目。或者,在列表理解或for循环中使用custA来获得365个结果的序列
  2. ordsA函数执行相同的操作,以获得另一个序列。在

从现在开始,你可以根据自己的需要做不同的事情。在

如果您希望两个图并行(叠加),则:

pyplot.plot(custA_result_list);
pyplot.plot(ordsA_result_list);
pyplot.show()

如果要关联数据,可以绘制散点图(更快),或使用点标记进行常规绘图(速度较慢但更可自定义IMO):

^{pr2}$

最后,如果数据是不规则采样的,您还可以提供横轴值的序列。例如,这样可以只绘制工作日的结果,而不会“崩溃”周末(否则,星期五和星期一之间的差距看起来就像一天):

weekdays = [1,2,3,4,5, 8,9,10,11,12, 15,16,17,18,19,  22, ...] # len(weekdays) ~ 260

pyplot.plot(weekdays, custA_result_list);
pyplot.plot(weekdays, ordsA_result_list);
pyplot.show()

希望这有帮助!在

编辑:关于excel,如果我理解正确,你已经有一个csv文件。然后,您可以使用csvpython模块,或者自己这样阅读:

with open('file.csv') as csv_in:
    content = [line.strip().split(',') for line in csv_in]

现在,如果您有一个实际的.xls.xlsx文件,请使用可以下载here或通过在命令提示符下运行pip install xlrd的模块。在

相关问题 更多 >