【问题标题】:How do I get my line graph to add to the previous data point?如何让我的折线图添加到前一个数据点?
【发布时间】:2020-01-12 16:50:18
【问题描述】:

我正在尝试创建一个折线图,显示一段时间内一个城市的啤酒厂总数。该图并未添加到先前的数据点,它只是从每个新日期重新开始。

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

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

【问题讨论】:

    标签: python pandas matplotlib jupyter-notebook linegraph


    【解决方案1】:

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

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

    绘制结果

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-02-22
      • 2020-09-21
      • 1970-01-01
      • 2021-11-13
      • 1970-01-01
      • 2022-01-04
      • 1970-01-01
      相关资源
      最近更新 更多