我试图找出如何在一个图窗口中自动注释最大值。我知道你可以手动输入x,y坐标,用.annotate()
方法注释你想要的任何点,但是我希望注释是自动的,或者自己找到最大点。
这是我目前的代码:
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from pandas import Series, DataFrame
df = pd.read_csv('macrodata.csv') #Read csv file into dataframe
years = df['year'] #Get years column
infl = df['infl'] #Get inflation rate column
fig10 = plt.figure()
win = fig10.add_subplot(1,1,1)
fig10 = plt.plot(years, infl, lw = 2)
fig10 = plt.xlabel("Years")
fig10 = plt.ylabel("Inflation")
fig10 = plt.title("Inflation with Annotations")
如果要绘制的数组是
x
和y
,则可以通过这可以合并到一个函数中,您可以简单地用数据调用它。
我没有
macrodata.csv
的数据。但是,一般来说,假设您有x
和y
轴数据作为列表,您可以使用以下方法获得max
的自动定位。工作代码:
绘图:
像这样的方法会奏效:
相关问题 更多 >
编程相关推荐