📜  如何使用 Matplotlib 在Python中突出显示时间序列图中的时间范围?

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

如何使用 Matplotlib 在Python中突出显示时间序列图中的时间范围?

时间序列图是包含在一段时间内测量的数据的图,例如一个国家的国内生产总值、世界人口和许多其他数据。

有时我们想突出时间线的特定时间段,以便观察者更容易阅读特定数据。我们可以使用 pyplot.axvspan() 在时间序列图中突出显示时间范围 matplotlib模块中。

句法:

下面是一些描述如何在时间序列图中突出显示时间范围的示例:

示例 1

让我们假设我们有一个国家,我们想绘制一个多年跨度的国内生产总值图。首先,我们必须准备我们的数据,以便我们可以绘制图表。在 Y 轴上,我们将采用 GDP 将是从 5 到 10 的随机整数。在 X 轴上,我们将采用 1900 年至 2020 年的时间段。

我们将取一个国家的 GDP 并在一段时间内突出显示它。

Python3
# import required modules
import random
import matplotlib.pyplot as plt
 
# create dataset
year = [i for i in range(1900,2021)]
GDP = []
for i in range(121):
    GDP.append(random.randint(5,10))
     
# display dataset   
print("Length of year list is: " + str(len(year)))
print("Length of GDP list is: " + str(len(GDP)))
print("First 10 elements of respective list are: ")
print(year[:10])
print(GDP[:10])


Python3
# depict time series
fig, ax = plt.subplots(figsize=(5,5))
ymin, ymax = plt. ylim()
ax.plot(year,GDP)
plt.ylim(ymin * 50, ymax * 50)
 
# adjust label
ax.set_ylabel("GDP")
 
# assign title
ax.set_title("GDP of country over years" ,size=15)
plt.show()


Python3
# depict illustration
fig, ax = plt.subplots(figsize=(5, 5))
ymin, ymax = plt. ylim()
ax.plot(year, GDP)
plt.ylim(ymin * 50, ymax * 50)
 
# adjust labels
ax.set_ylabel("GDP")
 
# assign title
ax.set_title("GDP of country over years", size=15)
 
# highlight a time range
ax.axvspan(1990, 2010, color="blue", alpha=0.3)
plt.show()


Python3
# importing libraries
import matplotlib.pyplot as plt
import random
 
# creating the dataset
date = [i for i in range(2000, 2021)]
value = []
for i in range(21):
    value.append(random.randint(5, 15))
 
# Create a figure and a set of subplots
fig, ax = plt.subplots(figsize=(10, 6))
 
# Creating the scatter plot
ax.scatter(date, value)
 
# Highlighting for a certain period of time
ax.axvspan(2002, 2005, alpha=0.3, color="green")
plt.show()


Python3
# import required modules
import random
import matplotlib.pyplot as plt
 
# create dataset
x = [int(i) for i in range(2000,2020)]
y = [i for i in range(20)]
 
# depict illustration
fig, ax = plt.subplots(figsize=(10, 10))
ymin, ymax = plt. ylim()
ax.plot(x, y)
 
# highlight a time range
ax.axvspan(2005, 2010, color="green", alpha=0.6)
plt.show()


输出:

现在我们将绘制 GDP 与年份的关系图。我们相应地缩放了 Y 轴。

蟒蛇3

# depict time series
fig, ax = plt.subplots(figsize=(5,5))
ymin, ymax = plt. ylim()
ax.plot(year,GDP)
plt.ylim(ymin * 50, ymax * 50)
 
# adjust label
ax.set_ylabel("GDP")
 
# assign title
ax.set_title("GDP of country over years" ,size=15)
plt.show()

输出:

GDP与年份的关系图

subplot 关键字返回图形,即绘图和 Axes 数组(存储在ax中),我们将使用它们来突出显示图形。

我们将使用axvspan()方法,该方法在给定范围内的轴上添加垂直跨度(矩形),然后我们可以更改矩形的颜色,然后降低其不透明度,以便看起来我们已经突出显示了它。

注意:请记住降低不透明度,否则它将在图形上显示为纯色矩形。使用**kwargs使用的alpha参数用于降低不透明度。

蟒蛇3

# depict illustration
fig, ax = plt.subplots(figsize=(5, 5))
ymin, ymax = plt. ylim()
ax.plot(year, GDP)
plt.ylim(ymin * 50, ymax * 50)
 
# adjust labels
ax.set_ylabel("GDP")
 
# assign title
ax.set_title("GDP of country over years", size=15)
 
# highlight a time range
ax.axvspan(1990, 2010, color="blue", alpha=0.3)
plt.show()

突出显示的 GDP 与年份图

示例 2

这是另一个程序,我们将在其中突出显示散点图的 x 轴。

蟒蛇3

# importing libraries
import matplotlib.pyplot as plt
import random
 
# creating the dataset
date = [i for i in range(2000, 2021)]
value = []
for i in range(21):
    value.append(random.randint(5, 15))
 
# Create a figure and a set of subplots
fig, ax = plt.subplots(figsize=(10, 6))
 
# Creating the scatter plot
ax.scatter(date, value)
 
# Highlighting for a certain period of time
ax.axvspan(2002, 2005, alpha=0.3, color="green")
plt.show()

在散点图上突出显示

示例 3

下面是另一个例子,我们突出显示从 2005 年到 2010 年的时间范围

蟒蛇3

# import required modules
import random
import matplotlib.pyplot as plt
 
# create dataset
x = [int(i) for i in range(2000,2020)]
y = [i for i in range(20)]
 
# depict illustration
fig, ax = plt.subplots(figsize=(10, 10))
ymin, ymax = plt. ylim()
ax.plot(x, y)
 
# highlight a time range
ax.axvspan(2005, 2010, color="green", alpha=0.6)
plt.show()

输出: