我需要為只有最大值和最小值的點顯示文本,以便我可以避免文本重疊,如圖所示。

這是代碼
x = df['Measurement Date']
y = df['SYS']
plt.plot(x, y)
plt.rcParams['figure.figsize'] = (15, 10)
for x, y in zip(df['Measurement Date'], df['SYS']):
plt.text(x = x, y = y, s = '{:.0f}'.format(y))
plt.show()
uj5u.com熱心網友回復:
這可以通過使用從資料幀中提取的最小值和最大值回圈遍歷資料幀來實作。
import pandas as pd
import matplotlib.pyplot as plt
df = pd.read_csv('./data/bp_log.csv')
min_max = df[(df['SYS'] == df['SYS'].min()) | (df['SYS'] == df['SYS'].max())]
x = df['Measurement Date']
y = df['SYS']
plt.plot(x, y)
plt.rcParams['figure.figsize'] = (15, 10)
#for x, y in zip(df['Measurement Date'], df['SYS']):
for x, y in zip(min_max['Measurement Date'], min_max['SYS']):
plt.text(x = x, y = y, s = '{:.0f}'.format(y))
plt.show()
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/354233.html
標籤:Python matplotlib
上一篇:matplotlib多Y軸熊貓圖
下一篇:在Python中的回圈內回圈
