我试图在seaborn中旋转标签45°,但得到ValueError: "Grouper和轴的长度必须相同"

我对Python相当陌生 (我正试图自己学习Data Sciente,但我实际上是个律师出身),我正试图让一个条形图将它的X Labels旋转45°以使它们可读(就像现在一样,有重叠)。我正在使用MovieLens数据集,并制作一个统计每个类型的电影数量的图表。这是我目前的代码。

import seaborn as sns
import matplotlib.pyplot as plt
sns.set_style("whitegrid")

filmes_por_genero = filmes["generos"].str.get_dummies('|').sum().sort_values(ascending=False)
genero = filmes_com_media.index

chart = plt.figure(figsize=(16,8))
sns.barplot(x=genero,
            y=filmes_por_genero.values,
            palette=sns.color_palette("BuGn_r", n_colors=len(filmes_por_genero) + 4)
            )

chart.set_xticklabels(
    chart.get_xticklabels(), 
    rotation=45, 
    horizontalalignment='right'
)

这是完整的错误信息

/usr/local/lib/python3.6/dist-packages/pandas/core/groupby/grouper.py in get_grouper(obj, key, axis, level, sort, observed, mutated, validate)
    623                 in_axis=in_axis,
    624             )
--> 625             if not isinstance(gpr, Grouping)
    626             else gpr
    627         )

/usr/local/lib/python3.6/dist-packages/pandas/core/groupby/grouper.py in __init__(self, index, grouper, obj, name, level, sort, observed, in_axis)
    254         self.name = name
    255         self.level = level
--> 256         self.grouper = _convert_grouper(index, grouper)
    257         self.all_grouper = None
    258         self.index = index

/usr/local/lib/python3.6/dist-packages/pandas/core/groupby/grouper.py in _convert_grouper(axis, grouper)
    653     elif isinstance(grouper, (list, Series, Index, np.ndarray)):
    654         if len(grouper) != len(axis):
--> 655             raise ValueError("Grouper and axis must be same length")
    656         return grouper
    657     else:

ValueError: Grouper and axis must be same length

解决方案:

import pandas as pd
import mathplotlib.pyplot as plt
import seaborn as sns
sns.set_style("whitegrid")

# data
df = pd.read_csv('ml-25m/movies.csv')

print(df.head())

   movieId                               title                                       genres
0        1                    Toy Story (1995)  Adventure|Animation|Children|Comedy|Fantasy
1        2                      Jumanji (1995)                   Adventure|Children|Fantasy
2        3             Grumpier Old Men (1995)                               Comedy|Romance
3        4            Waiting to Exhale (1995)                         Comedy|Drama|Romance
4        5  Father of the Bride Part II (1995)                                       Comedy

# clean genres
df['genres'] = df['genres'].str.split('|')
df = df.explode('genres')

print(df.head())

   movieId             title     genres
0        1  Toy Story (1995)  Adventure
0        1  Toy Story (1995)  Animation
0        1  Toy Story (1995)   Children
0        1  Toy Story (1995)     Comedy
0        1  Toy Story (1995)    Fantasy

类型数量

genre_count = pd.DataFrame(df.genres.value_counts())

print(genre_count)

                    genres
Drama                25606
Comedy               16870
Thriller              8654
Romance               7719
Action                7348
Horror                5989
Documentary           5605
Crime                 5319
(no genres listed)    5062
Adventure             4145
Sci-Fi                3595
Children              2935
Animation             2929
Mystery               2925
Fantasy               2731
War                   1874
Western               1399
Musical               1054
Film-Noir              353
IMAX                   195

情节

plt.figure(figsize=(16,8))
chart = sns.barplot(genre_count.index, genre_count.genres, palette=sns.color_palette("BuGn_r", n_colors=len(genre_count) + 4))
chart.set_xticklabels(chart.get_xticklabels(), rotation=45, horizontalalignment='right')
plt.show()

enter image description here

给TA打赏
共{{data.count}}人
人已打赏
解决方案

何时为春季启动执行指定多个配置文件

2022-4-22 2:00:12

解决方案

Angular 8 - 当点击菜单栏上href中提到的路由路径时,路由不工作。

2022-4-22 2:00:14

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索