Python中的 Matplotlib.axis.Axis.axis_date()函数
Matplotlib是Python中的一个库,它是 NumPy 库的数值数学扩展。它是Python中用于二维数组图的惊人可视化库,用于处理更广泛的 SciPy 堆栈。
matplotlib.axis.Axis.axis_date()函数
matplotlib 库的轴模块中的Axis.axis_date()函数用于设置轴刻度和标签,将沿该轴的数据视为日期。
Syntax: Axis.axis_date(self, tz=None)
Parameters: This method accepts the following parameters.
- tz : This parameter is the timezone used to create date labels.
Return value: This method does not return any value.
以下示例说明了 matplotlib.axis 中的 matplotlib.axis.Axis.axis_date()函数:
示例 1:
Python3
# Implementation of matplotlib function
from matplotlib.axis import Axis
import datetime as dt
import matplotlib.pyplot as plt
from matplotlib.dates import DateFormatter, MinuteLocator
x = [16.7,16.8,17.1,17.4]
y = [15,17,14,16]
plt.plot(x, y)
plt.gca().yaxis.axis_date()
plt.title("Matplotlib.axis.Axis.axis_date()\
Function Example", fontsize = 12, fontweight ='bold')
plt.show()
Python3
# Implementation of matplotlib function
from matplotlib.axis import Axis
from datetime import datetime
import matplotlib.pyplot as plt
from matplotlib.dates import (
DateFormatter, AutoDateLocator, AutoDateFormatter, datestr2num
)
days = [
'30/01/2019',
'31/01/2019',
'01/02/2019',
'02/02/2019',
'03/02/2019',
'04/02/2019'
]
data1 = [2, 5, 13, 6, 11, 7]
data2 = [6, 3, 10, 3, 6, 5]
z = datestr2num([
datetime.strptime(day, '%d/%m/%Y').strftime('%m/%d/%Y')
for day in days
])
r = 0.25
figure = plt.figure(figsize =(8, 4))
axes = figure.add_subplot(111)
axes.bar(z - r, data1, width = 2 * r,
color ='g', align ='center',
tick_label = days)
axes.bar(z + r, data2, width = 2 * r,
color ='y', align ='center',
tick_label = days)
axes.xaxis.axis_date()
plt.title("Matplotlib.axis.Axis.axis_date()\
Function Example", fontsize = 12, fontweight ='bold')
plt.show()
输出:
示例 2:
Python3
# Implementation of matplotlib function
from matplotlib.axis import Axis
from datetime import datetime
import matplotlib.pyplot as plt
from matplotlib.dates import (
DateFormatter, AutoDateLocator, AutoDateFormatter, datestr2num
)
days = [
'30/01/2019',
'31/01/2019',
'01/02/2019',
'02/02/2019',
'03/02/2019',
'04/02/2019'
]
data1 = [2, 5, 13, 6, 11, 7]
data2 = [6, 3, 10, 3, 6, 5]
z = datestr2num([
datetime.strptime(day, '%d/%m/%Y').strftime('%m/%d/%Y')
for day in days
])
r = 0.25
figure = plt.figure(figsize =(8, 4))
axes = figure.add_subplot(111)
axes.bar(z - r, data1, width = 2 * r,
color ='g', align ='center',
tick_label = days)
axes.bar(z + r, data2, width = 2 * r,
color ='y', align ='center',
tick_label = days)
axes.xaxis.axis_date()
plt.title("Matplotlib.axis.Axis.axis_date()\
Function Example", fontsize = 12, fontweight ='bold')
plt.show()
输出: