python 图例怎样横排_在python中水平对齐条形图图例

本文介绍了如何在Python的matplotlib库中将图例水平排列,特别是在创建多轴图形时。通过设置`ncol`参数为2,可以将图例分为两列。同时,使用`bbox_to_anchor`和`loc`属性来调整图例的位置,使其在图表的右下角和左下角对齐。
摘要由CSDN通过智能技术生成

I have made a multi axis graph using the following code and I am unable to arrange the legend as I want. The code of my graph is as follows:

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'),bbox_to_anchor=(0., 1.02, 1, .102) , borderaxespad=0.)

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.11,1.07) , borderaxespad=0.)

plt.sh

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值