大学物理实验绘图——光电效应

第一次写博客,更多是一种记录,写的不是很好。时间有限,没有过加以解释,请多多谅解。

U-v图

import numpy as np
import matplotlib.pyplot as plt

# 设置字体
plt.rcParams['font.family'] = 'Microsoft YaHei'

# 设置颜色代码
color1 = "#038355"  # 孔雀绿
color2 = "#ffc34e"  # 向日黄

# 示例散点数据
x = [8.214, 7.408, 6.879, 5.490, 5.196]
y = [-1.907, -1.549, -1.338, -0.873, -0.660]

# 使用numpy的polyfit函数进行线性拟合
coefficients = np.polyfit(x, y, 1)

# 获取拟合得到的斜率和截距
slope, intercept = coefficients

# 计算拟合直线上的点
fit_x = np.linspace(min(x), max(x), 100)
fit_y = slope * fit_x + intercept

# 绘制散点图和拟合直线,使用指定的颜色代码
plt.scatter(x, y, label='散点数据', color=color1)
plt.plot(fit_x, fit_y, label=f'拟合直线: y = {slope:.2f}x + {intercept:.2f}', color=color2)
plt.plot([min(x), max(x)], [slope * min(x) + intercept, slope * max(x) + intercept], '--', color=color2)
plt.xlabel('v')
plt.ylabel('$U_0$')
plt.legend()
plt.title('$U_0$-v图')
plt.savefig(r"C:\Users\86131\Desktop\线性拟合u-v.png", dpi=300)
plt.show()

# 输出拟合直线的斜率
print(f'拟合直线的斜率为: {slope:.2f}')

电压-电流关系图

import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
from scipy.optimize import curve_fit
import seaborn as sns

# 设置Seaborn的背景样式
sns.set_style("whitegrid")

# 读取Excel数据
excel_file = r"C:\Users\86131\Desktop\工作簿1.xlsx"
df = pd.read_excel(excel_file)

# 提取电压和电流数据
voltage1, current1 = df['Voltage1'], df['Current1']
voltage2, current2 = df['Voltage2'], df['Current2']
voltage3, current3 = df['Voltage3'], df['Current3']
voltage4, current4 = df['Voltage4'], df['Current4']

# 设置字体
plt.rcParams['font.family'] = 'Microsoft YaHei'

# 定义拟合函数,例如二次多项式
def quadratic_function(x, a, b, c):
    return a * x**2 + b * x + c

# 拟合数据
params1, _ = curve_fit(quadratic_function, voltage1, current1)
params2, _ = curve_fit(quadratic_function, voltage2, current2)
params3, _ = curve_fit(quadratic_function, voltage3, current3)
params4, _ = curve_fit(quadratic_function, voltage4, current4)

# 生成拟合曲线上的点
fit_voltage = np.linspace(min(voltage1.min(), voltage2.min(), voltage3.min(), voltage4.min()),
                          max(voltage1.max(), voltage2.max(), voltage3.max(), voltage4.max()), 100)

fit_current1 = quadratic_function(fit_voltage, *params1)
fit_current2 = quadratic_function(fit_voltage, *params2)
fit_current3 = quadratic_function(fit_voltage, *params3)
fit_current4 = quadratic_function(fit_voltage, *params4)

# 绘制原始散点和拟合曲线,调整颜色和大小
plt.scatter(voltage1, current1, label='φ=8, 365nm', marker='o', color='#FD6D5A', s=17)
plt.scatter(voltage2, current2, label='φ=8, 436nm', marker='o', color='#FEB40B', s=17)
plt.scatter(voltage3, current3, label='φ=4, 365nm', marker='o', color='#6DC354', s=17)
plt.scatter(voltage4, current4, label='φ=4, 436nm', marker='o', color='#994487', s=17)

plt.plot(fit_voltage, fit_current1,  linestyle='-', color='#FD6D5A')
plt.plot(fit_voltage, fit_current2,  linestyle='-', color='#FEB40B')
plt.plot(fit_voltage, fit_current3,  linestyle='-', color='#6DC354')
plt.plot(fit_voltage, fit_current4,  linestyle='-', color='#994487')

# 添加坐标轴
plt.axhline(0, color='black', linewidth=0.5)
plt.axvline(0, color='black', linewidth=0.5)

# 设置坐标轴样式
for spine in plt.gca().spines.values():
    spine.set_edgecolor("#CCCCCC")
    spine.set_linewidth(1.5)

# 设置坐标轴的背景色
ax = plt.gca()
ax.set_facecolor("#F5F5F5")

# 设置整个图的背景色
plt.gca().set_facecolor("#F5F5F5")

plt.xlabel('电压')
plt.ylabel('电流',rotation=0,labelpad=15)
plt.legend()
plt.title('电流-电压关系')

# 移动坐标轴到原点
ax.spines['left'].set_position('zero')
ax.spines['bottom'].set_position('zero')
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
plt.savefig(r"C:\Users\86131\Desktop\I-Uu-v.png", dpi=300)
plt.show()

输入数据,电流单位为A/10^9,电压单位v

最后输出效果为:

  • 4
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值