【发布时间】:2022-01-23 23:16:22
【问题描述】:
- 我是 Python 的初学者,正在学习在线课程。以下是课程中给定解决方案的抽象版本。
- 在一个练习中,生成了一个
seaborn图并添加了一个legend。 -
问题:根据使用的参数,我不明白图例在右侧的情况。
loc = 'center left'是如何将legend放在情节右侧的? -
matplotlibmanual 说:
字符串 'upper center', 'lower center', 'center left', 'center right' 将图例放置在相应边缘的中心 轴/图。
- 我确信有一个合乎逻辑的答案,但我看不到它:)。
代码清单
import pandas as pd
import seaborn as sb
# https://www.geeksforgeeks.org/different-ways-to-create-pandas-dataframe/
# initialize data of lists.
data = {'Name':['Tim', 'Tom', 'Cindy', 'Mandy'],
'Age':[20, 21, 19, 18],
'Gender':['Male', 'Male', 'Female', 'Female']}
# Create DataFrame
df = pd.DataFrame(data)
sb.barplot(data = df, x = 'Name', y = 'Age', hue = 'Gender')
plt.legend(loc = 'center left', bbox_to_anchor = (1, 0.5)) # legend to right of figure
截图
(https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.legend.html)
【问题讨论】:
标签: python matplotlib seaborn