【问题标题】:Adding a line to a bar chart在条形图中添加一条线
【发布时间】:2022-01-22 15:32:41
【问题描述】:

我想用matplotlib创建一个基于幸福指数数据库的条形折线图。

我想用折线图在二级 y 轴上标出每个国家的慷慨指数。 df1['慷慨']

fig, ax = plt.subplots()

ax.bar(df1['Country name'], df1['Ladder score'])
plt.xticks(rotation=30, ha='right')
fig.set_size_inches(18, 10, forward=True)

【问题讨论】:

    标签: python matplotlib jupyter-notebook


    【解决方案1】:

    这解决了我的问题。

    x=df1['Country name']
    y=df1['Generosity']
    ax2 = ax.twinx()
    ax2 = plt.plot(x, y,color ='r')
    plt.ylabel('Generosity')
    plt.show()
    

    【讨论】: