笔记本Python中直方图子图的错误

发布于 2025-01-31 01:57:32 字数 1077 浏览 2 评论 0原文

我在做什么错?我刚刚开始学习Python而不是R。 我试图在彼此相邻完成三个直方图。他们每个人都是由另一个机场从专栏起点分组的。

这就是我想要看起来的方式

import pandas as pd
import seaborn 
import matplotlib.pyplot as plt 

flights2_EWR=flights2.loc[flights2['origin'] == 'EWR']
histEWR=seaborn.histplot(x="delay", bins=12, palette="pastel",
                 data=flights2_EWR)

flights2_EWR=flights2.loc[flights2['origin'] == 'EWR']
histEWR=plt.hist(x="delay", bins=12, 
                 data=flights2_EWR)

flights2_JFK=flights2.loc[flights2['origin'] == 'JFK']
histJFK=plt.hist(x="delay", bins=12,
                 data=flights2_JFK)

flights2_LGA=flights2.loc[flights2['origin'] == 'LGA']
histLGA=plt.hist(x="delay", bins=12, 
                 data=flights2_LGA)

plt.subplot(1, 3, 1) 
plt.hist(histEWR)
plt.subplot(1, 3, 2)  
plt.hist(histJFK)
plt.subplot(1, 3, 3)  
plt.hist(histLGA)
plt.show() 

​< ='在'矩形'和'矩形''之间不支持'

What am I doing wrong? I've just started learning Python instead of R.
I'm trying to accomplish three histograms next to each other. Each of them is grouped by another airport from column origin.

This is how I want it too look

I can do sth like this, but it's not enough for me

packages:

import pandas as pd
import seaborn 
import matplotlib.pyplot as plt 

First I tried with seaborn:

flights2_EWR=flights2.loc[flights2['origin'] == 'EWR']
histEWR=seaborn.histplot(x="delay", bins=12, palette="pastel",
                 data=flights2_EWR)

There is code with matplotlib

flights2_EWR=flights2.loc[flights2['origin'] == 'EWR']
histEWR=plt.hist(x="delay", bins=12, 
                 data=flights2_EWR)

flights2_JFK=flights2.loc[flights2['origin'] == 'JFK']
histJFK=plt.hist(x="delay", bins=12,
                 data=flights2_JFK)

flights2_LGA=flights2.loc[flights2['origin'] == 'LGA']
histLGA=plt.hist(x="delay", bins=12, 
                 data=flights2_LGA)

plt.subplot(1, 3, 1) 
plt.hist(histEWR)
plt.subplot(1, 3, 2)  
plt.hist(histJFK)
plt.subplot(1, 3, 3)  
plt.hist(histLGA)
plt.show() 

TypeError: '<=' not supported between instances of 'Rectangle' and 'Rectangle'

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

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

发布评论

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

评论(1

七颜 2025-02-07 01:57:32

嗨,欢迎来到堆栈溢出。您可以使用 seaborn Distplot col = Origin(JFK,EWR,LGA)实现这一目标。它将以您的寻找方式创建多个子图。由于我没有您所指的数据,因此使用Seaborn默认情况下使用了企鹅数据集。

代码

penguins = sns.load_dataset("penguins") # Not required in your case as you already have your flights2 dataframe
sns.displot(penguins, x="flipper_length_mm", col="sex", bins=12, hue = 'sex', palette="pastel")

输出图

Hi and welcome to Stack Overflow. You can achieve this using seaborn distplot and the col = origin (JFK, EWR, LGA). It will create multiple subplots the way you are looking for. As I don't have the data you are referring to, used penguins dataset available by default in seaborn.

Code

penguins = sns.load_dataset("penguins") # Not required in your case as you already have your flights2 dataframe
sns.displot(penguins, x="flipper_length_mm", col="sex", bins=12, hue = 'sex', palette="pastel")

Output Graph
Graph

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