用matplotlib画简单折线图示例

例1

import numpy as np
import matplotlib.pyplot as plt
from scipy import stats
rx1 = np.array([54.52, 55.14, 55.80, 56.43, 57.08, 57.71, 58.35, 58.97, 59.61, 60.25]) #纵坐标
t1 = np.linspace(20.5, 47.5, 10) #横坐标
a = stats.linregress(t1, rx1) #求线性回归方程
k = a[0] #斜率
b = a[1] #截距
plt.rcParams['font.sans-serif'] = ['SimHei'] #使中文能正常显示
plt.rcParams['axes.unicode_minus'] = False #使符号能正常显示
plt.rcParams['font.size'] = 16 #改变字体大小
plt.figure(figsize=(12, 6)) #改变图片大小
plt.grid(True) #显示网格
plt.plot(t1, rx1, 'k-o') #画图:穿过点的折线
plt.title(u'铜电阻阻值与温度曲线图(惠斯通电桥)') #标题
plt.xlabel('t(℃)') #横坐标说明
plt.ylabel('$R_X$(Ω)') #纵坐标说明(一对$之间是Tex表达式)
for  i in zip(t1, rx1):
    plt.text(i[0], i[1], str(i[1]), ha='right', va='bottom') #给点加上数据
plt.show() #显示图片

例2

import numpy as np
from scipy import stats
from scipy import interpolate
import matplotlib.pyplot as plt

def xxdz():
    u1 = np.array([0.23, 0.5, 0.75, 1.01, 1.25, 1.51, 1.75])
    i1 = np.array([2.2, 4.6, 6.8, 9.7, 12.0, 14.7, 17.0])
    k1, b1, *_ = stats.linregress(u1, i1)
    x1 = np.linspace(0, max(u1), 1000)
    u2 = np.array([0.23, 0.5, 0.75, 1, 1.26, 1.51, 1.75])
    i2 = np.array([2.2, 4.8, 7.8, 10.0, 12.8, 15.5, 18.0])
    k2, b2, *_ = stats.linregress(u2, i2)
    x2 = np.linspace(0, max(u2), 1000)
    plt.scatter(u1, i1, c='k', marker='^', label='内接法') #画点
    plt.scatter(u2, i2, c='k', marker='o', label='外接法')
    plt.plot(x1, k1 * x1 + b1, 'k', label='内接法')
    plt.plot(x2, k2 * x2 + b2, 'k--', label = '外接法')
    plt.title('测量线性电阻的伏安特性')
    plt.xticks(np.linspace(0, 1.8, 10)) #设置坐标轴的刻度
    plt.yticks(np.linspace(0, 20, 11))
    plt.xlabel('U/V')
    plt.ylabel('I/mA')
    plt.legend()

def bdtejg():
    u1 = np.array([0.238, 0.426, 0.670, 0.740, 0.782, 0.810, 0.830, 0.852, 0.874, 0.885])
    i1 = np.array([0.0, 0.0, 0.1, 1.2, 4.5, 9.0, 13.2, 18.8, 24.8, 28.5])
    u2 = -np.array([2.5, 4.02, 4.1, 4.22, 4.51, 4.7, 4.76, 4.8, 4.84, 4.87])
    i2 = -np.array([0.00, 0.1, 0.18, 0.28, 0.85, 2.03, 3.5, 5.88, 8.33, 12.18])
    u3 = np.concatenate((u2, u1)) #对两组数据进行连接
    i3 = np.concatenate((i2, i1))
    f = interpolate.interp1d(u3, i3, kind='cubic') #获得三次方插值函数以平滑曲线
    xnew = np.linspace(min(u3), max(u3), 1000); #平滑曲线时用到的经细分后的x坐标
    plt.plot(xnew, f(xnew), 'k')
    plt.scatter(u3, i3, c='k', marker='o')
    plt.title('半导体二极管2CW52的正反向伏安特性曲线')
    plt.xticks(np.linspace(-5, 1, 11))
    plt.yticks(np.linspace(-15, 30, 10))
    plt.xlabel('U/V')
    plt.ylabel('I/mA')
    
def jtsjg():
    u1 = np.array([0.00, 0.1, 0.26, 0.52, 0.9, 1.5, 2.24, 2.76, 3.38, 4.00, 4.54, 5.0])
    i1 = np.array([0.0, 5.09, 9.25, 9.4, 9.51, 9.6, 9.8, 9.91, 10.05, 10.09, 10.35, 10.4])
    f1 = interpolate.interp1d(u1, i1, kind='linear') #获得线性(更高次会过拟合)插值函数以平滑曲线
    u2 = np.array([0.0, 0.1, 0.15, 0.24, 0.4, 1.22, 1.9, 2.4, 3.2, 3.93, 4.51, 5.00])
    i2 = np.array([0.0, 6.8, 11.6, 13.2, 13.8, 14.1, 14.4, 14.7, 15.0, 15.3, 15.6, 15.8])
    f2 = interpolate.interp1d(u2, i2, kind='linear')
    u3 = np.array([0.0, 0.05, 0.1, 0.16, 0.55, 1.1, 1.85, 2.43, 2.87, 3.3, 3.8, 5])
    i3 = np.array([0.0, 4.4, 10.3, 14.7, 18.3, 18.6, 19.3, 19.7, 20, 20.3, 20.6, 21])
    f3 = interpolate.interp1d(u3, i3, kind='linear')
    xnew = np.linspace(0, 5, 1000)
    plt.plot(xnew, f1(xnew), 'k', label='40μA')
    plt.plot(xnew, f2(xnew), 'k--', label='60μA')
    plt.plot(xnew, f3(xnew), 'k-.', label='80μA')
    plt.scatter(u1, i1, c='k', marker='o')
    plt.scatter(u2, i2, c='k', marker='o')
    plt.scatter(u3, i3, c='k', marker='o')
    plt.title('晶体三极管的输出特性曲线')
    plt.xticks(np.linspace(0, 5, 11))
    plt.yticks(np.linspace(0, 22, 12))
    plt.xlabel('$U_{ce}$/V')
    plt.ylabel('$I_c$/mA')
    plt.legend()
    print(f1(3.5))
    print(f2(3.5))
    print(f3(3.5))

plt.rcParams['font.sans-serif'] = ['SimHei'] #使中文能正常显示
plt.rcParams['axes.unicode_minus'] = False #使符号能正常显示
plt.rcParams['font.size'] = 16 #改变字体大小
plt.figure(figsize=(12, 6)) #改变图片大小
plt.grid(True) #显示网格
ax = plt.gca() #获得坐标轴
ax.spines['right'].set_color('none') #隐藏右边框和上边框
ax.spines['top'].set_color('none')
ax.spines['bottom'].set_position(('data', 0)) #把坐标轴移到(0, 0)
ax.spines['left'].set_position(('data', 0))
jtsjg()
plt.show()
posted @ 2019-03-05 17:22  YuanZiming  阅读(595)  评论(0编辑  收藏  举报