ReportGeneration/PythonReportGeneration/utils/chart_generation.py

62 lines
2.2 KiB
Python

import matplotlib.pyplot as plt
import numpy as np
def plot_curves_and_function(curves, function, file_path):
# 创建图表和轴
fig, ax = plt.subplots(figsize=(10, 8))
# 设置x轴和y轴的对数刻度
ax.set_xscale('log')
ax.set_yscale('log')
# 设置网格
ax.grid(True, which="both", ls="--", linewidth=0.5)
# 生成函数曲线
I_values = np.logspace(1, 4, 100) # 生成从 10 到 10000 的等比数据点
t_values = function(I_values)
# 把t_values中的负值变为not a number
t_values = np.where(t_values > 0, t_values, np.nan)
# 绘制曲线
ax.plot(I_values, t_values, label='Function Curve', color='black', linestyle='--')
ax.annotate('Function Curve', xy=(I_values[-1], t_values[-1]), textcoords="offset points", xytext=(-20, 10), ha='center')
for label, data in curves.items():
ax.plot(data['x'], data['y'], label=label, color=data['color'])
ax.annotate(label, xy=(data['x'][-1], data['y'][-1]), textcoords="offset points", xytext=(10, -10), ha='center')
# 设置标签和标题
ax.set_xlabel('Axis-X', fontsize=12)
ax.set_ylabel('Axis-Y', fontsize=12)
ax.set_title('Example of a logarithmic graph', fontsize=14)
# 设置轴限
ax.set_xlim([5, 10000])
ax.set_ylim([0.01, 1000])
# 保存图表
plt.savefig(file_path)
# plt.show()
if __name__ == "__main__":
curves = {
'Curve0': {'x': [10, 30, 100, 1000, 3000], 'y': [1000, 100, 10, 1, 0.1], 'color': 'blue'},
'Curve1': {'x': [10, 30, 100, 1000, 3000], 'y': [1000, 300, 30, 3, 0.3], 'color': 'green'},
'Curve2': {'x': [10, 30, 100, 1000, 3000], 'y': [1000, 200, 20, 2, 0.2], 'color': 'purple'},
'Curve3': {'x': [15, 40, 150, 1500, 5000], 'y': [500, 50, 5, 0.5, 0.05], 'color': 'orange'},
'Curve4': {'x': [10, 30, 100, 1000, 3000], 'y': [800, 80, 8, 0.8, 0.08], 'color': 'red'},
'Curve5': {'x': [20, 60, 200, 2000, 6000], 'y': [1000, 150, 15, 1.5, 0.15], 'color': 'brown'}
}
Tp = 1
I_op = 100
# 定义函数表达式
def function_expression(I_values):
epsilon = 1e-10
return 80 * Tp / (((I_values / I_op) ** 2 - 1) + epsilon)
plot_curves_and_function(curves, function_expression, file_path="log_chart.png")