Seaborn的定制传奇

发布于 2025-01-21 03:27:29 字数 628 浏览 2 评论 0原文

我想在这里设置一个图例,到目前为止,有一个颜色不正确的图例,请帮我修改一下。

fig, ax = plt.subplots(figsize=(12, 6))
sns.set(style='whitegrid')

ax=sns.pointplot(data=june_avg, x='Hour', y='bike_count', color='black')
ax=sns.pointplot(data=houravg, x='Hour', y='bike_count', color='green')

plt.legend(title='Avg Bike Rent', loc='upper left', labels=['Annaul', 'June'])
ax.set(xlabel='Hour', ylabel='Bike Count')
plt.title('Annual Avg vs June Avg Rent per Hour', fontsize=17, pad=30)

plt.show()

输入图片此处描述

I am trying to set a legend here, and so far got one with incorrect colors, pls help me amend this.

fig, ax = plt.subplots(figsize=(12, 6))
sns.set(style='whitegrid')

ax=sns.pointplot(data=june_avg, x='Hour', y='bike_count', color='black')
ax=sns.pointplot(data=houravg, x='Hour', y='bike_count', color='green')

plt.legend(title='Avg Bike Rent', loc='upper left', labels=['Annaul', 'June'])
ax.set(xlabel='Hour', ylabel='Bike Count')
plt.title('Annual Avg vs June Avg Rent per Hour', fontsize=17, pad=30)

plt.show()

enter image description here

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

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

发布评论

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

评论(1

无远思近则忧 2025-01-28 03:27:29

而不是随后尝试更改传奇,而是此相关帖子也没有解决方案,我建议以某种方式组合您的两个数据范围,以便您可以使用一个pointplot使用hue> hue变量:

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

#Set seed for reproducibility
np.random.seed(42)

#Create some dummy data
june_avg = pd.DataFrame({'Hour': range(24), "bike_count": np.random.randint(800,1000,size=(24,))})
houravg = pd.DataFrame({'Hour': range(24), "bike_count": np.random.randint(400,600,size=(24,))})

#Introduce Extra column that we use for the hue keyword. Content is what we want in the legend
june_avg["Avg Bike Rent"] = "June"
houravg["Avg Bike Rent"] = "Annual"

#Settings from your question
fig, ax = plt.subplots(figsize=(12, 6))
sns.set(style='whitegrid')

#Single plot command
sns.pointplot(data=pd.concat([june_avg,houravg ]), x="Hour", y="bike_count", hue="Avg Bike Rent", ax=ax)

plt.show()

输出:输出:

Instead of trying to change the legend afterwards, which this related post also has no solution for, I would suggest to combine your two dataframes in a way so that you can have a single pointplot call with a hue variable:

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

#Set seed for reproducibility
np.random.seed(42)

#Create some dummy data
june_avg = pd.DataFrame({'Hour': range(24), "bike_count": np.random.randint(800,1000,size=(24,))})
houravg = pd.DataFrame({'Hour': range(24), "bike_count": np.random.randint(400,600,size=(24,))})

#Introduce Extra column that we use for the hue keyword. Content is what we want in the legend
june_avg["Avg Bike Rent"] = "June"
houravg["Avg Bike Rent"] = "Annual"

#Settings from your question
fig, ax = plt.subplots(figsize=(12, 6))
sns.set(style='whitegrid')

#Single plot command
sns.pointplot(data=pd.concat([june_avg,houravg ]), x="Hour", y="bike_count", hue="Avg Bike Rent", ax=ax)

plt.show()

Output:
enter image description here

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