之前有一篇關於爬蟲的博客,今天寫一篇對數據的處理操作的文章。這裏只是選取了部分電影信息做參考(可以將數據庫信息導出,轉爲json格式—容易處理,如果你有耐心可以將其改爲文本文件,但是處理起來非常麻煩,也沒能那個必要,哈哈…),電影信息包括(id,name,type,country,language,releasetime,ranking)。
因爲時間有限,所以只做了下圖中的的餅圖“類型佔比”和柱狀圖“type”(色彩可能會略有不適),另外兩個當作參考,隨便寫的。
代碼:
import matplotlib.pyplot as plt
import numpy as np
# 1.線圖
# figure
figure = plt.figure()
subplot = figure.add_subplot(2, 2, 1)
subplot.plot(np.arange(0, 100), np.arange(1, 101))
# 2.柱狀圖
subplot2 = figure.add_subplot(2, 2, 2)
data = np.array(((1, '奔騰年代', '劇情片', '美國', '英語', '2003', 4.0),
(2, '逃出珊瑚海', '恐怖片', '香港', '國語', '1986', 4.0),
(3, '甜蜜的謊言', '喜劇片', '韓國', '韓語', '2008', 10.0),
(4, '他鄉的童年', '電影', '大陸', '國語', '2019', 9.0),
(5, 'WiFi過敏的少女', '喜劇片', 'None', 'None', '2018', 3.0),
(6, '回到過去擁抱你', '愛情片', '大陸', '國語', '2019', 3.0),
(7, '血胎換骨', '電影', '香港', 'None', '2009', 4.0),
(8, '偉大的轉折1946', '電影', 'None', 'None', '1946', 8.0),
(9, '七月與安生', '電影', '大陸', 'None', '2016', 7.6),
(10, '降龍祖師', '喜劇片', '大陸', '國語', '2019', 2.0),
(11, '我親愛的表哥', '劇情片', 'None', 'None', '2019', 9.0),
(12, '地獄男爵:血皇后崛起', '動作片', '美國', '英語', '2019', 1.0),
(13, '疾速備戰', '動作片', '美國', '英語', '2019', 4.0), (14, '金手套', '恐怖片', '其它', '德語', '2019', 10.0),
(15, '永不退縮3', '電影', '美國', '英語', '2016', 5.9), (16, '永不退縮2', '電影', '美國', 'None', '2011', 4.0),
(17, '珍珍的髮屋', '劇情片', '大陸', '國語', '未知', 7.0), (18, '拯救愛情', '劇情片', '大陸', '國語', '2001', 4.0),
(19, '闖江湖', '劇情片', '大陸', '國語', '1984', 3.0), (20, '瘋狂粉絲王', '喜劇片', '香港', '粵語', '2007', 6.0),
(21, '夜幕獵人', '動作片', '美國', '英語', '2018', 8.0), (22, '我的心裏住着一隻貓', '愛情片', '大陸', '國語', '2019', 10.0),
(23, '重金屬', '電影', '香港', 'None', '1994', 8.0), (24, '竹升妹之以牙還牙', '電影', '香港', 'None', '1996', 4.0),
(25, '預言者', '電影', '法國', 'None', '2009', 3.0), (26, '訴訟2018', '劇情片', '美國', '英語', '2018', 10.0),
(27, '草葉集', '劇情片', '韓國', '韓語', '2018', 1.0), (28, '姐姐2019', '動作片', '韓國', '韓語', '2019', 10.0),
(29, '長安詭事之末世神兵', '動作片', '大陸', '國語', '2019', 10.0),
(30, '學園爆笑王', '喜劇片', '日本', '日語', '2019', 9.0),
(31, '使徒行者2:諜影行動', '劇情片', '大陸', '粵語', '2019', 4.0), (32, '馬大姐', '恐怖片', '美國', '英語', '2019', 1.0),
(33, '隔牆有鬼', '恐怖片', '美國', '英語', '2018', 4.0), (34, '偉大的轉折1946', '電影', 'None', 'None', '1946', 8.0),
(35, '永不退縮2', '電影', '美國', 'None', '2011', 4.0), (36, '重金屬', '電影', '香港', 'None', '1994', 8.0),
(37, '竹升妹之以牙還牙', '電影', '香港', 'None', '1996', 4.0), (38, '預言者', '電影', '法國', 'None', '2009', 3.0),
(39, '奔騰年代', '劇情片', '美國', '英語', '2003', 4.0), (40, '逃出珊瑚海', '恐怖片', '香港', '國語', '1986', 4.0),
(41, '甜蜜的謊言', '喜劇片', '韓國', '韓語', '2008', 10.0), (42, '他鄉的童年', '電影', '大陸', '國語', '2019', 9.0)))
data1 = data.T
print(data1)
xlabel = data1[2]
print(data)
# 統計劇情片
new1 = (data=="劇情片")
juqingpian = np.sum(new1)
# 統計恐怖片
new2 = (data=="恐怖片")
kongbupian = np.sum(new2)
# 統計愛情片
new3 = (data=="愛情片")
aiqingpian = np.sum(new3)
# 統計動作片
new4 = (data=="動作片")
dongzuopian = np.sum(new4)
# 統計喜劇片
new5 = (data=="喜劇片")
xijupian = np.sum(new5)
# 統計'電影'
new6 = (data=="電影")
dianying = np.sum(new6)
xlist = ["plot","horror","comedy","film","romantic","actioner"]
ylist = [juqingpian,kongbupian,xijupian,dianying,aiqingpian,dongzuopian]
x = [i for i in np.array(xlist)]
y = [i for i in np.array(ylist)]
# y = data1[0]
print(x)
print(y)
bars = subplot2.bar(x,y,width=0.3)
# subplot2 = figure.add_subplot(2, 2, 2).subplot()
subplot2.set_ylabel('total-number')
subplot2.set_xlabel('type',bbox=dict(facecolor='y', edgecolor='red', alpha=0.65 ))
subplot2.set_xticks(x)
subplot2.set_xticklabels(x)
# subplot2.title('Type - Film')
subplot2.grid(linestyle='--')
# 設置顏色
i = 0
for bar in bars:
bar.set_color('#'+str(205001+i))
i += 119500
for x,y in zip(x,y):
subplot2.text(x,y,'%s'% np.float(y),ha="center",va="bottom")
# 3.餅圖
# 用來正常顯示中文
plt.rcParams['font.sans-serif'] = ['SimHei']
# 用來正常顯示符號
plt.rcParams['axes.unicode_minus'] = False
subplot3 = figure.add_subplot(2, 2, 3)
x = [i for i in np.array(xlist)]
y = [i for i in np.array(ylist)]
data = [temp for temp in (np.array(y) / np.sum(np.array(y)))]
print(data,'666')
labels = ['%.2f %%' % (x*100) for x in data]
subplot3.pie(data,colors=['#50F4FF','#14FF4C',
'#FF120C','#DC460C',
'#2365E1','#FFFF5A'],labels=labels)
plt.title('類型佔比',bbox=dict(facecolor='y', edgecolor='red', alpha=0.65 ))
# 4.點圖
subplot4 = figure.add_subplot(2, 2, 4)
x = np.random.randint(1, 100, 100)
y = np.random.randint(1, 100, 100)
subplot4.scatter(x, y, s=30, c=x, alpha=0.8, marker='*')
plt.show()