如何将我的折线图添加到上一个数据点?

2024-05-13 21:19:57 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图创建一个线条图,显示一个城市在一段时间内的啤酒厂总数。图表并没有添加到以前的数据点上,它只是从每个新的日期重新开始

这是我正在使用的数据集:

Year Opened City Brewery Name
2012    Charlottesville 1
Fredericksburg  1
Norfolk 1
2013    Leesburg    1
Manassas    2
Richmond    2
2014    Fredericksburg  1
Purcellville    3
Richmond    4
Roanoke 3
Virginia Beach  3
2015    Fredericksburg  2
Leesburg    1
Manassas    1
Norfolk 1
Richmond    1
Sterling    1
Virginia Beach  2

以下是图形代码:

# plot data
fig, ax = plt.subplots(figsize=(15,7))
# use unstack()
top10brew_df.unstack().plot(kind='line', y="Brewery Name", ax=ax)

what my graph looks like now


Tags: 数据nameplotaxbeach啤酒厂breweryunstack
1条回答
网友
1楼 · 发布于 2024-05-13 21:19:57

使用重新索引并用0填充值

df = df.pivot(index='Year', columns='Opened City',values='Brewery Name').reindex(range(2012,2015)).fillna(0)

enter image description here

绘制结果

fig, ax = plt.subplots(figsize=(15,7))
plt.xticks(df.index.values.astype(int))
df.plot.line(ax=ax)

enter image description here

相关问题 更多 >