matplotlib 设置legend的位置在轴最上方,长度与图的长度相同

复制代码
import matplotlib.pyplot as plt
import numpy as np

x1 = np.linspace(0, 10, 50)
x2 = [6,4,3]

ax = plt.subplot()
ax.plot(x1, label="test1")
ax.plot(x2, label="test2")
# 设置图例的位置
# 将左下角放置在【0, 1.02】位置处,横为1,纵为0.102
# borderaxespad:设置坐标轴与图例外边框的间距
ax.legend(bbox_to_anchor=(0, 1.02, 1, 0.102), 
loc='lower left',ncol=2, mode="expand", borderaxespad=0)

plt.show()


参考文献:

相关推荐
pchaoda2 天前
RSI与布林带技术指标实战
python·matplotlib·量化
啊阿狸不会拉杆2 天前
《机器学习导论》第 5 章-多元方法
人工智能·python·算法·机器学习·numpy·matplotlib·多元方法
砚边数影3 天前
数据可视化入门:Matplotlib 基础语法与折线图绘制
数据库·信息可视化·matplotlib·数据可视化·kingbase·数据库平替用金仓·金仓数据库
pchaoda3 天前
基本面因子计算入门
python·matplotlib·量化
铁手飞鹰3 天前
[深度学习]常用的库与操作
人工智能·pytorch·python·深度学习·numpy·scikit-learn·matplotlib
啊阿狸不会拉杆4 天前
《机器学习导论》第 1 章 - 引言
人工智能·python·算法·机器学习·ai·numpy·matplotlib
不懒不懒4 天前
【机器学习:下采样 VS 过采样——逻辑回归在信用卡欺诈检测中的实践】
python·numpy·scikit-learn·matplotlib·pip·futurewarning
byzh_rc5 天前
[数学建模从入门到入土] matplotlib例图
数学建模·matplotlib
七夜zippoe5 天前
数据可视化高级技巧:Matplotlib + Seaborn实战大全
python·信息可视化·matplotlib·数据可视化·seaborn·gridspec
-To be number.wan7 天前
Python数据分析:Matplotlib 绘图练习
python·数据分析·matplotlib