使用不同的数据进行传说matplotlib

发布于 2025-01-21 10:29:48 字数 676 浏览 1 评论 0原文

我正在尝试创建与数据框架中其他数据相对应的传奇,而不是用于创建条形图的列。 我的理解是,Matplotlib Legend使用数据输入来分配传说,因为我输入一列(x)和一列(y),所以我只会得到一个传说。 我正在尝试将“位置”列用作我的传奇输入,以便我将有3个传奇输入['bot','mid','top'],并分配了各自的条。不确定如何。

import numpy as np 
import pandas as pd 
import matplotlib.pyplot as plt

df = pd.DataFrame({'wins': [8,6,2],
                   'player': ['x','y','z'],
                   'position' : ['bot', 'mid', 'top'],
                   'color': ['red', 'blue', 'green']})

bar = plt.bar(df["player"], df["wins"], color = (df["color"]))
bar = plt.legend(labels =df['position'])

代码的结果

I'm trying to create a legend that corresponds to other data in dataframe rather than the column used to create the bar plot.
My understanding is that matplotlib legend, uses the data input to assign legends, since I enter one column (x) and one column (y) I get one legends only.
I'm trying to use the "position "column as my legend input, such that I'll have 3 legends inputs ['bot','mid','top'] with their respective bars assigned. Not sure how.

import numpy as np 
import pandas as pd 
import matplotlib.pyplot as plt

df = pd.DataFrame({'wins': [8,6,2],
                   'player': ['x','y','z'],
                   'position' : ['bot', 'mid', 'top'],
                   'color': ['red', 'blue', 'green']})

bar = plt.bar(df["player"], df["wins"], color = (df["color"]))
bar = plt.legend(labels =df['position'])

results of code

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

将军与妓 2025-01-28 10:29:48

使用Seaborn,将其写为:

import seaborn as sns

sns.barplot(data=df, x='player', y='wins', hue='position', dodge=False,
            palette=dict(zip(df['position'], df['color'])))

“带有色调的海底barplot”

Using seaborn, one would write this as:

import seaborn as sns

sns.barplot(data=df, x='player', y='wins', hue='position', dodge=False,
            palette=dict(zip(df['position'], df['color'])))

seaborn barplot with hue

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文