繁体   English   中英

使用Seaborn将y轴值限制为1的问题

[英]Problem with y-axis value limited to 1 using seaborn

我正在使用seaborn进行分类点图绘制,并且为日期分配了面积值(km2)。

当我绘制这些日期时,当我知道有多个值大于1时,y轴从0限制为1。

import numpy as np
import pandas as pd

import seaborn as sns
import matplotlib.pyplot as plt
from matplotlib.pyplot import figure


# Read in the backscatter csv file as a data frame
df_lakearea = pd.read_csv('lake_area.csv')

figure(num=None, figsize=(8, 6), dpi=300, facecolor='w', edgecolor='k')

# Control aesthetics 
sns.set()
sns.set(style="whitegrid", rc={"grid.linewidth": 0.2, "lines.linewidth": 0.5}) # White grid background, width of grid  line and series line
sns.set_context(font_scale = 0.5) # Scale of font

# Use seaborn pointplot function to plot the lake area 
lakearea_plot = sns.pointplot(x="variable", y="value", data=pd.melt(df_lakearea), color='maroon', linestyles=["-"], join="True", capsize=0.2)
# Use the pd.melt function to converts the wide-form data frame to long-form.

# Rotate the x axis labels so that they are readable
plt.setp(lakearea_plot.get_xticklabels(), rotation=20)

params = {'mathtext.default': 'regular' }
plt.rcParams.update(params)

lakearea_plot.set(xlabel='', ylabel='Area $(km^2)$')
lakearea_plot.tick_params(labelsize=8) # Control the label size

我希望结果看起来很像正常的时间序列图,分配给每个日期的值和达到最小和最大点的误差线,而不是在y轴上的最大值不为1。 下图显示了我所拥有的,y轴最大值为1。

到目前为止我有什么

先感谢您。

首先,当您在seaborn绘制分类点seaborn ,您的y值(数值)将根据每个类别汇总到均值。 让我们使用seaborn的数据集进行演示。

import seaborn as sns

df = sns.load_dataset('tips')
sns.pointplot(x='day', y='tip', data=df)

在此处输入图片说明

在此图中,您可以看到Thur的y值大约为2.8,这是因为Thur的提示的平均值为2.8-ish。 我们可以通过以下方式对此进行验证:

df.groupby('day').tip.mean()

[Out]:
day
Thur    2.771452
Fri     2.734737
Sat     2.993103
Sun     3.255132
Name: tip, dtype: float64

其次,您可能还注意到Fri的置信区间(CI)比其他组大。 实际上,这种线图中CI的大小表示您的样本大小,而不是数据分布。 我们可以通过以下方式对此进行验证:

df.day.value_counts()

[Out]:
df.day.value_counts()
Sat     87
Sun     76
Thur    62
Fri     19
Name: day, dtype: int64

如您所见,我们的数据集中只有19个与Fri相关的观测值。 因此,与其他组相比,我们对我们的估计(均值)“信心不足”。 这就是为什么它具有比其他团体更广泛的CI的原因。

这是另一个例子:

sns.regplot(x='total_bill', y='tip', data=df)

在此处输入图片说明

您可以说CI大约宽于50,因为那里只有几个数据点。

因此,您应该检查数据中每个组的平均值是否在y轴范围内,并且CI是否代表每个组中数据点的数量。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM