我在同一个 Matplotlib 图上绘制不同频率的数据集时遇到问题。具体来说,我有从 1998 年到 2021 年的年度叶绿素 a 浓度数据和每月 MEI.V2 数据。如何在同一个图上有效地可视化它们?
这里有一些用于解决问题的虚拟数据:
import numpy as np
import pandas as pd
# Generate yearly chlorophyll-a concentration data
np.random.seed(0)
dates = pd.date_range(start='1998-01-01', end='2021-12-31', freq='Y')
chlorophyll_data = np.random.uniform(low=0, high=5, size=len(dates))
chlorophyll_df = pd.DataFrame({'Date': dates, 'Chlorophyll-a': chlorophyll_data})
# Generate monthly MEI.V2 data
monthly_dates = pd.date_range(start='1998-01-01', end='2021-12-31', freq='M')
mei_data = np.random.uniform(low=-2, high=2, size=len(monthly_dates))
mei_df = pd.DataFrame({'Date': monthly_dates, 'MEI.V2': mei_data})
我绘制两个数据集的尝试如下:
import matplotlib.pyplot as plt
fig, ax1 = plt.subplots(figsize=(12, 6))
# Primary y-axis (left)
ax1.plot(chlorophyll_df.index, chlorophyll_df['Chlorophyll-a'], marker='P', linestyle='-', linewidth=2)
ax1.set_ylabel('Chlorophyll-a concentration [mg $m^{-3}$]')
ax1.legend(ncol=4, loc='upper center')
# Explicitly set the x-axis range
plt.xlim(1998, 2021)
# Secondary y-axis (right)
ax2 = ax1.twinx()
ax2.plot(mei_df.index, mei_df['MEI.V2'], color='red', linestyle='-')
ax2.set_ylabel('MEI.V2', color='red')
ax2.tick_params(axis='y')
ax2.legend(loc='upper right')
# Plot formatting
plt.xticks(rotation=90)
plt.tight_layout()
plt.show()
期望的结果:
我的目标是在同一图上可视化两个数据集,其中叶绿素 a 浓度位于主 y 轴,MEI.V2 位于次 y 轴。每月 MEI.V2 数据与 x 轴上的相应年份保持一致至关重要。
我将不胜感激任何有关如何有效实现这一目标的指导或建议。
谢谢!
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.dates import YearLocator
# Generate yearly chlorophyll-a concentration data
np.random.seed(1)
dates = pd.date_range(start='1998-01-01', end='2021-12-31', freq='YE')
chlorophyll_data = np.random.uniform(low=0, high=5, size=len(dates))
chlorophyll_df = pd.DataFrame({'Date': dates, 'Chlorophyll-a':
chlorophyll_data})
# Generate monthly MEI.V2 data
monthly_dates = pd.date_range(start='1998-01-01', end='2021-12-31',
freq='ME')
mei_data = np.random.uniform(low=-2, high=2, size=len(monthly_dates))
mei_df = pd.DataFrame({'Date': monthly_dates, 'MEI.V2': mei_data})
# Resample monthly MEI.V2 data to yearly averages
mei_yearly_df = mei_df.resample('YE', on='Date').mean()
# Plot both datasets
fig, ax1 = plt.subplots(figsize=(12, 6))
# Primary y-axis (left) for chlorophyll-a concentration
ax1.plot(chlorophyll_df['Date'], chlorophyll_df['Chlorophyll-a'],
marker='P', linestyle='-', label='Chlorophyll-a concentration')
ax1.set_ylabel('Chlorophyll-a concentration [mg $m^{-3}$]', color =
'blue')
ax1.legend(loc='upper left')
# Secondary y-axis (right) for MEI.V2
ax2 = ax1.twinx()
ax2.plot(mei_yearly_df.index, mei_yearly_df['MEI.V2'], color='red',
linestyle='-', label='MEI.V2')
ax2.set_ylabel('MEI.V2', color='red')
ax2.tick_params(axis='y')
ax2.legend(loc='upper right')
# Set x-axis ticks to match the years
ax1.xaxis.set_major_locator(plt.MaxNLocator(len(dates.year)))
# Set x-axis ticks to show only the year
ax1.xaxis.set_major_locator(YearLocator())
# Plot formatting
plt.xticks(rotation=90)
plt.title('Chlorophyll-a Concentration and MEI.V2')
plt.tight_layout()
plt.show()