April 12, 2024, 3:15 a.m. | drake

DEV Community dev.to


def gen_graph(self, days):
"""
根据历史数据绘制折线图并且保存
"""
gas_list = self.get_histroy_gas(days)
if gas_list:
x = []
y = []
for index, gas in enumerate(gas_list):
x.append(index+1)
y.append(gas)
# 计算变化百分比
percentage_change = [None] # 第一个点的变化百分比设为None
for i in range(1, len(y)):
change = ((y[i] - y[i - 1]) / y[i - 1]) * 100
percentage_change.append(change)

# 生成折线图
plt.plot(x, y)
plt.title(f'{len(gas_list)} days')
plt.xlabel('day')
plt.ylabel('GAS')

# 在每个坐标点上显示Y轴的值和变化百分比
for i in range(len(x)):
if percentage_change[i] is not None:
plt.annotate(f'{y[i]} ({percentage_change[i]:.2f}%)', (x[i], y[i]), textcoords="offset points",
xytext=(0, 10), ha='center')
else:
plt.annotate(f'{y[i]}', …

change index matplotlib plot python

AI Research Scientist

@ Vara | Berlin, Germany and Remote

Data Architect

@ University of Texas at Austin | Austin, TX

Data ETL Engineer

@ University of Texas at Austin | Austin, TX

Lead GNSS Data Scientist

@ Lurra Systems | Melbourne

Senior Machine Learning Engineer (MLOps)

@ Promaton | Remote, Europe

Senior Software Engineer, Generative AI (C++)

@ SoundHound Inc. | Toronto, Canada