python 条形图与线图的图例_在python中水平对齐条形图图例

您需要使用ncol参数,该参数设置要在

legend中使用的列数,例如ncol = 2会给你两列.

ax.legend(..., ncol=2)

然后,您可以使用loc参数和bbox_to_anchor查看how to place the legend,以查找合理的参数并使两个图例彼此对齐:

import matplotlib.pyplot as plt

import numpy as np

x = np.arange(4)

y = [5, 7, 4, 9]

z = [9, 3, 5, 6]

r = [30, 40, 45, 37]

fig,ax = plt.subplots()

abc = ax.bar(x,y,0.25 )

cde = ax.bar(x+0.25,z,0.25)

ax.legend((abc[0], cde[0]), ('y', 'z'),loc="lower right", bbox_to_anchor=(1., 1.02) , borderaxespad=0., ncol=2)

ax.set_xticks(x + 0.25 / 2)

ax.set_xticklabels(('A', 'B', 'C', 'D'))

ax2 = ax.twinx()

efg = ax2.plot(x+0.25/2,r,color = 'black',label = "r")

ax2.legend(bbox_to_anchor=(0,1.02),loc="lower left", borderaxespad=0.)

plt.show()

Y9VUs.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值