我有以下函数来绘制图形:
def plot_ATD(DataFrame):
#Initialise 225V ATD plot
fig = plt.figure()
ax = fig.add_subplot(111)
#take columns from data set and make to list which is passed to matplotlib to plot a graph
x = DataFrame['Arrival Time (ms)'].tolist()
y = DataFrame['Intensity'].tolist()
line, = ax.plot(x,y, 'r-')
#use numpy to get the max of Intensity, then determine the corresponding arrival time
ymax = np.max(y)
xpos = y.index(ymax)
xmax = x[xpos]
time = xmax
#add an annotation point at the maxima giving the arrival time at this position
# ax.annotate(s=text of annotation, xy=point to annotate, xytext=position to place text
# arrowprops=dict(facecolor=color of arrow))
ax.annotate(s=xmax, xy=(xmax, ymax), xytext=(xmax+5, ymax+5),
arrowprops=dict(facecolor='orange'),
)
#ax.set_ylim(0,600000)
ax.set_xlim(0,20)
plt.xlabel('Arrival time (ms)')
plt.title(DataFrame.name)
return plt.show()
我在以下pandas数据帧上使用它:
^{pr2}$我希望图形的标题是数据帧的名称,即V100、V125等
我不确定正确的语法或如何做到这一点?请帮忙!在
首先,在函数中使用
DataFrame
作为数据帧的名称不是一个好的做法,因为它是pandas.DataFrame
类本身的名称。例如,最好将其更改为df
。在因此可以使用(例如)设置数据帧的名称
^{1}$对所有的数据帧都这样做。然后在函数调用(新命名)
df.name
中获取先前分配给数据帧的名称。在更新
要自动设置数据帧名称,只需
^{pr2}$这里有一个解决方法:
^{1}$创建变量时,必须使用更干净的方法(因为
^{pr2}$eval
不安全),例如使用系列(或字典):会简化工作。在
相关问题 更多 >
编程相关推荐