在一个图上覆盖概率密度函数

发布于 2025-01-23 12:27:08 字数 1479 浏览 0 评论 0 原文

我想为来自三个NOX源的N的同位素测量创建概率密度函数。测量量之间的数量在来源之间有所不同,因此我创建了三个数据范围。这是代码:

import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
#import matplotlib.ticker as plticker
#from matplotlib.ticker import (MultipleLocator, AutoMinorLocator)


df = pd.DataFrame({
    'Mobile':[15.6, 14.2, 14.4, 10.2, 13.1, 12.8, 13.3, 16.9, 15.8, 15.3, 16.9, 15.6, 15.6, 17, 16, 15.1, 15, 14.4,
              14.6, 16.2, 15.3, 16.4, -0.4, -2.9, 1.6, 9.8, 1.6, -8.1, -4.4, -0.4, 8.6]})
    
df1 = pd.DataFrame({
    'Soil':[-47, -37, -29, -26, -25, -24, -31, -23, -22, -19, -49, -42, -44, -37, -29, -29, -32, -31, -29, -28,
            -26.5, -30.8]})
df2 = pd.DataFrame({
    'Biomass Burning':[-2.7, -5, -5.9, -7.2, 3.2, 2.6, 3.8, 8.1, 12, 0.9, 1.3, 1.6, -1.5, -1.3, -0.1, 0.5, 4.4, 2,
                       2.9, 1.7, 3.2, 1.6, -0.3, -0.9]})

fig = plt.figure()
ax = fig.add_subplot()
ax.hist([df, df1, df2], label = ("Mobile", "Soil", "Biomass Burning"), bins=25, stacked=True, range=[0,25])

问题是我收到一条错误消息,上面写着: valueerror:x必须具有2个或更少的维度。我已经尝试了一种“胖”方法,但是收到一个错误消息,该错误消息说 attributeError:'dataFrame'对象没有属性'Flatten'。我不确定接下来要做什么以使代码运行并可以使用一些帮助。我还认为 Hist 可能是使用错误的功能,因为我想要概率密度分布。我也尝试过:

sns.displot(data=[df,df1,df2], x=['Mobile','Soil','Biomass Burning'], hue='target', kind='kde', 
            fill=True, palette=sns.color_palette('bright')[:3], height=5, aspect=1.5)

但是,我再次遇到了数据范围的问题不同。谢谢!

I would like to create a probability density function for the isotopic measurements of N from three NOx sources. The number of measurements varies between sources, so I've created three dataframes. Here is the code:

import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
#import matplotlib.ticker as plticker
#from matplotlib.ticker import (MultipleLocator, AutoMinorLocator)


df = pd.DataFrame({
    'Mobile':[15.6, 14.2, 14.4, 10.2, 13.1, 12.8, 13.3, 16.9, 15.8, 15.3, 16.9, 15.6, 15.6, 17, 16, 15.1, 15, 14.4,
              14.6, 16.2, 15.3, 16.4, -0.4, -2.9, 1.6, 9.8, 1.6, -8.1, -4.4, -0.4, 8.6]})
    
df1 = pd.DataFrame({
    'Soil':[-47, -37, -29, -26, -25, -24, -31, -23, -22, -19, -49, -42, -44, -37, -29, -29, -32, -31, -29, -28,
            -26.5, -30.8]})
df2 = pd.DataFrame({
    'Biomass Burning':[-2.7, -5, -5.9, -7.2, 3.2, 2.6, 3.8, 8.1, 12, 0.9, 1.3, 1.6, -1.5, -1.3, -0.1, 0.5, 4.4, 2,
                       2.9, 1.7, 3.2, 1.6, -0.3, -0.9]})

fig = plt.figure()
ax = fig.add_subplot()
ax.hist([df, df1, df2], label = ("Mobile", "Soil", "Biomass Burning"), bins=25, stacked=True, range=[0,25])

The problem is that I get an error message that says: ValueError: x must have 2 or fewer dimensions. I've tried a "fatten" method but get an error message that says AttributeError: 'DataFrame' object has no attribute 'flatten'. I am unsure of what to try next to get the code to run and could use some help. I am also thinking that hist might be the wrong function to use since I want a probability density distribution. I've also tried:

sns.displot(data=[df,df1,df2], x=['Mobile','Soil','Biomass Burning'], hue='target', kind='kde', 
            fill=True, palette=sns.color_palette('bright')[:3], height=5, aspect=1.5)

But again, I run into the issue of the dataframes being different lengths. Thanks!

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

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

发布评论

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

评论(1

单身狗的梦 2025-01-30 12:27:08

一个选项是熔融 dataframes, concat 它们,然后使用 hue strendot

data = pd.concat([df.melt(), df1.melt(), df2.melt()], ignore_index=True)
sns.displot(data=data, x='value', hue='variable', kind='kde')

outption:output:

使用 var_name value_name 熔融>熔融 对于比“变量”和“ value”的有意义的标识符,例如

kws = {'var_name': 'Source', 'value_name': 'Measurements'}
data  = pd.concat([df.melt(**kws), df1.melt(**kws), df2.melt(**kws)], 
                  ignore_index=True)
sns.displot(
    data=data, x='Measurements', hue='Source', kind='kde', 
    fill=True, palette=sns.color_palette('bright')[:3], height=5, aspect=1.5
)

One option is to melt the dataframes, concat them, and then use hue with displot:

data = pd.concat([df.melt(), df1.melt(), df2.melt()], ignore_index=True)
sns.displot(data=data, x='value', hue='variable', kind='kde')

Output:

enter image description here

Use the var_name and value_name parameters of melt for more meaningful identifiers than "variable" and "value", e.g.

kws = {'var_name': 'Source', 'value_name': 'Measurements'}
data  = pd.concat([df.melt(**kws), df1.melt(**kws), df2.melt(**kws)], 
                  ignore_index=True)
sns.displot(
    data=data, x='Measurements', hue='Source', kind='kde', 
    fill=True, palette=sns.color_palette('bright')[:3], height=5, aspect=1.5
)

Output:

enter image description here

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