Python basic drawing (about the drawing method of the graph that changes with the time series)

created at 01-07-2022 views: 5

There are two points to consider when drawing this kind of picture:

  1. How to generate a continuous timeline

  2. How to properly display the style and number of axis labels in the diagram.

import matplotlib.pyplot as plt
import matplotlib as mpl
import numpy as np
% matplotlib inline
import datetime#This package is very important

#Set start and end time
start=datetime.datetime(2013,1,1)
stop=datetime.datetime(2013,12,31)
#Set the date interval
delta=datetime.timedelta(1)
# Returns a floating-point date sequence. This is to generate a time sequence. Similarly, what if the sequence is converted to a date?
dates=mpl.dates.drange(start,stop,delta)
values=np.random.rand(len(dates))

#There are two problems. One is that the axis is not marked in the form of date, and the other is that the number and position of the scale are not appropriate.
fig=plt.figure(figsize=(24,12))#Adjust the size of the drawing space
plt.plot(dates,values,linestyle='-',marker='*',c='r',alpha=0.5)#plotting
ax=plt.gca()
#Set the format of the display
date_format=mpl.dates.DateFormatter('%Y-%m-%d')
#Set the main format of the x axis
ax.xaxis.set_major_formatter(date_format)
)#Set the scale interval of the coordinate axis display
ax.xaxis.set_major_locator(mpl.ticker.MultipleLocator(30)
#Prevent the overlap of data on the x-axis and automatically adjust it.
fig.autofmt_xdate()

plot with time series

created at:01-07-2022
edited at: 01-07-2022: