📜  Python中的 Matplotlib.pyplot.plot_date()函数

📅  最后修改于: 2022-05-13 01:54:58.657000             🧑  作者: Mango

Python中的 Matplotlib.pyplot.plot_date()函数

Matplotlib 是Python中用于数据可视化的模块或包或库。 Pyplot 是 Matplotlib 模块的接口,它提供了类似 MATLAB 的接口。

matplotlib.pyplot.plot_date()

此函数用于向绘图添加日期。

句法:

这是日期函数的语法。它包含下面解释的各种参数或参数。

S.no.

Parameter/Arguments

Description

1.

x, y

x and y both are the coordinates of the data i.e. x-axis horizontally and y-axis vertically.

2.

fmt

It is a optional string parameter that contains the corresponding plot details like color, style etc. 

3.

tz

tz stands for timezone used to label dates, default(UTC).

4.

xdate

xdate parameter contains boolean value. If xdate is true then x-axis is interpreted as date in matplotlib. By default xdate is true.

5.

ydate

If ydate is true then y-axis is interpreted as date in matplotlib. By default ydate is false.

6.

data

The data which is going to be used in plot.

最后一个参数 **kwargs 是 Keyword 参数,控制 Line2D 属性,如动画、dash_joint-style、颜色、线宽、线型、标记等。

示例 1:

Python3
# importing libraries
import matplotlib.pyplot as plt
from datetime import datetime
  
# creating array of dates for x axis
dates = [
    datetime(2020, 6, 30),
    datetime(2020, 7, 22),
    datetime(2020, 8, 3),
    datetime(2020, 9, 14)
]
  
# for y axis
x = [0, 1, 2, 3]
  
plt.plot_date(dates, x, 'g')
plt.xticks(rotation=70)
plt.show()


Python3
# importing libraries
import pandas as pd
import matplotlib.pyplot as plt
from datetime import datetime
  
# creating a dataframe
data = pd.DataFrame({'Date': [datetime(2020, 6, 30),
                              datetime(2020, 7, 22),
                              datetime(2020, 8, 3),
                              datetime(2020, 9, 14)],
                       
                     'Close': [8800, 2600, 8500, 7400]})
  
# x-axis
price_date = data['Date']
  
# y-axis
price_close = data['Close']
  
plt.plot_date(price_date, price_close, linestyle='--', color='r')
plt.title('Market', fontweight="bold")
plt.xlabel('Date of Closing')
plt.ylabel('Closing Amount')
  
plt.show()


Python3
# importing libraries
import pandas as pd
import matplotlib.pyplot as plt
from datetime import datetime
  
# creating a dataframe
data = pd.DataFrame({'Date': [datetime(2020, 6, 30), 
                              datetime(2020, 7, 22), 
                              datetime(2020, 8, 3),
                              datetime(2020, 9, 14)],
                       
                     'Close': [8800, 2600, 8500, 7400]})
  
# x-axis
price_date = data['Date']
  
# y-axis
price_close = data['Close']
  
plt.plot_date(price_date, price_close, linestyle='--', color='r')
plt.title('Market', fontweight="bold")
plt.xlabel('Date of Closing')
plt.ylabel('Closing Amount')
  
# Changing the formate of the date using
# dateformatter class
format_date = mpl_dates.DateFormatter('%d-%m-%Y')
  
# getting the accurate current axes using gca()
plt.gca().xaxis.set_major_formatter(format_date)
  
plt.show()


输出:

示例 2:使用数据集创建绘图。

蟒蛇3

# importing libraries
import pandas as pd
import matplotlib.pyplot as plt
from datetime import datetime
  
# creating a dataframe
data = pd.DataFrame({'Date': [datetime(2020, 6, 30),
                              datetime(2020, 7, 22),
                              datetime(2020, 8, 3),
                              datetime(2020, 9, 14)],
                       
                     'Close': [8800, 2600, 8500, 7400]})
  
# x-axis
price_date = data['Date']
  
# y-axis
price_close = data['Close']
  
plt.plot_date(price_date, price_close, linestyle='--', color='r')
plt.title('Market', fontweight="bold")
plt.xlabel('Date of Closing')
plt.ylabel('Closing Amount')
  
plt.show()

输出:

示例 3:更改日期格式:

蟒蛇3

# importing libraries
import pandas as pd
import matplotlib.pyplot as plt
from datetime import datetime
  
# creating a dataframe
data = pd.DataFrame({'Date': [datetime(2020, 6, 30), 
                              datetime(2020, 7, 22), 
                              datetime(2020, 8, 3),
                              datetime(2020, 9, 14)],
                       
                     'Close': [8800, 2600, 8500, 7400]})
  
# x-axis
price_date = data['Date']
  
# y-axis
price_close = data['Close']
  
plt.plot_date(price_date, price_close, linestyle='--', color='r')
plt.title('Market', fontweight="bold")
plt.xlabel('Date of Closing')
plt.ylabel('Closing Amount')
  
# Changing the formate of the date using
# dateformatter class
format_date = mpl_dates.DateFormatter('%d-%m-%Y')
  
# getting the accurate current axes using gca()
plt.gca().xaxis.set_major_formatter(format_date)
  
plt.show()

输出:

日期格式更改为 dd-mm-yyyy。要了解有关 dataformatter 和 gca() 的更多信息,请单击此处。