向条形图添加更多数据

Add more data to barplot

有一个数据框:

{'Date': {0: '2020-06-01',
  1: '2020-06-02',
  2: '2020-06-03',
  3: '2020-06-04',
  4: '2020-06-08'},
 'Started': {0: 1, 1: 1, 2: 3, 3: 2, 4: 2},
 'Ended': {0: 0, 1: 0, 2: 2, 3: 0, 4: 1},
 'conversion': {0: 0.0, 1: 0.0, 2: 0.67, 3: 0.0, 4: 0.5}}

我在 JupiterNotebook

中通过 data2.set_index('Date').plot.bar(figsize = (25, 20)) 将其可视化

但我还需要从 'conversion' 列添加数据到 'date' 下或可能超过列。没必要按我的方式做,可以用seaborn之类的

你可以查看matplotlib中的vignette for annotate。要将注释放在条形图的顶部,您可以计算每个 x-axis 条目的最大高度并使用注释(默认设置为 textcoords='data'):

data2 = pd.DataFrame({'Date': {0: '2020-06-01',
  1: '2020-06-02',
  2: '2020-06-03',
  3: '2020-06-04',
  4: '2020-06-08'},
 'Started': {0: 1, 1: 1, 2: 3, 3: 2, 4: 2},
 'Ended': {0: 0, 1: 0, 2: 2, 3: 0, 4: 1},
 'conversion': {0: 0.0, 1: 0.0, 2: 0.67, 3: 0.0, 4: 0.5}})

fig, ax = plt.subplots(figsize=(8,3))
data2.set_index('Date').plot.bar(ax=ax)
hts = data2.iloc[:,1:].apply(max,axis=1) - 0.05
for i in range(len(hts)):
    ax.text(i,hts[i],data2['conversion'][i])

要在 x-axis 下进行注释,您需要执行类似的操作,只是需要将 x-axis:

进行转换
fig, ax = plt.subplots(figsize=(8,3))
data2.set_index('Date').plot.bar(ax=ax)
for i in range(len(hts)):
    ax.annotate(data2['conversion'][i],xy=(i,-0.6),
                xycoords=ax.get_xaxis_transform(),color="blue")