python图例位置_Python | 图例位置

图例在数据可视化中至关重要,Matplotlib提供多种方式自定义图例位置。本文展示了如何使用Python代码将图例放置在图表的不同位置。

python图例位置

Legends are one of the key components of data visualization and plotting. Matplotlib can automatically define a position for a legend in addition to this, it allows us to locate it in our required positions. Following is the list of locations on which we can locate our plot legend.

图例是数据可视化和绘图的关键组成部分之一。 Matplotlib可以为此自动定义图例的位置,它使我们能够将其定位在所需的位置。 以下是我们可以在其中找到图例的位置的列表。

Python | Legend Locations (1)
Python | Legend Locations (2)
Python | Legend Locations (3)
Python | Legend Locations (4)

图例位置的Python代码 (Python code for legend locations)

# Data Visualization using Python
# Adding a Legend Location

import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(0, 2, 100)

# Example 1
plt.figure()
plt.plot(x, x+2, label='linear')  
plt.plot(x, x**2, label='quadratic')  
plt.plot(x, x**3, label='cubic')
plt.xlabel('x label')
plt.ylabel('y label')
plt.title("lower right")
plt.legend(loc='lower right')

# Example 2
plt.figure()
x = np.linspace(0.0, 5.0)
y = x*x
plt.subplot(2, 1, 2)
plt.plot(x, y, 'g.-',label='quadratic')
plt.plot(x,x, 'r.-', label='linear')
plt.title('uper left')
plt.xlabel('numbers')
plt.ylabel('Square')
plt.legend(loc='uper left')
plt.show()

# Example 3
# Random Bar Graph Example
plt.figure()
plt.bar(np.arange(26), np.random.randint(0,50,26), alpha = 0.5, color='g', label='Legend Bar Graph')
plt.title('center right')
plt.legend(loc='center right')

Output:

输出:

Output is as figure


翻译自: https://www.includehelp.com/python/legend-locations.aspx

python图例位置

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值