Home > Blockchain >  Advance year problem appear when plotting (pandas && matplotlib)
Advance year problem appear when plotting (pandas && matplotlib)

Time:02-11

My problem is when I plot the users joining by day the advance year appear, it should not have year 2023. I tried to search it into my csv file and there is no row holding the value of 2023.

data = pd.read_csv('users-current.csv')

#transform datetime to date
data['dateCreated'] = pd.to_datetime(data['created_on']).dt.date

#date Count Registered
dataCreated = data.groupby('dateCreated').size()
#dataCreatedArray = np.array([dataCreated], dtype = object)
dataCreated.head(50)

dataCreated.plot().invert_xaxis()
plt.title('Users Joining in a Day',pad=20, fontdict={'fontsize':24})
plt.show()

the output:

enter image description here

column in my csv used below:

enter image description here

CodePudding user response:

This is because the range of x is automatically generated. Instead, you can explicitly limit a range of x using plt.xlim(), as follows:

import pandas as pd
import matplotlib.pyplot as plt
import datetime

data = pd.read_csv('users-current.csv')

#transform datetime to date
data['dateCreated'] = pd.to_datetime(data['created_on']).dt.date

#date Count Registered
dataCreated = data.groupby('dateCreated').size()
#dataCreatedArray = np.array([dataCreated], dtype = object)
dataCreated.head(50)

dataCreated.plot().invert_xaxis()

# import datetime, and use this code to set a period as you want.
plt.xlim([datetime.date(2021, 1, 1), datetime.date(2022, 12, 31)]) 

plt.title('Users Joining in a Day', pad=20, fontdict={'fontsize':24})
plt.show()
  • Related