1

试图绘制两组数组。x 对 y 和 x1 对 y1 在同一张图上。两组的日期时间值不同,但 y 和 y1 是根据相同的 x 值绘制的,而不是写入数组中的值(两组的日期不同)。

任何帮助表示赞赏!

x = ['01/01/2019', '05/11/2019', '09/02/2019', '09/10/2020', '09/19/2019', '09/24/2019', 
'10/26/2019', '03/14/2020', '03/16/2020', '03/16/2020', '05/10/2020', '07/28/2020', '09/03/2020', 
'14/09/2020']
y = [0.0025, 0.00881983, 0.0025, 0.009436, 0.01069436, 0.01213136, 0.00925736, 0.01503343, 
0.01803343, 0.02103343, 0.02603343, 0.01353343, 0.02703343, 0.03065149]
fig,ax = plt.subplots()
ax.plot(x, y, color="blue", marker="o")
ax.set_xlabel("Time",fontsize=14)
ax.set_ylabel("Y1",color="blue",fontsize=14)
plt.xticks(rotation=90)
x1 = ['16/11/2018', '27/12/2018', '07/01/2019', '18/03/2019', '13/05/2019', '19/09/2019', 
'16/03/2020', '7/09/2020', '7/09/2020']
y1 = [10, 15, 20, 30, 32, 52, 115.27, 165.27, 160.79]
ax2=ax.twinx()
ax2.plot(x1, y1, color="green",marker="o")
ax2.set_ylabel("Y2",color="green",fontsize=14)
plt.xticks(rotation=90)
plt.show()`

输出图可以在这里看到

4

1 回答 1

1

这是使用pandas. 由于您想要一个通用的时间序列,因此需要执行一些预处理。

import pandas as pd
import matplotlib.pyplot as plt

x = ['01/01/2019', '05/11/2019', '09/02/2019', '09/10/2020', '09/19/2019', '09/24/2019',
'10/26/2019', '03/14/2020', '03/16/2020', '03/16/2020', '05/10/2020', '07/28/2020', '09/03/2020',
'14/09/2020']
y = [0.0025, 0.00881983, 0.0025, 0.009436, 0.01069436, 0.01213136, 0.00925736, 0.01503343,
0.01803343, 0.02103343, 0.02603343, 0.01353343, 0.02703343, 0.03065149]

x1 = ['16/11/2018', '27/12/2018', '07/01/2019', '18/03/2019', '13/05/2019', '19/09/2019',
'16/03/2020', '7/09/2020', '7/09/2020']
y1 = [10, 15, 20, 30, 32, 52, 115.27, 165.27, 160.79]

#Converting first data to dataframe and date to datetime format
df = pd.DataFrame([x,y]).T
df.columns = ['Date','Data1']
df['Date'] = pd.to_datetime(df['Date'])

#Converting second data to dataframe and date to datetime format
df1 = pd.DataFrame([x1,y1]).T
df1.columns = ['Date','Data2']
df1['Date'] = pd.to_datetime(df1['Date'])

#Merging both dataframes to have one common dataset 
complete_df = df.merge(df1, on='Date', how='outer')
complete_df.set_index('Date',inplace=True)

#Sorting the data according to Date
complete_df = complete_df.sort_index()

#Taking care of the nan values coming due to a common timeseries
complete_df.fillna(method='ffill',inplace=True)

##Plotting
ax = complete_df['Data1'].plot(lw=3)
complete_df['Data2'].plot(ax=ax, lw=3,secondary_y=True)
plt.show()

在此处输入图像描述

于 2020-09-16T22:04:44.487 回答