Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I order dates and show only month+year on the x axis in matplotlib?

I would like to improve my bitcoin dataset but I found that the date is not sorted in the right way and want to show only the month and year. How can I do it?

data = Bitcoin_Historical['Price']
Date1 = Bitcoin_Historical['Date']
train1 = Bitcoin_Historical[['Date','Price']]
#Setting the Date as Index
train2 = train1.set_index('Date')
train2.sort_index(inplace=True)
cols = ['Price']
train2 = train2[cols].apply(lambda x: pd.to_numeric(x.astype(str)
                     .str.replace(',',''), errors='coerce'))
print (type(train2))
print (train2.head())

plt.figure(figsize=(15, 5))
plt.plot(train2)
plt.xlabel('Date', fontsize=12)
plt.xlim(0,20)
plt.ylabel('Price', fontsize=12)
plt.title("Closing price distribution of bitcoin", fontsize=15)
plt.gcf().autofmt_xdate()
plt.show()

The result shows picture below:

result image

It's not ordered and shows all dates. I would like to order by month+year and show only the month name+year. How can that be done?

Example of Data:

result image

Thank you

like image 278
Spatdy Avatar asked Oct 26 '25 08:10

Spatdy


2 Answers

I've made the following edits to your code:

  • converted the column Date column as datetime type
  • cleaned up the Price column and converting to float
  • removed the line plt.xlim(0,20) which is causing the output to display 1970
  • used alternative way to plot, so that the x-axis can be formatted to get monthly tick marks, more info here

Please try the code below:

import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
pd.options.mode.chained_assignment = None

Bitcoin_Historical = pd.read_csv('data.csv')
train1 = Bitcoin_Historical[['Date','Price']]
train1['Date'] = pd.to_datetime(train1['Date'], infer_datetime_format=True, errors='coerce')
train1['Price'] = train1['Price'].str.replace(',','').str.replace(' ','').astype(float)
train2 = train1.set_index('Date')    #Setting the Date as Index
train2.sort_index(inplace=True)

print (type(train2))
print (train2.head())

ax = train2.plot(figsize=(15, 5))
ax.xaxis.set_major_locator(mdates.MonthLocator(interval=1))
ax.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%b'))
plt.xlabel('Date', fontsize=12)
plt.ylabel('Price', fontsize=12)
plt.title("Closing price distribution of bitcoin", fontsize=15)
plt.show()

Output

enter image description here

like image 141
Black Raven Avatar answered Oct 28 '25 22:10

Black Raven


Try to cast your "Date" column into datetime, check if it does the trick:

train1.Date = pd.to_datetime(train1.Date)
train2 = train1.set_index('Date')
like image 26
Sergey Sakharovskiy Avatar answered Oct 28 '25 21:10

Sergey Sakharovskiy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!