van*_*der 4 python matplotlib pandas 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()
Run Code Online (Sandbox Code Playgroud)
问题在于缺少数据.有31年的评级,但超过42年的时间跨度.这意味着应该有一些没有显示的空箱.有没有办法在Seaborn/Matplotlib中配置它?我应该使用其他类型的图表,还是有另一种解决方案?
我考虑过调查是否可以将其配置为时间序列,但我对评级量表存在同样的问题.因此,在1-10范围内,例如4的计数可能为零,因此"4"不在Pandas数据系列中,这意味着它也不会出现在图表中.
我想要的结果是x轴上的满刻度,y轴上的计数(步长为1),并显示缺少刻度实例的零/空箱,而不是简单地显示下一个箱哪些数据可用.
编辑:
数据(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"])
Run Code Online (Sandbox Code Playgroud)
它有更多的值,但格式是相同的.正如你在...中看到的
rated.year.value_counts()
Run Code Online (Sandbox Code Playgroud)
..有很多x值,图中的计数必须为零.目前的情节看起来像:
我通过在我的问题的评论中使用@mwaskom建议的解决方案解决了这个问题.即向countplot添加一个'order',其中包含年份的所有有效值,包括计数等于零的那些值.这是生成图形的代码:
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()
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1877 次 |
最近记录: |