Click here to Skip to main content
15,881,715 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am new in programming. So, i try to read some files from a folder and plot timeseries. What to do to add y=intercept*x2 in the graph?
df is:
                  values
time                    
2005-01-01           NaN
2005-01-02  3.610269e+15
2005-01-03           NaN
                 ...

2018-12-28           NaN
2018-12-29           NaN
2018-12-30           NaN
2018-12-31  2.833773e+15

[5092 rows x 1 columns]


What I have tried:

Python
fnames = glob.glob('C:/Users/file/*.csv')
for filename in fnames:

    df= pd.read_csv(filename, skiprows=8, parse_dates =["time"], index_col ="time")
 
    df.columns=['values']
    df = df.replace(-1.267651e+30, np.NaN)
    monthly_resampled_data = df.resample('M').mean()
    monthly_resampled_counts = df.resample('M').count()
    
    yearly_resampled_data= monthly_resampled_data.resample('Y').mean()
    yearly_resamped_counts=monthly_resampled_data.resample('Y').count()
    x=yearly_resampled_data.index.year
    x.shape
    

    slope, intercept, r_value, p_value, std_err = stats.linregress(x, yearly_resampled_data.values[:,0])
    print("slope: %f    intercept: %f   r_value:%f   p_value:%f   std_err:%f" % (slope, intercept,r_value, p_value, std_err))
    print("R-squared: %f" % r_value**2)
    
    #how to plot linear in the graph???

    ax=monthly_resampled_data.plot(color='black')

    ax.set_ylim((0.1e16,1.2e16)) 
    plt.xlabel('Year')

 
    plt.show()
    plt.close()
Posted
Updated 7-Jun-19 11:36am
v4
Comments
Richard MacCutchan 7-Jun-19 11:06am    
It rather depends on what you want to show, but I would guess that the X-axis would be the dates, and the Y-axis the values. However, plotting more than 5000 values will take rather a lot of space. It would probably be quicker to load the file into Excel and use the built in graphing tool.

1 solution

99% of the time, the "time dimension" goes along the x-axis (the "independent" variable) and the other ("dependent" variable) along the y-axis; the "numbers" in your case.

In your case, you will probably drop the "NAN" entries.

You would need to "scale" the numerics (by dropping the exponent; for example).

As with $, you chart in thousands, millions, etc. to get the chart "looking right", but still reflective.

And as Richard alluded to, you'll probably divide the x-axis into "quarters" or "years" in order to accomodate all the detail.
 
Share this answer
 
v2
Comments
Member 14476982 9-Jun-19 9:18am    
How do i do that?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900