Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何根据数据自动更改图表标题_Python_Regression_Seaborn - Fatal编程技术网

Python 如何根据数据自动更改图表标题

Python 如何根据数据自动更改图表标题,python,regression,seaborn,Python,Regression,Seaborn,我有创建seaborn regplots的代码。 我的目标是每次在特定日期和时间运行此代码。 问题是每次运行此绘图时,我都需要更改标题中的日期和小时。我想自动完成这项工作,我想使用格式,但不知道如何在字符串中完成 这是我创建图表的方式: #Filter/Create the database based on the dat and the hour # the date and hour for the title should come from here byDH=df_NDVIall[

我有创建seaborn regplots的代码。 我的目标是每次在特定日期和时间运行此代码。 问题是每次运行此绘图时,我都需要更改标题中的日期和小时。我想自动完成这项工作,我想使用格式,但不知道如何在字符串中完成

这是我创建图表的方式:

#Filter/Create the database based on the dat and the hour
# the date and hour for the title should come from here

byDH=df_NDVIall[(df_NDVIall.index.get_level_values('date')=='6/23/2019')&(df_NDVIall.index.get_level_values('hour') == '12:00')]['NDVI']
NITbyDH=df_Nit[['plant','nitrogen']]


merged_data=pd.merge(byDH.to_frame(),NITbyDH,how='inner',on='plant')
merged_data.head(18)

import seaborn as sns

#Define X and Y
x = merged_data['NDVI']
y = merged_data['nitrogen']

#The function
def give_me_scatter(x, y, title, xlabel, ylabel):
    slope, intercept, r_value, p_value, std_err = linregress(x, y)
    #print('slope:',slope)
    #print('intercept:',intercept)
    #print('R:',r_value)
    #print('R^2:',(r_value**2))  

    # use line_kws to set line label for legend
    plt.figure(figsize=(7.5,6.5))
    ax = sns.regplot(x='NDVI', y='nitrogen', data=merged_data, color='b', line_kws={'label':"y={0:.1f}x+{1:.1f}".format(slope,intercept)})
    ax = sns.regplot(x='NDVI', y='nitrogen', data=merged_data, color='b', line_kws={'label':"R^2={0:.3}".format(r_value**2)})
    ax.set_title('NDVI vs Nitrogen% ')

    # plot legend
    ax.legend()
    plt.show()

    # plot legend

    ax.legend()
    plt.show()

#get scatter plot

give_me_scatter(x, y, 'NDVI vs Nitrogen ', 'NDVI', 'Nitrogen %')

*我无法共享我的数据库


*我的最终目标是自动将日期和小时添加到标题中,而无需手动更改标题

您可以使用名为datetime的软件包。然后,您可以执行以下操作以获取当前日期:

from datetime import date
today = date.today()
当然,你可以按照你想要的方式格式化它。 下面是更多的信息和示例:


因此,您只需创建一个变量并为其指定格式化的日期/时间。

如果您将日期和时间放入变量中,则可以使用这些变量写入字符串以用作标题(您最初没有在函数中使用该标题)

date_to_plot = '6/23/2019'
time_to_plot = '12:00'

byDH=df_NDVIall[(df_NDVIall.index.get_level_values('date')==date_to_plot)&(df_NDVIall.index.get_level_values('hour') == time_to_plot)]['NDVI']
(...)

def give_me_scatter(x, y, title, xlabel, ylabel):
    (...)
    ax.set_title(title)
    (...)

#get scatter plot
give_me_scatter(x, y, 'NDVI vs Nitrogen on {} {}'.format(date_to_plot, time_to_plot), 'NDVI', 'Nitrogen %')