【动手】
(1)英国和美国各自YouTube的数据结合之前的matplotlib绘制出各自的评论数量的直方图 ---- 直方图
注意:可以传列表,解决组距除不尽的情况
(2)希望了解英国的YouTube中视频的评论数和喜欢数的关系,应该如何绘制该图 ---- 散点图
注意:趋势是折线图,相关是散点图
【问题1】
英国和美国各自YouTube的数据结合之前的matplotlib绘制出各自的评论数量的直方图 ---- 直方图
注意:可以传列表,解决组距除不尽的情况
'''
点击, 喜欢, 不喜欢, 评论数量
([“views”,“likes”,“dislikes”,“comment_total”])
'''
import numpy as np
from matplotlib import pyplot as plt
us_path = './code/youtube_video_data/US_video_data_numbers.csv'
t_us = np.loadtxt(us_path,dtype='int',delimiter=',')
t_us_comment = t_us[:,-1]
print(t_us_comment.max(),t_us_comment.min())
d = 10000
bin_nums = (t_us_comment.max()-t_us_comment.min())//d
print(bin_nums)
plt.figure(figsize=(20,8),dpi=80)
plt.hist(t_us_comment,bin_nums)
plt.grid()
plt.show()
'''
(1)大多数数据集中在0---50000之间
(2)可以传列表,解决组距除不尽的情况
'''
582624 0
58
![在这里插入图片描述](https://img-blog.csdnimg.cn/20210510092311551.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQ0NjQ3NTU5,size_16,color_FFFFFF,t_70#pic_center)
'\n(1)大多数数据集中在0---50000之间\n(2)可以传列表,解决组距除不尽的情况\n'
'''
点击, 喜欢, 不喜欢, 评论数量
([“views”,“likes”,“dislikes”,“comment_total”])
'''
import numpy as np
from matplotlib import pyplot as plt
us_path = './code/youtube_video_data/US_video_data_numbers.csv'
t_us = np.loadtxt(us_path,dtype='int',delimiter=',')
print(t_us)
t_us = t_us[t_us[:,-1]<=50000]
t_us_comment = t_us[:,-1]
print(t_us_comment.max(),t_us_comment.min())
d = 250
bin_nums = (t_us_comment.max()-t_us_comment.min())//d
plt.figure(figsize=(20,8),dpi=80)
plt.hist(t_us_comment,bin_nums)
plt.grid()
plt.show()
'''
大多数数据集中在0---5000,故准备调整组距为50,组数为100
'''
[[4394029 320053 5931 46245]
[7860119 185853 26679 0]
[5845909 576597 39774 170708]
...
[ 142463 4231 148 279]
[2162240 41032 1384 4737]
[ 515000 34727 195 4722]]
46245 0
![在这里插入图片描述](https://img-blog.csdnimg.cn/20210510092329509.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQ0NjQ3NTU5,size_16,color_FFFFFF,t_70#pic_center)
'\n大多数数据集中在0---5000,故准备调整组距为50,组数为100\n'
'''
点击, 喜欢, 不喜欢, 评论数量
([“views”,“likes”,“dislikes”,“comment_total”])
'''
import numpy as np
from matplotlib import pyplot as plt
us_path = './code/youtube_video_data/US_video_data_numbers.csv'
t_us = np.loadtxt(us_path,dtype='int',delimiter=',')
print(t_us)
t_us = t_us[t_us[:,-1]<=5000]
t_us_comment = t_us[:,-1]
print(t_us_comment.max(),t_us_comment.min())
d = 250
bin_nums = (t_us_comment.max()-t_us_comment.min())//d
plt.figure(figsize=(20,8),dpi=80)
plt.hist(t_us_comment,bin_nums)
plt.grid()
plt.show()
'''
大多数数据集中在0---5000,故准备调整组距为250,组数为20
'''
[[4394029 320053 5931 46245]
[7860119 185853 26679 0]
[5845909 576597 39774 170708]
...
[ 142463 4231 148 279]
[2162240 41032 1384 4737]
[ 515000 34727 195 4722]]
4995 0
![在这里插入图片描述](https://img-blog.csdnimg.cn/20210510092419774.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQ0NjQ3NTU5,size_16,color_FFFFFF,t_70#pic_center)
'\n大多数数据集中在0---5000,故准备调整组距为250,组数为20\n'
【问题2】
希望了解英国的YouTube中视频的评论数和喜欢数的关系,应该如何绘制该图 ---- 散点图
注意:趋势是折线图,相关是散点图
'''
点击, 喜欢, 不喜欢, 评论数量
([“views”,“likes”,“dislikes”,“comment_total”])
'''
import numpy as np
from matplotlib import pyplot as plt
uk_path = './code/youtube_video_data/US_video_data_numbers.csv'
us_path = './code/youtube_video_data/US_video_data_numbers.csv'
t_uk = np.loadtxt(uk_path,dtype='int',delimiter=',')
print(t_uk)
t_uk_comment = t_uk[:,-1]
t_uk_like = t_uk[:,1]
plt.figure(figsize=(20,8),dpi=80)
plt.scatter(t_uk_like,t_uk_comment)
plt.show()
'''
大多数数据集中于 0 ---- 500000
保持数据的一致性
'''
[[4394029 320053 5931 46245]
[7860119 185853 26679 0]
[5845909 576597 39774 170708]
...
[ 142463 4231 148 279]
[2162240 41032 1384 4737]
[ 515000 34727 195 4722]]
![在这里插入图片描述](https://img-blog.csdnimg.cn/20210510092436785.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQ0NjQ3NTU5,size_16,color_FFFFFF,t_70#pic_center)
'''
点击, 喜欢, 不喜欢, 评论数量
([“views”,“likes”,“dislikes”,“comment_total”])
'''
import numpy
from matplotlib import pyplot as plt
uk_path = './code/youtube_video_data/US_video_data_numbers.csv'
t_uk = np.loadtxt(uk_path,dtype='int',delimiter=',')
print(t_uk)
t_uk = t_uk[t_uk[:,1]<=500000]
t_uk_comment = t_uk[:,-1]
t_uk_like = t_uk[:,1]
plt.figure(figsize=(20,8),dpi=80)
plt.scatter(t_uk_like,t_uk_comment)
plt.show()
[[4394029 320053 5931 46245]
[7860119 185853 26679 0]
[5845909 576597 39774 170708]
...
[ 142463 4231 148 279]
[2162240 41032 1384 4737]
[ 515000 34727 195 4722]]
![在这里插入图片描述](https://img-blog.csdnimg.cn/20210510092449843.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQ0NjQ3NTU5,size_16,color_FFFFFF,t_70#pic_center)