matplotlib绘图实例

实例1:

需求: 一元线性回归+描点绘制+方程输出

效果:

KIl71f.png

code:

# encoding = utf-8
import numpy as np
import matplotlib.pyplot as plt
from sklearn import linear_model

plt.rcParams['font.sans-serif'] = ['SimHei']  # 用来正常显示中文标签
plt.rcParams['axes.unicode_minus'] = False  # 用来正常显示负号

#n=int(input())

x = [[8.214], [7.408], [6.879], [5.490], [5.196]]
y = [[-1.756], [-1.442], [-1.194], [-0.606], [-0.476]]

# for i in range (1, n):
#     print('x' ,i, " = ")
#     tmp = float(input())
#     x.append(tmp)
   
# for i in range (1, n):
#     print('x\y' ,i, " = ")
#     tmp = float(input())
#     y.append(tmp)

plt.title('$U_0-v$')
plt.xlabel(r'频率$v/10^{14} HZ$')
plt.ylabel(r'截止电压$U_0/1 Volt$')
plt.grid(True)
plt.plot(x, y, 'r^')
regr = linear_model.LinearRegression();
regr.fit(x, y)
print(type(float(regr.coef_)))
#plt.text(7.5, 1.2, r'y='.join(str(float(regr.coef_))).join('x+').join(str(float(regr.intercept_))).join('$'))
plt.plot(x, regr.predict(x), 'g--', label = '$y = '+ str(float(regr.coef_))+"x +" +str(float(regr.intercept_))+'$')
plt.legend()
plt.show()
print(x), print(y)

实例2:

需求: 曲线描点

效果:

KI1kB4.png

code:

# encoding = utf-8
import numpy as np
import matplotlib.pyplot as plt
from sklearn import linear_model

plt.rcParams['font.sans-serif'] = ['SimHei']  # 用来正常显示中文标签
plt.rcParams['axes.unicode_minus'] = False  # 用来正常显示负号

#n=int(input())

x = [list(range(-1, 49, 2))]
x = np.array(x);
x = x.reshape(25, 1)
print(x)
y = [0.1, 5.1, 10.5, 14.6, 17.1, 19.2, 20.9, 22.4, 23.6, 24.7, 25.5, 26.3, 26.8, 27.2, 27.6, 27.9, 28.3, 28.9, 29.2, 29.6, 29.9, 30.2, 30.7, 31.2, 31.5]
y = np.array(y);

#y = y.reshape(25, 1)

# for i in range (1, n):
#     print('x' ,i, " = ")
#     tmp = float(input())
#     x.append(tmp)

# for i in range (1, n):
#     print('x\y' ,i, " = ")
#     tmp = float(input())
#     y.append(tmp)

plt.title('伏安特性数据')
plt.xlabel(r'$U_{AK} / V$')
plt.ylabel(r'$I / 10^{-10} A$')
plt.grid(True)
plt.plot(x, y, 'r--',label = r'伏安特性曲线')
plt.plot(x, y, 'k^')
# regr = linear_model.LinearRegression();
# regr.fit(x, y)
# print(type(float(regr.coef_)))
#plt.text(7.5, 1.2, r'y='.join(str(float(regr.coef_))).join('x+').join(str(float(regr.intercept_))).join('$'))
#plt.plot(x, regr.predict(x), 'g--', label = '$y = '+ str(float(regr.coef_))+"x +" +str(float(regr.intercept_))+'$')
plt.legend()
plt.show()
#print(x), print(y)

实例3 简单描点:

效果:

KI1U8P.png

KI1dv8.png

两个差不多的,放第一个代码

# encoding = utf-8
import numpy as np
import matplotlib.pyplot as plt
from sklearn import linear_model

plt.rcParams['font.sans-serif'] = ['SimHei']  # 用来正常显示中文标签
plt.rcParams['axes.unicode_minus'] = False  # 用来正常显示负号

#n=int(input())

x = [2, 4, 8]
x = np.array(x);

print(x)
y = [7.9, 31.5, 110.1]
y = np.array(y);

#y = y.reshape(25, 1)

# for i in range (1, n):
#     print('x' ,i, " = ")
#     tmp = float(input())
#     x.append(tmp)

# for i in range (1, n):
#     print('x\y' ,i, " = ")
#     tmp = float(input())
#     y.append(tmp)

plt.title('$I_m - \phi$')
plt.xlabel(r'光阑孔径$\phi / mm$')
plt.ylabel(r'$I / 10^{-10} A$')
plt.grid(True)
plt.plot(x, y, 'r--',label = r'$I_m - \phi$数据曲线')
plt.plot(x, y, 'k^')
# regr = linear_model.LinearRegression();
# regr.fit(x, y)
# print(type(float(regr.coef_)))
#plt.text(7.5, 1.2, r'y='.join(str(float(regr.coef_))).join('x+').join(str(float(regr.intercept_))).join('$'))
#plt.plot(x, regr.predict(x), 'g--', label = '$y = '+ str(float(regr.coef_))+"x +" +str(float(regr.intercept_))+'$')
plt.legend()
plt.show()
#print(x), print(y)
egr.predict(x), 'g--', label = '$y = '+ str(float(regr.coef_))+"x +" +str(float(regr.intercept_))+'$')
plt.legend()
plt.show()
#print(x), print(y)
  • 4
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值