Python - 使用折线图创建带有多个列的时间序列图
要在使用折线图的情况下创建带有多个列的时间序列图,请使用 lineplot()。首先导入所需的库 -
import seaborn as sb import pandas as pd import matplotlib.pyplot as plt
创建一个 DataFrame。我们的 DataFrame 包含多列 -
dataFrame = pd.DataFrame({'Date_of_Purchase': ['2018-07-25', '2018-10-25', '2019-01-25', '2019-05-25', '2019-08-25','2020-09-25','2021-03-25'],'Units Sold': [98, 77, 51, 70, 70, 87, 76],'Units Returned' : [60, 50, 40, 57, 62, 51, 60] })
为多列绘制时间序列图 -
sb.lineplot(x="Date_of_Purchase", y="Units Sold", data=dataFrame) sb.lineplot(x="Date_of_Purchase", y="Units Returned", data=dataFrame)
示例
以下是代码 -
import seaborn as sb import pandas as pd import matplotlib.pyplot as plt # creating DataFrame dataFrame = pd.DataFrame({'Date_of_Purchase': ['2018-07-25', '2018-10-25', '2019-01-25', '2019-05-25', '2019-08-25','2020-09-25','2021-03-25'],'Units Sold': [98, 77, 51, 70, 70, 87, 76],'Units Returned' : [60, 50, 40, 57, 62, 51, 60] }) # time series plot for multiple columns sb.lineplot(x="Date_of_Purchase", y="Units Sold", data=dataFrame) sb.lineplot(x="Date_of_Purchase", y="Units Returned", data=dataFrame) # set label plt.ylabel("Units Returned Unites Sold") plt.show()
输出
将产生以下输出 -
广告