27 、Python 使用plot进行绘制图表

基本思想:因为数据在csv中,需要使用panda进行读取,此处暂不上传数据表,数据写在代码中,同时进行曲线拟合计算机 系数 A B W(误差) 主要目的学习一下如何使用plot

import  pandas  as pd
from pylab import *

import os
mpl.rcParams['font.sans-serif'] = ['SimHei']
import math
def linefit(x , y):
    N = float(len(x))
    sx,sy,sxx,syy,sxy=0,0,0,0,0
    for i in range(0,int(N)):
        sx  += x[i]
        sy  += y[i]
        sxx += x[i]*x[i]
        syy += y[i]*y[i]
        sxy += x[i]*y[i]
    a = (sy*sx/N -sxy)/( sx*sx/N -sxx)
    b = (sy - a*sx)/N
    r = abs(sy*sx/N-sxy)/math.sqrt((sxx-sx*sx/N)*(syy-sy*sy/N))
    return a,b,r

def fit_line(X,Y):
    a, b, r = linefit(X, Y)
    print("X=", X)
    print("Y=", Y)
    print("拟合结果: y = %10.5f x + %10.5f , r=%10.5f" % (a, b, r))
    return a,b,r
def fit_y(a,b,r,y):
    x=(y-b)/a
    return x


# encoding=utf-8
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.ticker import FuncFormatter
from mpl_toolkits.axisartist.axislines import SubplotZero
from pylab import *
mpl.rcParams['font.sans-serif'] = ['SimHei']

ax = gca()
ax.spines['right'].set_color('none')

plt.ylabel(u'A/%',fontproperties='SimHei')#也可以直接显示中文。
plt.xlabel(u'B/(ug/mL)',fontproperties='SimHei')

legends={'X':'^','Y':'s','Z':'p'}
plt.tick_params(top='on')

x0=    [7.8125, 15.625, 31.25, 62.5, 125.0]
y0 =  [28.04, 28.54, 37.47, 70.35, 98.64]
a,b,r=fit_line(x0,y0)
y=50
print("y=50","x=%f"%fit_y(a,b,r,y))
plt.plot(x0, y0, lw=1, c='k', marker='^',  label='Y1')  # 绘制y1
x1=    [1.931, 3.862, 7.723, 15.447, 30.894]
y1 =  [38.21, 51.32, 68.73, 80.71, 90.16]
a,b,r=fit_line(x1,y1)
y=50
print("y=50","x=%f"%fit_y(a,b,r,y))
plt.plot(x1, y1, lw=1, c='k', marker='s', label='Y2')  # 绘制y2

x2= [1.693, 3.386, 6.772, 13.544, 27.088]
y2= [45.65, 65.95, 74.53, 85.88, 91.8]
a,b,r=fit_line(x2,y2)
y=50
print("y=50","x=%f"%fit_y(a,b,r,y))
plt.plot(x2, y2, lw=1, c='k', marker='p', label='Y2')  # 绘制y2
plt.legend(legends.keys(),loc=2)
plt.savefig("filename.png")
plt.show()


############################################


# encoding=utf-8
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.ticker import FuncFormatter
from mpl_toolkits.axisartist.axislines import SubplotZero
from pylab import *
mpl.rcParams['font.sans-serif'] = ['SimHei']

ax = gca()
ax.spines['right'].set_color('none')

plt.ylabel(u'AA/%',fontproperties='SimHei')#也可以直接显示中文。
plt.xlabel(u'BB/(ug/mL)',fontproperties='SimHei')

legends={'XX':'^','YY':'*','ZZ':'s','WW':'p'}
plt.tick_params(top='on')




x0=    [0.5,1,2,3,4,5]
y0 =  [23.90,32.20,45.37,63.90,78.54,80.00]
a,b,r=fit_line(x0,y0)

y=50
print("y=50","x=%f"%fit_y(a,b,r,y))
plt.plot(x0, y0, lw=1, c='k', marker='^',  label='Y1')  # 绘制y1


x1=    [0.5,1,2,3,4,5]
y1 =  [51.15,55.30,63.59,75.12,82.03,84.79]
a,b,r=fit_line(x1,y1)
y=50
print("y=50","x=%f"%fit_y(a,b,r,y))
plt.plot(x1, y1, lw=1, c='k', marker='*', label='Y2')  # 绘制y2



x2=    [0.5,1,2,3,4,5]
y2 =  [5.76,11.52,37.86,43.62,44.86,54.32]
a,b,r=fit_line(x2,y2)
y=50
print("y=50","x=%f"%fit_y(a,b,r,y))
plt.plot(x2, y2, lw=1, c='k', marker='s', label='Y2')  # 绘制y2

x3=  [0.5,1,2,3,4,5]
y3= [25.12,38.60,48.37,56.28,66.05,81.40]
a,b,r=fit_line(x3,y3)
y=50
print("y=50","x=%f"%fit_y(a,b,r,y))
plt.plot(x3, y3, lw=1, c='k', marker='p', label='Y2')  # 绘制y2
plt.legend(legends.keys(),loc=2)
plt.savefig("filename2.png")
plt.show()
##################################################################


# encoding=utf-8
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.ticker import FuncFormatter
from mpl_toolkits.axisartist.axislines import SubplotZero
from pylab import *
mpl.rcParams['font.sans-serif'] = ['SimHei']

ax = gca()
ax.spines['right'].set_color('none')

plt.ylabel(u'AAA/%',fontproperties='SimHei')#也可以直接显示中文。
plt.xlabel(u'BBB/(ug/mL)',fontproperties='SimHei')

legends={'XXX':'^','YYY':'*','ZZZ':'s','WWW':'p'}
plt.tick_params(top='on')

x0=    [0.91, 1.82, 3.64, 5.45,7.27,9.09]
y0 =  [17.69,28.38,35.37,53.28,64.85,82.31]
a,b,r=fit_line(x0,y0)
y=50
print("y=50","x=%f"%fit_y(a,b,r,y))
plt.plot(x0, y0, lw=1, c='k', marker='^',  label='Y1')  # 绘制y1


x1=     [9.09,18.18,36.36,54.55,72.73,90.91]
y1 =  [10.35,19.76,32.71,44.24,54.35,60.71]
a,b,r=fit_line(x1,y1)
y=50
print("y=50","x=%f"%fit_y(a,b,r,y))
plt.plot(x1, y1, lw=1, c='k', marker='*', label='Y2')  # 绘制y2

x2=     [18.28,36.57,54.85,73.13,91.41,109.7]
y2 =  [23.17,42.59,52.19,60.96,67.64,73.28]
a,b,r=fit_line(x2,y2)
y=50
print("y=50","x=%f"%fit_y(a,b,r,y))
plt.plot(x2, y2, lw=1, c='k', marker='s', label='Y2')  # 绘制y2


x3=  [9.2,18.41,36.82,55.23,73.64,92.05]
y3= [20.71,31.29,55.06,73.18,84.00,86.59]
a,b,r=fit_line(x3,y3)
y=50
print("y=50","x=%f"%fit_y(a,b,r,y))
plt.plot(x3, y3, lw=1, c='k', marker='p', label='Y2')  # 绘制y2

plt.legend(legends.keys(),loc=2)

plt.savefig("filename3.png")
plt.show()

C:\python3.6\python.exe F:/project12/make_plot.py
X= [7.8125, 15.625, 31.25, 62.5, 125.0]
Y= [28.04, 28.54, 37.47, 70.35, 98.64]
拟合结果: y =    0.64103 x +   21.55833 , r=   0.98509
y=50 x=44.369018
X= [1.931, 3.862, 7.723, 15.447, 30.894]
Y= [38.21, 51.32, 68.73, 80.71, 90.16]
拟合结果: y =    1.61287 x +   46.51764 , r=   0.89661
y=50 x=2.159100
X= [1.693, 3.386, 6.772, 13.544, 27.088]
Y= [45.65, 65.95, 74.53, 85.88, 91.8]
拟合结果: y =    1.49210 x +   57.10000 , r=   0.84843
y=50 x=-4.758387
X= [0.5, 1, 2, 3, 4, 5]
Y= [23.9, 32.2, 45.37, 63.9, 78.54, 80.0]
拟合结果: y =   13.41255 x +   19.33592 , r=   0.98290
y=50 x=2.286223
X= [0.5, 1, 2, 3, 4, 5]
Y= [51.15, 55.3, 63.59, 75.12, 82.03, 84.79]
拟合结果: y =    7.96953 x +   48.07537 , r=   0.98704
y=50 x=0.241498
X= [0.5, 1, 2, 3, 4, 5]
Y= [5.76, 11.52, 37.86, 43.62, 44.86, 54.32]
拟合结果: y =   10.56493 x +    5.69726 , r=   0.93662
y=50 x=4.193377
X= [0.5, 1, 2, 3, 4, 5]
Y= [25.12, 38.6, 48.37, 56.28, 66.05, 81.4]
拟合结果: y =   11.31430 x +   23.40805 , r=   0.98838
y=50 x=2.350295
X= [0.91, 1.82, 3.64, 5.45, 7.27, 9.09]
Y= [17.69, 28.38, 35.37, 53.28, 64.85, 82.31]
拟合结果: y =    7.61891 x +   11.19652 , r=   0.99479
y=50 x=5.093049
X= [9.09, 18.18, 36.36, 54.55, 72.73, 90.91]
Y= [10.35, 19.76, 32.71, 44.24, 54.35, 60.71]
拟合结果: y =    0.61554 x +    8.10807 , r=   0.99059
y=50 x=68.057164
X= [18.28, 36.57, 54.85, 73.13, 91.41, 109.7]
Y= [23.17, 42.59, 52.19, 60.96, 67.64, 73.28]
拟合结果: y =    0.52270 x +   19.85775 , r=   0.97324
y=50 x=57.667001
X= [9.2, 18.41, 36.82, 55.23, 73.64, 92.05]
Y= [20.71, 31.29, 55.06, 73.18, 84.0, 86.59]
拟合结果: y =    0.83553 x +   18.73512 , r=   0.96869
y=50 x=37.419095

Process finished with exit code 0

猜你喜欢

转载自blog.csdn.net/sxj731533730/article/details/106597914
今日推荐