【问题标题】:how to plot categorical and continuous data in pandas/matplotlib/seaborn如何在 pandas/matplotlib/seaborn 中绘制分类和连续数据
【发布时间】:2020-06-20 10:56:15
【问题描述】:

我想知道如何绘制这些数据:

第 1 列 ['genres']:这些是表中所有类型的值计数

Drama              2453
Comedy             2319
Action             1590
Horror              915
Adventure           586
Thriller            491
Documentary         432
Animation           403
Crime               380
Fantasy             272
Science Fiction     214
Romance             186
Family              144
Mystery             125
Music               100
TV Movie             78
War                  59
History              44
Western              42
Foreign               9
Name: genres, dtype: int64

第 2 列 ['release_year']:这些是不同类型类型的所有发行年份的值计数

2014    699
2013    656
2015    627
2012    584
2011    540
2009    531
2008    495
2010    487
2007    438
2006    408
2005    363
2004    307
2003    281
2002    266
2001    241
2000    226
1999    224
1998    210
1996    203
1997    192
1994    184
1993    178
1995    174
1988    145
1989    136
1992    133
1991    133
1990    132
1987    125
1986    121
1985    109
1984    105
1981     82
1982     81
1983     80
1980     78
1978     65
1979     57
1977     57
1971     55
1973     55
1976     47
1974     46
1966     46
1975     44
1964     42
1970     40
1967     40
1972     40
1968     39
1965     35
1963     34
1962     32
1960     32
1969     31
1961     31
Name: release_year, dtype: int64

我需要回答以下问题 - 每年最受欢迎的类型是什么?等等

可以使用什么样的图,最好的方法是什么,因为单个图表中有很多箱?

seaborn 是否更适合绘制此类变量?

我应该将年份数据分为 2 个十年(1900 年和 2000 年)吗?

Sample of the table: 
    id   popularity runtime genres  vote_count  vote_average    release_year
0   135397  32.985763   124 Action     5562     6.5             2015
1   76341   28.419936   120 Action     6185     7.1             1995
2   262500  13.112507   119 Adventure  2480     6.3             2015
3   140607  11.173104   136 Thriller   5292     7.5             2013
4   168259  9.335014    137 Action     2947     7.3             2005

【问题讨论】:

  • 为什么不按年份对表格进行分组,然后计算类型?您应该向我们展示原始表格的样本。
  • 问题中添加的示例
  • 如果你有很多类型,也许线图是要走的路。顶行将是最受欢迎的类型。只要确保有一个清晰的图例并使用非常独特的颜色即可。

标签: python pandas matplotlib seaborn


【解决方案1】:

你可以这样做:

Plotting histogram using seaborn for a dataframe

我个人更喜欢 seaborn 来制作这种情节,因为它更容易。但是你也可以使用 matplotlib。

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

# sample data
samples = 300
ids = range(samples)
gind = np.random.randint(0, 4, samples)
years = np.random.randint(1990, 2000, samples)

# create sample dataframe
gkeys = {1: 'Drama', 2: 'Comedy', 3: 'Action', 4: 'Adventure', 0: 'Thriller'}
df = pd.DataFrame(zip(ids, gind, years),
                  columns=['ID', 'Genre', 'Year'])
df['Genre'] = df['Genre'].replace(gkeys)

# count the year groups
res = df.groupby(['Year', 'Genre']).count()
res = res.reset_index()

# only the max values
# res_ind = res.groupby(['Year']).idxmax()
# res = res.loc[res_ind['ID'].tolist()]

# viz
sns.set(style="white")
g = sns.catplot(x='Year',
                y= 'ID',
                hue='Genre',
                data=res,
                kind='bar',
                ci=None,
                   )
g.set_axis_labels("Year", "Count")
plt.show()

如果这是一个情节中的许多垃圾箱,只需将其拆分即可。

【讨论】:

  • 我认为最好在 y 轴上使用年份计数,然后在 x 轴上表示该特定年份的流派计数最高的每个 bin,不应该使用投票计数或此比较所必需的。
  • 我已经编辑了我的帖子。如果您取消注释该部分,则最大值您每年只会看到一个条形图。但是酒吧的外观还不完美。也许我明天改变这个。您可以创建热图,而不是条形图。
  • 感谢您更新代码,我是数据分析师领域的新手,希望在进入高级级别之前掌握基础知识。我认为现在条形图就足够了。
猜你喜欢
  • 2015-09-10
  • 2019-08-22
  • 1970-01-01
  • 1970-01-01
  • 2020-11-23
  • 2019-08-14
  • 2017-06-28
  • 2017-08-04
  • 1970-01-01
相关资源
最近更新 更多