绘制时间序列散点图

时间:2017-04-17 21:18:02

标签: python pandas matplotlib time-series scatter-plot

我有以下数据 -

ProductName    01/01/2016    01/07/2016    01/14/2017
ABC              12             34            51
XYZ               9             76            12
PQR              12             23             7
DEF              54              4            34

我想绘制一个时间序列散点图,显示每天的总销售额。我创建了以下函数 -

def scatterplot(x_data, y_data, x_label, y_label, title):
_, ax = plt.subplots()
ax.scatter(x_data, y_data, s = 30, color = '#539caf', alpha = 0.75)

ax.set_title(title)
ax.set_xlabel(x_label)
ax.set_ylabel(y_label)

我对如何调用此函数以获得我想要的结果感到困惑。该图应显示x轴上的日期和y上的总销售额。

1 个答案:

答案 0 :(得分:2)

如果您的数据位于pandas DataFrame中,您可以将列标题作为x值,将垂直轴上的数据总和(即当天销售的产品总数)作为y值。

import pandas as pd
import matplotlib.pyplot as plt

# replicate Data from question in DataFrame
v = [[12,34,51], [9,76,12], [12,23,7], [54,4,34]]
df = pd.DataFrame(v, columns=["01/01/2016","01/07/2016","01/14/2017"], 
                      index=["ABC", "XYZ", "PQR", "DEF"])
print(df)


def scatterplot(x_data, y_data, x_label, y_label, title):
    fig, ax = plt.subplots()
    ax.scatter(x_data, y_data, s = 30, color = '#539caf', alpha = 0.75)

    ax.set_title(title)
    ax.set_xlabel(x_label)
    ax.set_ylabel(y_label)
    fig.autofmt_xdate()

#use column headers as x values
x = pd.to_datetime(df.columns, format='%m/%d/%Y')
# sum all values from DataFrame along vertical axis
y = df.values.sum(axis=0)    
scatterplot(x,y, "x_label", "y_label", "title")

plt.show()

enter image description here