2

I have the following dataframe

DATE;NORD
2017-01-01 00:00:00;51.22
2017-01-01 01:00:00;53.0
2017-01-01 02:00:00;52.0
2017-01-01 03:00:00;51.0
2017-01-01 04:00:00;47.27
2017-01-01 05:00:00;45.49
2017-01-01 06:00:00;45.69
2017-01-01 07:00:00;48.07
...

I read it as:

DF = pd.read_csv(fname, sep=';',index_col=0, header = 0)

I would like to plot in the matplolib framework it but selecting between two dates and at the same time I would like to have as x-ticks the date.

I usually use to plot all the dataframe and it is quite easy to get what I want:

ax.plot(DATAF.index.values,DATAF['NORD'].values)

However, I have a problem when I want to plot only between two selected date. In the following an example:

sp = '2017-01-01 00:00:00'
ep = '2017-01-02 00:00:00'

ax.plot(DF.index.values[sp:ep],DF.loc[sp:ep,:].values) 

It does not work. It seems to not have a problem with:

DF.loc[sp:ep,:].values)

but with

DF.index.values[sp:ep]

thanks for any kind of helps or suggestions.

Diego

1 Answer 1

1

You are also there. You can filter then use the DataFrame.plot function:

df.loc[sp:ep].plot(y='NORD')

Or if you prefer using matplotlib directly:

df_plot = df.loc[sp:ep]
ax.plot(df_plot.index, df_plot['NORD'])
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. However, the results unreadable. I have tried with #ax.xaxis.set_major_formatter(dates.DateFormatter(r'\textbf{%Y-%m-%d}')) But it seems not to work.
I get it. I have to convert the index in date. DF.index = pd.to_datetime(DF.index)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.