1

How change the date distance and only showing month in a year?

import pandas as pd
import numpy as np
import xlsxwriter
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import datetime
df = pd.read_csv('lclax.csv')


x =df['Date']
y= df['Close']

plt.plot(x , y)



plt.title("Los Cerros Daily Share Price")
plt.xlabel("Date")
plt.ylabel("Price")

plt.xticks( rotation=90)
plt.show()

enter image description here

I've tried several methods but nothing seem to be working, FYI I'm new to coding so any suggestion is welcomed. Trying to learn to format the dates in the picture into either showing DD/MM/YY in monthly frequency or months only in the year.

           Date   Open   High    Low  Close  Adj Close   Volume
0    02/01/2020  0.070  0.070  0.068  0.069      0.069    33728
1    03/01/2020  0.069  0.070  0.068  0.068      0.068    65471
2    06/01/2020  0.068  0.070  0.066  0.067      0.067   306723
3    07/01/2020  0.067  0.067  0.067  0.067      0.067    64016
4    08/01/2020  0.066  0.067  0.062  0.064      0.064   969921
..          ...    ...    ...    ...    ...        ...      ...
243  14/12/2020  0.100  0.105  0.093  0.100      0.100  8060996
244  15/12/2020  0.099  0.099  0.095  0.095      0.095  1876563
245  16/12/2020  0.099  0.100  0.096  0.096      0.096  2707856
246  17/12/2020  0.099  0.099  0.098  0.098      0.098  1518175
247  18/12/2020  0.098  0.098  0.095  0.096      0.096  2124907

[248 rows x 7 columns]
Teo
  • 11
  • 2
  • Are you sure your dates are in datetime format after the import? If so, you can set the [MonthLocator and DateFormatter](https://stackoverflow.com/a/65343940/8881141). – Mr. T Dec 25 '20 at 09:05
  • 1
    [how-to-change-the-datetime-tick-label-frequency-for-matplotlib-plots](https://stackoverflow.com/questions/45704366/how-to-change-the-datetime-tick-label-frequency-for-matplotlib-plots), what you want to look for is how to change pandas xticks labels. – Ferris Dec 25 '20 at 09:15
  • This has been answered in a previous question - https://stackoverflow.com/questions/1574088/plotting-time-in-python-with-matplotlib – MeatBoyed Dec 25 '20 at 09:35
  • Does this answer your question? [Plotting time in Python with Matplotlib](https://stackoverflow.com/questions/1574088/plotting-time-in-python-with-matplotlib) – Ruli Dec 25 '20 at 09:47
  • I've tried several methods, some messed up my dates, bringing it back to 1970, where it supposed to be 2020. So my date within the csv is from 1/1/20 to 17/12/20. Is there a way to specify the start and end date? Will defining the start and end of x, which is the date inside the csv helps? – Teo Dec 25 '20 at 10:04
  • There was a reason why I initially asked if you are sure that the data are in datetime format. "Bringing it back to 1970" sounds like your data are numerical and not in datetime format. What does `df.dtypes` return? Maybe you can link to the csv file or provide a data sample. Please read [How to make good reproducible pandas examples](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples). – Mr. T Dec 25 '20 at 11:02
  • @Mr.T , I've just edited my post, see the data I've got in the csv file. I extracted them from yahoofinancials and saved into csv file. As for df.dtypes. Date is Object,Open, high, low, close, adj close are all float64, volume is int64 and dtype is object. – Teo Dec 25 '20 at 12:39
  • Your date column is probably not a datettime object. Try to convert it: `df['Date'] = pd.to_datetime(df['Date'], dayfirst=True)`, check that the dtype has changed, then try to format the axis ticks and labels as indicated here with multiple links. – Mr. T Dec 25 '20 at 12:53
  • 1
    Hey @Mr.T, thanks for pointing out the fact using `df.dtypes`, managed to use `parse_dates =['Date'], dayfirst=True` and solved the issue. Thanks! Well, you posted seconds before me haha. Thanks for helping. Enjoy your Christmas everyone! – Teo Dec 25 '20 at 12:54
  • Glad we solved the problem. This is just to mark is as a duplicate: [Wrong Dates in Dataframe and Subplots](https://stackoverflow.com/questions/51268856/wrong-dates-in-dataframe-and-subplots) Merry Christmas to you, too. – Mr. T Dec 25 '20 at 13:00

0 Answers0