python ,Seaborn : Plotting frequencies with zero-values

标签 python pandas matplotlib seaborn

我有一个 Pandas 系列,其中包含我想要绘制计数的值。这大致创建了我想要的:

dy = sns.countplot(rated.year, color="#53A2BE")
axes = dy.axes
dy.set(xlabel='Release Year', ylabel = "Count")
dy.spines['top'].set_color('none')
dy.spines['right'].set_color('none')
plt.show()

问题来自数据缺失。评级有 31 年,但时间跨度超过 42 年。这意味着应该有一些没有显示的空箱子。有没有办法在 Seaborn/Matplotlib 中配置它?我应该使用其他类型的图表,还是有其他解决方法?

我考虑过研究是否可以将其配置为时间序列,但我对等级量表也有同样的问题。因此,在 1-10 的范围内,例如4 可能为零,因此“4”不在 Pandas 数据系列中,这意味着它也不会出现在图表中。

我想要的结果是 x 轴上的完整刻度,y 轴上的计数(步长为 1),并显示零/空 bin 以表示缺少的刻度实例,而不是简单地显示数据可用的下一个 bin。

编辑:

数据 (rated.year) 看起来像这样:

import pandas as pd

rated = pd.DataFrame(data = [2016, 2004, 2007, 2010, 2015, 2016, 2016, 2015,
                             2011, 2010, 2016, 1975, 2011, 2016, 2015, 2016, 
                             1993, 2011, 2013, 2011], columns = ["year"])

它有更多的值,但格式是一样的。正如您在...中看到的那样

rated.year.value_counts()

..图中有很多 x 值的计数必须为零。目前情节看起来像:

Seaborn Plot

最佳答案

我通过使用 @mwaskom 在我的问题的评论中建议的解决方案解决了这个问题。 IE。向计数图中添加一个“订单”,其中包含年份的所有有效值,包括那些计数为零的值。这是生成图表的代码:

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

rated = pd.DataFrame(data = [2016, 2004, 2007, 2010, 2015, 2016, 2016, 2015,
                             2011, 2010, 2016, 1975, 2011, 2016, 2015, 2016, 
                             1993, 2011, 2013, 2011], columns = ["year"])

dy = sns.countplot(rated.year, color="#53A2BE", order = list(range(rated.year.min(),rated.year.max()+1)))
axes = dy.axes
dy.set(xlabel='Release Year', ylabel = "Count")
dy.spines['top'].set_color('none')
dy.spines['right'].set_color('none')
plt.show()

关于 python ,Seaborn : Plotting frequencies with zero-values,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45352766/

相关文章:

python - 使用 PIL 获取像素的 RGB

python - 将 LabelEncoder 用于 scikit learn 中的一系列

python - 有没有一种方法可以找到/检测两个像素的中间或opencv创建的两行?

python - matplotlib 中矩形面片之间存在不需要的空间

python - 尝试加载腌制的 matplotlib 图时出现 AttributeError

python - 我在 python 中的程序上出现语义错误,该程序上升然后又下降

python - python中的ANOVA使用带有statsmodels或scipy的pandas数据框?

Python string.strip 剥离太多字符

python - Pandas Timedelta 以天为单位

python - 如何将 pandas 列中的浮点值离散为 [1, 10]