欢迎光临散文网 会员登陆 & 注册

fx_2.0 python一/二次函数绘图

2022-08-27 16:49 作者:ICE27182  | 我要投稿

在学python

画图好难


#2.0 !!!

#介绍

print('\n\n\n\nfx_2.0可以绘制二次函数图像和一次函数图像',end='')

print('''

2.0更新内容:

1 图像由1.0版本旋转90°与通常的坐标轴方向一致

2 改善了缩放机制 相对河里

3 改进输入 可以防止输入错误值如字符导致的异常

4 增加说明、注释

5 解决了换行问题 而且图像可以很大了

''',end='')


#输入

def fxin(n):

    str(n)

    while True:

        try :

            a = float(input('%s=' % n))

            break

        except ValueError:

            print('请输入数字')

       

    return(a)


while True:

    a,b,c,x_min,x_max,scale = fxin('a'),fxin('b'),fxin('c'),fxin('x min'),fxin('x max'),fxin('图像缩放')


    #输入内容

    print('\nf(x)=',end='')


    if a == 1:

            print('x²',end='')

    elif a == -1:

            print('-x²',end='')

    elif a != 0:

            print('%fx²' % a,end='')


    if b == 1 and a == 0:

            print('x',end='')

    elif b == 1 and a != 0:

            print('+x',end='')

    elif b == -1:

            print('-x',end='')

    elif b != 0:

            if b > 0 and a != 0:

                print('+%fx' % b,end='')

            if b > 0 and a == 0:

                print('%fx' % b,end='')

            if b <= 0:

                print('%fx' % b,end='')


    if a == 0 and b == 0:

            print(c)

    elif a != 0 or b != 0:

            if c > 0:

                print('+%f' % c)

            if c < 0:

                print(c)

            if c == 0:

                print('')


    #确认

    while True:

        yrq = input('输入y确认\n输入r重新输入\n输入q退出\n')

        if yrq == 'y' or yrq == 'r':

            break

        elif yrq == 'q':

            exit()

        else:

            print('\n仅限输入 y 或 r 或 q')

    if yrq == 'y':

         del yrq

         break  


#计算函数值

def f_x(x_min,x_max,a,b,c,scale):

    '''计算函数值 列表A=B A排序B也会跟着 没办法 就干脆定义一个函数罢:('''

    fx = []

    while x_min <= x_max :

        fx.append(int(round((a*x_min**2+b*x_min+c)*scale,0)))

        x_min+=(1/scale)

    return fx


fx = f_x(x_min,x_max,a,b,c,scale)  #我爱咋咋

fx_sequence = set(f_x(x_min,x_max,a,b,c,scale))

fx_sequence = list(fx_sequence)

fx_sequence.sort(reverse=True)


#绘图

for e_fxs in fx_sequence:

    count = fx.count(e_fxs)

    count2,plen,pplen = 0,0,0

    while count2 < count:

        #print(pplen,'   ',len(fx)-1,'   ',fx)

        plen = fx.index(e_fxs,pplen,len(fx))

        indicator = fx[plen]

        print(' '*(plen-pplen), end='')

        print('*', end='')

        pplen = plen+1

        count2+=1

    if fx_sequence.index(e_fxs) < (len(fx_sequence)-1):

        print('\n'* (e_fxs - fx_sequence[fx_sequence.index(e_fxs)+1]),end="")


fx_2.0 python一/二次函数绘图的评论 (共 条)

分享到微博请遵守国家法律