When I draw a time series graph using Python plot, date conversion does not work properly. For example, 2020-01-01 is mistakenly displayed as 0051-01-01.
For the data frame generated by Pandas, display the graph with the command "Data frame.plot (...)" and use "mdates.DateFormatter ('% Y-% m-% d')" to "Format YMD". It turned out that the date is not displayed correctly when it is set to. When the code below is executed, the date on the horizontal axis is not displayed correctly.
/home/sampletest/sample.py
import datetime
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import seaborn as sns
#Define a Data set.(The date is datetime.Describe in datetime. Notated by date type)
dat = [
[datetime.datetime(2020,1,1),4,10],
[datetime.datetime(2020,1,2),7,7],
[datetime.datetime(2020,1,3),10,4],
[datetime.datetime(2020,1,4),13,2],
[datetime.datetime(2020,1,5),17,1],
[datetime.datetime(2020,1,6),12,4],
[datetime.datetime(2020,1,7),9,3],
[datetime.datetime(2020,1,8),7,8],
[datetime.datetime(2020,1,9),5,9],
[datetime.datetime(2020,1,10),3,12],
]
dat=pd.DataFrame(dat,columns=["DATE","Y","Z"])
dat.set_index("DATE",inplace=True) #Set the date displayed on the horizontal axis to the index of DataFrame.
print(dat)
fig = sns.mpl.pyplot.figure() #Create an object to draw the graph.
ax=dat.plot(marker="o",figsize=(15, 5)) #Dataframe.An error occurs when drawing a graph in plot format
ax.legend() #Draw a legend
#Graph format settings(Set the date display method on the horizontal axis.)
days = mdates.DayLocator(bymonthday=None, interval=2, tz=None) #Horizontal axis: "Everyday" is displayed.(Without this line the date will be duplicated)
daysFmt = mdates.DateFormatter('%Y-%m-%d') #Horizontal axis: Format Y-M-Set to D.
ax.xaxis.set_major_locator(days) #Display the date on the horizontal axis.
ax.xaxis.set_major_formatter(daysFmt) #Display the date on the horizontal axis.
fig.autofmt_xdate() #The date on the horizontal axis is slanted so that it is easy to see.
#Give the graph a name
ax.set_xlabel('Date') #Set the X-axis title
ax.set_ylabel('Y') #Set the Y-axis title
plt.title(r"TEST",fontname="MS Gothic") #Set the title of the graph. When specifying Japanese, it is necessary to specify fontname
#Set the size of the graph
fig.set_figheight(10)
fig.set_figwidth(20)
#Set the display range on the horizontal axis
ax.set_xlim(datetime.datetime(2020,1,1), datetime.datetime(2020,1,12))
It is necessary to change the date line of the data frame from datetime to Matplotlib date.
Add dat ['DATE'] = mdates.date2num (dat ['DATE']).
/home/sampletest/sample.py
import datetime
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import seaborn as sns
import pandas as pd
#Define a Data set.(The date is datetime.Describe in datetime. Notated by date type)
dat = [
[datetime.datetime(2020,1,1),4,10],
[datetime.datetime(2020,1,2),7,7],
[datetime.datetime(2020,1,3),10,4],
[datetime.datetime(2020,1,4),13,2],
[datetime.datetime(2020,1,5),17,1],
[datetime.datetime(2020,1,6),12,4],
[datetime.datetime(2020,1,7),9,3],
[datetime.datetime(2020,1,8),7,8],
[datetime.datetime(2020,1,9),5,9],
[datetime.datetime(2020,1,10),3,12],
]
dat=pd.DataFrame(dat,columns=["DATE","Y","Z"])
dat['DATE'] = mdates.date2num(dat['DATE']) #Add here Convert datetime objects to Matplotlib dates.
dat.set_index("DATE",inplace=True) #Set the date displayed on the horizontal axis to the index of DataFrame.
print(dat)
fig = sns.mpl.pyplot.figure() #Create an object to draw the graph.
ax=dat.plot(marker="o",figsize=(15, 5))
ax.legend() #Draw a legend
#Graph format settings(Set the date display method on the horizontal axis.)
days = mdates.DayLocator(bymonthday=None, interval=2, tz=None) #Horizontal axis: "Everyday" is displayed.(Without this line the date will be duplicated)
daysFmt = mdates.DateFormatter('%Y-%m-%d') #Horizontal axis: Format Y-M-Set to D.
ax.xaxis.set_major_locator(days) #Display the date on the horizontal axis.
ax.xaxis.set_major_formatter(daysFmt) #Display the date on the horizontal axis.
fig.autofmt_xdate() #The date on the horizontal axis is slanted so that it is easy to see.
#Give the graph a name
ax.set_xlabel('Date') #Set the X-axis title
ax.set_ylabel('Y') #Set the Y-axis title
plt.title(r"TEST",fontname="MS Gothic") #Set the title of the graph. When specifying Japanese, it is necessary to specify fontname
#Set the size of the graph
fig.set_figheight(10)
fig.set_figwidth(20)
#Set the display range on the horizontal axis
ax.set_xlim(datetime.datetime(2020,1,1), datetime.datetime(2020,1,12))
Recommended Posts