all AI news
python matplotlib.pyplot 绘制图表图像重叠的问题
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]}', …
More from dev.to / DEV Community
Jobs in AI, ML, Big Data
Data Engineer
@ Lemon.io | Remote: Europe, LATAM, Canada, UK, Asia, Oceania
Artificial Intelligence – Bioinformatic Expert
@ University of Texas Medical Branch | Galveston, TX
Lead Developer (AI)
@ Cere Network | San Francisco, US
Research Engineer
@ Allora Labs | Remote
Ecosystem Manager
@ Allora Labs | Remote
Founding AI Engineer, Agents
@ Occam AI | New York