今天我们直奔主题,分享一种使用 Matplotlib 进行有趣的数据可视化的案例!在我的印象中,Matplotlib 属于那种中规中矩的可视化库,能画出“扭扭捏捏”的可视化图片,着实让你吃惊,欢迎收藏学习,喜欢点赞支持。

我们使用 Matplotlib 创建类似 xkcd 的绘图,并可以在这个项目中同 Matplotlib 可视化组合起来,让整个数据分析变得更有趣。

下面我们先来看看数据吧

数据集

我们可以在 Kaggle 上找到 Netflix 数据集,截至 2020 年,已经包含 7787 部 Netflix 上可用的电影和电视节目的数据

下面就先查看数据

import pandas as pd
import matplotlib.pyplot as plt
plt.rcParams['figure.dpi'] = 200df = pd.read_csv("../input/netflix-shows/netflix_titles.csv")
df.head()

接下来我们向数据集当中增加一些新功能,我们后面使用

df["date_added"] = pd.to_datetime(df['date_added'])
df['year_added'] = df['date_added'].dt.year.astype('Int64')
df['month_added'] = df['date_added'].dt.monthdf['season_count'] = df.apply(lambda x : x['duration'].split(" ")[0] if "Season" in x['duration'] else "", axis = 1)
df['duration'] = df.apply(lambda x : x['duration'].split(" ")[0] if "Season" not in x['duration'] else "", axis = 1)
df.head()

下面我们就可以进入有趣的数据分析了

当然,如果要在 Matplotlib 中使用 XKCDify 可视化,还需要添加如下代码

with plt.xkcd():

1.Netflix 时间轴

我们先查看一个描述 Netflix 多年来演变的时间表

from datetime import datetime# these go on the numbers below
tl_dates = ["1997\nFounded","1998\nMail Service","2003\nGoes Public","2007\nStreaming service","2016\nGoes Global","2021\nNetflix & Chill"
]
tl_x = [1, 2, 4, 5.3, 8,9]# the numbers go on these
tl_sub_x = [1.5,3,5,6.5,7]tl_sub_times = ["1998","2000","2006","2010","2012"
]tl_text = ["Netflix.com launched","Starts\nPersonal\nRecommendations","Billionth DVD Delivery","Canadian\nLaunch","UK Launch"]with plt.xkcd():
# Set figure & Axesfig, ax = plt.subplots(figsize=(15, 4), constrained_layout=True)ax.set_ylim(-2, 1.75)ax.set_xlim(0, 10)# Timeline : lineax.axhline(0, xmin=0.1, xmax=0.9, c='deeppink', zorder=1)# Timeline : Date Pointsax.scatter(tl_x, np.zeros(len(tl_x)), s=120, c='palevioletred', zorder=2)ax.scatter(tl_x, np.zeros(len(tl_x)), s=30, c='darkmagenta', zorder=3)# Timeline : Time Pointsax.scatter(tl_sub_x, np.zeros(len(tl_sub_x)), s=50, c='darkmagenta',zorder=4)# Date Textfor x, date in zip(tl_x, tl_dates):ax.text(x, -0.55, date, ha='center',fontfamily='serif', fontweight='bold',color='royalblue',fontsize=12)# Stemplot : vertical linelevels = np.zeros(len(tl_sub_x))levels[::2] = 0.3levels[1::2] = -0.3markerline, stemline, baseline = ax.stem(tl_sub_x, levels, use_line_collection=True)plt.setp(baseline, zorder=0)plt.setp(markerline, marker=',', color='darkmagenta')plt.setp(stemline, color='darkmagenta')# Textfor idx, x, time, txt in zip(range(1, len(tl_sub_x)+1), tl_sub_x, tl_sub_times, tl_text):ax.text(x, 1.3*(idx%2)-0.5, time, ha='center',fontfamily='serif', fontweight='bold',color='royalblue', fontsize=11)ax.text(x, 1.3*(idx%2)-0.6, txt, va='top', ha='center',fontfamily='serif',color='royalblue')# Spinefor spine in ["left", "top", "right", "bottom"]:ax.spines[spine].set_visible(False)# Ticksax.set_xticks([])ax.set_yticks([])# Titleax.set_title("Netflix through the years", fontweight="bold", fontfamily='serif', fontsize=16, color='royalblue')ax.text(2.4,1.57,"From DVD rentals to a global audience of over 150m people - is it time for Netflix to Chill?", fontfamily='serif', fontsize=12, color='mediumblue')plt.show()

上图展示了 Netflix 旅程当中一幅相当不错的画面, 此外,由于 plt.xkcd() 函数,这幅图看起来是手绘的,看起来确实很棒!

2.电影和电视节目

接下来我们看一下电影与电视节目的比例

col = "type"
grouped = df[col].value_counts().reset_index()
grouped = grouped.rename(columns = {col : "count", "index" : col})with plt.xkcd():explode = (0, 0.1)  # only "explode" the 2nd slice (i.e. 'TV Show')fig1, ax1 = plt.subplots(figsize=(5, 5), dpi=100)ax1.pie(grouped["count"], explode=explode, labels=grouped["type"], autopct='%1.1f%%',shadow=True, startangle=90)ax1.axis('equal')  # Equal aspect ratio ensures that pie is drawn as a circle.plt.show()

平台上的电视节目数量不到总内容的三分之一, 所以,与 Netflix 上的电视节目相比,我们可能更有机会找到一部相对较好的电影。

3.内容最多的国家

这一次我们制作一个水平条形图,代表内容最多的前 25 个国家/地区。DataFrame 中的 country 列有几行包含 1 个以上的国家。

from collections import Counter
col = "country"categories = ", ".join(df[col].fillna("")).split(", ")
counter_list = Counter(categories).most_common(25)
counter_list = [_ for _ in counter_list if _[0] != ""]
labels = [_[0] for _ in counter_list]
values = [_[1] for _ in counter_list]with plt.xkcd():fig, ax = plt.subplots(figsize=(10, 10), dpi=100)y_pos = np.arange(len(labels))ax.barh(y_pos, values, align='center')ax.set_yticks(y_pos)ax.set_yticklabels(labels)ax.invert_yaxis()  # labels read top-to-bottomax.set_xlabel('Content')ax.set_title('Countries with most content')plt.show()

看完上图后的一些总体看法:

  • Netflix 上的绝大多数内容都来自美国

  • 尽管 Netflix 在印度起步较晚(2016 年),但它已经排在美国之后的第二位。因此,印度是 Netflix 的一个大市场。

4.受欢迎的导演和演员

为了看看受欢迎的导演和演员,我们决定绘制一个图形,其中包含来自内容最多的前六个国家的六个子情节,并为每个子情节制作水平条形图。我们来看看下面的图

a.最受欢迎的导演

from collections import Counter
from matplotlib.pyplot import figure
import mathcolours = ["orangered", "mediumseagreen", "darkturquoise", "mediumpurple", "deeppink", "indianred"]
countries_list = ["United States", "India", "United Kingdom", "Japan", "France", "Canada"]
col = "director"with plt.xkcd():figure(num=None, figsize=(20, 8))x=1for country in countries_list:country_df = df[df["country"]==country]categories = ", ".join(country_df[col].fillna("")).split(", ")counter_list = Counter(categories).most_common(6)counter_list = [_ for _ in counter_list if _[0] != ""]labels = [_[0] for _ in counter_list][::-1]values = [_[1] for _ in counter_list][::-1]if max(values)<10:values_int = range(0, math.ceil(max(values))+1)else:values_int = range(0, math.ceil(max(values))+1, 2)plt.subplot(2, 3, x)plt.barh(labels,values, color = colours[x-1])plt.xticks(values_int)plt.title(country)x+=1plt.suptitle('Popular Directors with the most content')plt.tight_layout()plt.show()

b.最受欢迎的演员

col = "cast"with plt.xkcd():figure(num=None, figsize=(20, 8))x=1for country in countries_list:df["from_country"] = df['country'].fillna("").apply(lambda x : 1 if country.lower() in x.lower() else 0)small = df[df["from_country"] == 1]cast = ", ".join(small['cast'].fillna("")).split(", ")tags = Counter(cast).most_common(11)tags = [_ for _ in tags if "" != _[0]]labels, values = [_[0]+"  " for _ in tags][::-1], [_[1] for _ in tags][::-1]if max(values)<10:values_int = range(0, math.ceil(max(values))+1)elif max(values)>=10 and max(values)<=20:values_int = range(0, math.ceil(max(values))+1, 2)else:values_int = range(0, math.ceil(max(values))+1, 5)plt.subplot(2, 3, x)plt.barh(labels,values, color = colours[x-1])plt.xticks(values_int)plt.title(country)x+=1plt.suptitle('Popular Actors with the most content')plt.tight_layout()plt.show()

5.一些最古老的电影和电视节目

先来查看最古老的电影

a.古老的电影

small = df.sort_values("release_year", ascending = True)
small = small[small['duration'] != ""].reset_index()
small[['title', "release_year"]][:15]

b.古老的电视节目

small = df.sort_values("release_year", ascending = True)
small = small[small['season_count'] != ""].reset_index()
small = small[['title', "release_year"]][:15]
small

哇,Netflix 有一些非常老的电影和电视节目——有些甚至在 80 多年前就已经发行了, 你看过这些吗?

6.Netflix 有最新的内容吗?

是的,Netflix 确实很酷,而且拥有一个世纪前的内容,但它是否也有最新的电影和电视节目呢, 为了找到这一点,我们首先计算一下内容添加到 Netflix 的日期与该内容的发布年份之间的差异

df["year_diff"] = df["year_added"]-df["release_year"]

然后,我们创建了一个散点图,x 轴作为年份差异,y 轴作为电影/电视节目的数量:

col = "year_diff"
only_movies = df[df["duration"]!=""]
only_shows = df[df["season_count"]!=""]
grouped1 = only_movies[col].value_counts().reset_index()
grouped1 = grouped1.rename(columns = {col : "count", "index" : col})
grouped1 = grouped1.dropna()
grouped1 = grouped1.head(20)
grouped2 = only_shows[col].value_counts().reset_index()
grouped2 = grouped2.rename(columns = {col : "count", "index" : col})
grouped2 = grouped2.dropna()
grouped2 = grouped2.head(20)with plt.xkcd():figure(num=None, figsize=(8, 5))plt.scatter(grouped1[col], grouped1["count"], color = "hotpink")plt.scatter(grouped2[col], grouped2["count"], color = '#88c999')values_int = range(0, math.ceil(max(grouped1[col]))+1, 2)plt.xticks(values_int)plt.xlabel("Difference between the year when the content has been\n added on Netflix and the realease year")plt.ylabel("Number of Movies/TV Shows")plt.legend(["Movies", "TV Shows"])plt.tight_layout()plt.show()

正如我们在上面的可视化中看到的,Netflix 上的大部分内容都是在发布之日起一年内添加的。因此,Netflix 在大多数情况下确实拥有最新的内容!

下面轻松一下,看一个 xkcd 漫画给你,然后继续

7. Netflix 专注于什么样的内容?

我们继续浏览评级栏并比较 Netflix 为儿童、青少年和成人制作的内容量——以及这些年来他们的重点是否从一个群体转移到另一个群体。

我们首先查看了 DataFrame 中的独特评级:

print(df['rating'].unique())
output:
['TV-MA' 'R' 'PG-13' 'TV-14' 'TV-PG' 'NR' 'TV-G' 'TV-Y' nan 'TV-Y7' 'PG' 'G' 'NC-17' 'TV-Y7-FV' 'UR']

然后,我们根据他们所属的组(即小孩子、大孩子、青少年和成熟)对评级进行分类,并将评级列中的值更改为他们的组名称

ratings_list = ['TV-MA', 'R', 'PG-13', 'TV-14', 'TV-PG', 'TV-G', 'TV-Y', 'TV-Y7', 'PG', 'G', 'NC-17', 'TV-Y7-FV']
ratings_group_list = ['Little Kids', 'Older Kids', 'Teens', 'Mature']
ratings_dict={'TV-G': 'Little Kids','TV-Y': 'Little Kids','G': 'Little Kids','TV-PG': 'Older Kids','TV-Y7': 'Older Kids','PG': 'Older Kids','TV-Y7-FV': 'Older Kids','PG-13': 'Teens','TV-14': 'Teens','TV-MA': 'Mature','R': 'Mature','NC-17': 'Mature'
}
for rating_val, rating_group in ratings_dict.items():df.loc[df.rating == rating_val, "rating"] = rating_group

最后,我们绘制了 x 轴为年份、y 轴为内容计数的线图

df['rating_val']=1
x=0
labels=['kinda\nless', 'not so\nbad', 'holyshit\nthat\'s too\nmany']with plt.xkcd():for r in ratings_group_list:grouped = df[df['rating']==r]year_df = grouped.groupby(['year_added']).sum()year_df.reset_index(level=0, inplace=True)plt.plot(year_df['year_added'], year_df['rating_val'], color=colours[x], marker='o')values_int = range(2008, math.ceil(max(year_df['year_added']))+1, 2)plt.yticks([200, 600, 1000], labels)plt.xticks(values_int)plt.title('Count of shows and movies that Netflix\n has been producing for different audiences', fontsize=12)plt.xlabel('Year', fontsize=14)plt.ylabel('Content Count', fontsize=14)x+=1plt.legend(ratings_group_list)plt.tight_layout()plt.show()

这个可视化中的数据向我们展示了 Netflix 上成熟观众的内容数量远高于其他群体。另一个有趣的观察结果是,从 2019 年到 2020 年,为小孩子制作的内容数量激增,而在此期间,为大孩子、青少年和成熟观众制作的内容数量有所减少

8. 热门类型(国家)

col = "listed_in"
colours = ["violet", "cornflowerblue", "darkseagreen", "mediumvioletred", "blue", "mediumseagreen", "darkmagenta", "darkslateblue", "seagreen"]
countries_list = ["United States", "India", "United Kingdom", "Japan", "France", "Canada", "Spain", "South Korea", "Germany"]with plt.xkcd():figure(num=None, figsize=(20, 8))x=1for country in countries_list:df["from_country"] = df['country'].fillna("").apply(lambda x : 1 if country.lower() in x.lower() else 0)small = df[df["from_country"] == 1]genre = ", ".join(small['listed_in'].fillna("")).split(", ")tags = Counter(genre).most_common(3)tags = [_ for _ in tags if "" != _[0]]labels, values = [_[0]+"  " for _ in tags][::-1], [_[1] for _ in tags][::-1]if max(values)>200:values_int = range(0, math.ceil(max(values)), 100)elif max(values)>100 and max(values)<=200:values_int = range(0, math.ceil(max(values))+50, 50)else:values_int = range(0, math.ceil(max(values))+25, 25)plt.subplot(3, 3, x)plt.barh(labels,values, color = colours[x-1])plt.xticks(values_int)plt.title(country)x+=1plt.suptitle('Top Genres')plt.tight_layout()plt.show()

从上图可以看出:

  • 戏剧和喜剧是几乎每个国家最受欢迎的类型

  • 日本看了很多动漫

  • 浪漫的电视节目和电视剧在韩国很受欢迎

  • 儿童和家庭电影是加拿大第三大流行类型

9. 词云

我们最终用两个词云结束了这个项目——第一个是描述列的词云,第二个是标题列的词云。

a.描述列的词云

from wordcloud import WordCloud
import random
from PIL import Image
import matplotlib# Custom colour map based on Netflix palette
cmap = matplotlib.colors.LinearSegmentedColormap.from_list("", ['#221f1f', '#b20710'])
text = str(list(df['description'])).replace(',', '').replace('[', '').replace("'", '').replace(']', '').replace('.', '')
mask = np.array(Image.open('../input/finallogo/New Note.png'))
wordcloud = WordCloud(background_color = 'white', width = 500, height = 200,colormap=cmap, max_words = 150, mask = mask).generate(text)plt.figure( figsize=(5,5))
plt.imshow(wordcloud, interpolation = 'bilinear')
plt.axis('off')
plt.tight_layout(pad=0)
plt.show()

Live、love、life、friend、family、world 和 find 是一些最常出现在电影和节目描述中的词,另一件有趣的事情是词——一、二、三和四——都出现在词云中

b.标题的词云

cmap = matplotlib.colors.LinearSegmentedColormap.from_list("", ['#221f1f', '#b20710'])
text = str(list(df['title'])).replace(',', '').replace('[', '').replace("'", '').replace(']', '').replace('.', '')
mask = np.array(Image.open('../input/finallogo/New Note.png'))
wordcloud = WordCloud(background_color = 'white', width = 500, height = 200,colormap=cmap, max_words = 150, mask = mask).generate(text)plt.figure( figsize=(5,5))
plt.imshow(wordcloud, interpolation = 'bilinear')
plt.axis('off')
plt.tight_layout(pad=0)
plt.show()

我们看到圣诞节就在这个词云的中心, Netflix 上似乎有很多圣诞电影。其他流行的词是爱、世界、男人、生活、故事、现场、秘密、女孩、男孩、美国人、游戏、夜晚、最后、时间和白天。

我们只花了几个小时就完成了这个项目,现在我们能够以全新的方式看待 Netflix 所做的一切。

希望这个项目和所有使用 Matplotlib 的有趣数据可视化示例也能给大家的项目带来灵感。

好了,今天的分享就到这里了

原文:https://www.dataquest.io/blog/comical-data-visualization-in-python-using-matplotlib/

无奇不有! Matplotlib竟然也可画出“扭扭捏捏”的数据可视化图片!相关推荐

  1. 用php画一个蓝底红色的圆_PHP 画出 透明背景 的 圆形 图片程序

    PHP 想要用 GD 来画出圆形.椭圆形等等的图形,该怎么画呢?背景想要是透明的,要怎么做呢? PHP 画出 透明背景 的 圆形 图片程序 这边来示范下述: 图片画出 圆形 和 椭圆形 画两个不同的圆 ...

  2. python:基于matplotlib在坐标轴上画出车辆路径规划示意图(箭头、中文图例)

    车辆路径规划问题的研究一般较常遇到需要画出车辆路径示意图,已知有每辆车的真实坐标序列,那么如何利用在一个空白的坐标轴上画出路径呢? 1.准备 1.1 matplotlib引入 一般情况下只引入plt就 ...

  3. python如何画出多个独立的图片_python使用matplotlib:subplot绘制多个子图的示例

    数据可视化的时候,常常需要将多个子图放在同一个画板上进行比较,python 的matplotlib包下的subplot可以帮助完成子功能. part1 绘制如下子图 1 2 3 4 5 6 7 8 9 ...

  4. Python matplotlib作图实例,画一张想要的图片

    系列文章目录 可视化是科学数据的呈现比较理想的一种方式,怎样画一张自己想要的图片很重要. 提示:写完文章后,目录可以自动生成,如何生成可参考右边的帮助文档 文章目录 系列文章目录 前言 一.单张折线图 ...

  5. python画厢式图_Python数据可视化:箱线图多种库画法

    概念 箱线图通过数据的四分位数来展示数据的分布情况.例如:数据的中心位置,数据间的离散程度,是否有异常值等. 把数据从小到大进行排列并等分成四份,第一分位数(Q1),第二分位数(Q2)和第三分位数(Q ...

  6. python画熊猫论文_Python数据可视化之美:专业图表绘制指南(全彩)

    Python数据可视化之美:专业图表绘制指南(全彩)电子书 系统性地介绍Python 的绘图语法系统,包括matplotlib.Seaborn.plotnine 包,以及用于地理空间数据可视化的Bas ...

  7. python画平面直角坐标系_Python 数据可视化:重新认识坐标系

    在数据可视化中,坐标系是最基本的.在一张图纸上,只要建立了坐标系,图中的任何一个点就可以用数学方式描述清楚了. 一般,小学学习了数轴,初中就开始学习了二维直角坐标系.但是,那仅仅是数学中的认识,现在要 ...

  8. python画误差图_Python数据可视化:如何创建误差图

    一图胜千言,使用Python的matplotlib库,可以快速创建高质量的图形. 这是Python数据可视化的系列短篇,针对初级和中级用户,将理论和示例代码相结合,使用matplotlib, seab ...

  9. python如何画出多个独立的图片_在Python中用Matplotlib绘制多个图形并组合显示,利用,多图,合并,展示...

    有个需求就是利用Matplotlib画几个像模像样的统计图然后合并在一张图中,因为此前很少用这方面的东西,所以折腾了不少时间,今天介绍一下. 1.subplot多合一 其实,利用python 的mat ...

最新文章

  1. Oracle数据库表设计时的注意事项
  2. 用 Javascript 获取页面大小、窗口大小和滚动条位置
  3. html5父子页面数据传递,使用iframe标签嵌套页面时 如何进行父子页面通讯/传值...
  4. SpringMVC jpa
  5. 10、HTML的基本结构
  6. Java第一次读文件慢_Java 关于文件读取速度问题,求助,谢谢啦
  7. 计算机的键盘功能,电脑键盘功能
  8. docker学习笔记-6.容器关联,容器网络
  9. HardSoft-Viewer SQL
  10. 关于JSP表单的一些技巧和经验
  11. NEWSCTF第二届--官方wp(2021.6.1萌新赛)
  12. Build-dep linux 知乎,apt-get安装和apt-get build-dep有什么区别?
  13. BTC隔离见证(3开头)地址离线签名
  14. 多多进鱼带VUE源码-任务悬赏网站源码-活动营销三级分销返佣积分商城版
  15. 29. 尚融宝借款申请
  16. Python学习出来好找工作吗?好找工作的关键是什么?
  17. 初始化k8s时,报错[kubelet-check] It seems like the kubelet isn‘t running or healthy.
  18. 光量子计算机的功能,光量子计算机有什么用
  19. 优雅的进行线上数据订正
  20. mybatis根据传进来的参数执行不同的SQL语句

热门文章

  1. Python中bisect的使用方法
  2. java 字符串转义回车键_JAVA字符串中的转义字符
  3. Livox激光雷达硬件时间同步---PPS方法
  4. dvd加密格式:css
  5. patch文件介绍和生成方法
  6. 电子证书管理系统前台_访客管理系统是什么?公司需要访客管理系统吗?
  7. Brook+ Programming (1)
  8. if ((MessageBox.Show(確定要關閉嗎﹖此次輸入數據將會丟失﹗, 確認, MessageBoxButtons.YesNo, MessageBoxIcon.Warning) ==
  9. android 黑白,使用Android拍摄黑白照片(黑白)
  10. uploadifive不用flash上传图片插件,基于h5和jQuery上传图片插件