pandas 绘制时间序列散点图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43459786/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Plot timeseries scatterplot
提问by ComplexData
I have below data -
我有以下数据 -
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
I want to plot a timeseries scatterplot showing total sales on each day. I have created the following function -
我想绘制一个时间序列散点图,显示每天的总销售额。我创建了以下功能 -
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)
I am confused about how to call this function to get my desired result. The plot should show date on the x-axis and total sales on the y.
我对如何调用这个函数来获得我想要的结果感到困惑。该图应在 x 轴上显示日期,在 y 轴上显示总销售额。
回答by ImportanceOfBeingErnest
If your data is in a pandas DataFrame, you may take the column headers as x values and the sum of the data along the vertical axis (i.e. the total number of products sold that day) as y values.
如果您的数据在 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()