Time Series Plot Pandas Error

i have a dataset like :

Date        Value
2017-11-08  6
2017-11-10  5
2017-11-16  3
2017-11-13  5
2017-11-06  6
2017-10-25  5
2017-10-31  1
2017-10-30  3
2017-10-13  6
2017-11-17  4
2017-10-22  2

i am trying to plot a simple line graph, using matplotlib

plt.plot(df['Date'],df['Value'])

it is showing a very weird graph: Graph Image

how to do it the right way?

second question : how to get a range of dates and plot them ?

my df.info()

Data columns (total 2 columns):
Date          272 non-null datetime64[ns]
Value         272 non-null int64
dtypes: datetime64[ns](1), int64(1)

Upvotes: 0

Views: 264

Answers (1)

jezrael
jezrael

Reputation: 862431

I suggest use sort_values with DataFrame.plot:

df.sort_values('Date').plot(x='Date', y='Value')

EDIT:

Filter by between and boolean indexing:

df1 = df[df['Date'].between('2017-10-10','2017-10-31')]
print (df1)
         Date  Value
5  2017-10-25      5
6  2017-10-31      1
7  2017-10-30      3
8  2017-10-13      6
10 2017-10-22      2

df1.sort_values('Date').plot(x='Date', y='Value')

Upvotes: 2

Related Questions