Pandas 为什么Seaborn一直在x轴上绘制不存在的范围值?

Pandas 为什么Seaborn一直在x轴上绘制不存在的范围值?,pandas,seaborn,Pandas,Seaborn,片段: import pandas as pd import numpy as np import matplotlib.pyplot as plt import seaborn as sns test = pd.DataFrame({'value':[1,2,5,7,8,10,11,12,15,16,18,20,36,37,39]}) test['range'] = pd.cut(test.value, np.arange(0,45,5)) # generate range test = t

片段:

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

test = pd.DataFrame({'value':[1,2,5,7,8,10,11,12,15,16,18,20,36,37,39]})
test['range'] = pd.cut(test.value, np.arange(0,45,5)) # generate range
test = test.groupby('range')['value'].count().to_frame().reset_index() # count occurance in each range
test = test[test.value!=0] #filter out rows with value = 0

plt.figure(figsize=(10,5))
plt.xticks(rotation=90)
plt.yticks(np.arange(0,10, 1))
sns.barplot(x=test.range, y=test.value)
test['range'] = test['range'].cat.remove_unused_categories()
输出:

如果我们看一下
测试中的内容

     range   value
0   (0, 5]      3
1   (5, 10]     3
2   (10, 15]    3
3   (15, 20]    3
7   (35, 40]    3
范围
(20,25],(25,30],(30,35]
已被过滤掉,但它们仍然出现在绘图中。这是为什么?如何输出没有空范围的绘图


p.S.@jezrael的解决方案与上面的代码片段完美结合。我在一个真实的数据集上进行了尝试:

片段:

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

test = pd.DataFrame({'value':[1,2,5,7,8,10,11,12,15,16,18,20,36,37,39]})
test['range'] = pd.cut(test.value, np.arange(0,45,5)) # generate range
test = test.groupby('range')['value'].count().to_frame().reset_index() # count occurance in each range
test = test[test.value!=0] #filter out rows with value = 0

plt.figure(figsize=(10,5))
plt.xticks(rotation=90)
plt.yticks(np.arange(0,10, 1))
sns.barplot(x=test.range, y=test.value)
test['range'] = test['range'].cat.remove_unused_categories()
警告:

A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead
我使用以下方法来避免警告:

test['range'].cat.remove_unused_categories(inplace=True)
这是由于使用多个变量造成的,因此请注意:

test = blah blah blah
test_df = test[test.value!=0]
test_df['range'] = test_df['range'].cat.remove_unused_categories() # warning!

有一个问题
range
列是,所以类别不会像在另一个列中那样被设计删除

你需要:


如果您稍后修改
test\u df
中的值,您会发现修改不会传播回原始数据(
test
),而且Pandas会发出警告。

WOW,我甚至不知道分类变量没有被删除!Thx@jezrael!您介意看看我遇到的另一个问题吗:非常感谢!