将DataFrame变量名称作为字符串传递

DS-V

我具有以下功能来绘制图形:

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()

我在以下熊猫DataFrames上使用它:

V100 = pd.read_csv('Documents/spreadsheets/Data/100V_9z.csv', names=['Arrival Time (ms)', 'Intensity'])
V125 = pd.read_csv('Documents/spreadsheets/Data/125V_9z.csv', names=['Arrival Time (ms)', 'Intensity'])
V150 = pd.read_csv('Documents/spreadsheets/Data/150V_9z.csv', names=['Arrival Time (ms)', 'Intensity'])
V175 = pd.read_csv('Documents/spreadsheets/Data/175V_9z.csv', names=['Arrival Time (ms)', 'Intensity'])
V200 = pd.read_csv('Documents/spreadsheets/Data/200V_9z.csv', names=['Arrival Time (ms)', 'Intensity'])
V225 = pd.read_csv('Documents/spreadsheets/Data/225V_9z.csv', names=['Arrival Time (ms)', 'Intensity'])

我想让图的标题为DataFrame的名称,即V100,V125等。

我不确定语法正确或如何执行此操作?请帮忙!

BM

解决方法:

Vs= [v for v in locals() if v.startswith('V')] 
for v in Vs:
    plot(eval(v),title=v)

eval创建变量(例如,使用系列(或字典))时,必须执行一种更简洁的方法(因为这是不安全的):

ser=pd.Series()
ser['V100'] = pd.read_csv('Documents/spreadsheets/Data/100V_9z.csv', \
names=['Arrival Time (ms)', 'Intensity'])

将简化工作。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

访问json将变量名称作为字符串传递

如何在Java中将接口名称和变量名称作为字符串传递

变量名称作为Javascript中的字符串

如何获取变量名称作为字符串?

将列名称作为字符串传递给with

将按钮名称作为字符串传递

获取PHP变量名称作为许多变量的字符串

使用变量名称作为字符串使用循环设置变量

在python中将变量名称作为原始字符串输入到请求中

反应元素名称作为字符串变量

通过名称作为字符串访问Java变量

获取Swift变量的实际名称作为字符串

将列名称作为字符串传递给group_by并进行汇总

如何将字体名称作为字符串传递给webfont.load

将函数参数名称作为字符串传递

将模型名称作为字符串传递的最佳实践

AngularJS将范围变量名称作为函数参数传递

在作为字符串传递的img src上将传递的导入名称作为prop进行响应

将宏变量名称作为值

将函数参数参数中传递的变量名称打印为字符串(Swift)

将变量名作为字符串获取

将变量名作为字符串获取

将LESS变量名称输出为字符串

变量名称作为宏参数

是否可以在mysql中将表本身的属性名称作为字符串传递?

Dplyr非标准评估,函数名称作为字符串传递

如何使用Java iText在pdf文件中将字体名称作为字符串传递

JavaScript对象:按名称作为字符串访问变量属性

检索未知名称作为字符串值存储在变量中的对象