"""
折線圖:
繪製10點到12點的氣溫的折線圖
x軸爲帶中文的時間
y軸爲氣溫
"""
from matplotlib import pyplot as plt
import matplotlib
from matplotlib import font_manager
from pylab import mpl,text
from matplotlib.font_manager import FontProperties
import random
"""
因爲matplotlib默認不顯示中文字體,需更改其字體,現有兩種方法
"""
"""
#方法1
font = {'family': 'Times New Roman',
'weight': 'normal',
'size': '13'}
matplotlib.rc('font',**font) #按ctrl+B鍵到1172行到1176行代碼瞭解
#又或者
#matplotlib.rc("font",family = 'monospace',weight= 'bold,',size= '13')
"""
my_font = font_manager.FontProperties(fname ="C:/Windows/Fonts/simsun.ttc")
x = range(120)
y_1 = [random.randint(20,35) for i in range(120)]
y_2 = [random.randint(22,32) for i in range(120)]
plt.figure(figsize=(25,9),dpi=100)
plt.plot(x,y_1,label = "北京",color = 'r',linestyle = '-',linewidth = 1,alpha = 0.8)
plt.plot(x,y_2,label = "上海",color = 'g',linestyle = '-',linewidth = 1,alpha = 0.8)
_x = list(x)[::3]
_xticks_labels = ["10點{}分".format(i) for i in range(60)]
print(_xticks_labels)
_xticks_labels += ["11點{}分".format(i) for i in range(60)]
print(_xticks_labels)
plt.xticks(_x,_xticks_labels[::3],rotation = 45,fontproperties = my_font)
""" 方法2 """
plt.xlabel("時間",fontproperties = my_font)
plt.ylabel("溫度 單位(‘C)",fontproperties = my_font)
plt.title("溫度折線圖",fontproperties = my_font)
plt.grid(alpha = 0.8)
plt.legend(prop = my_font,loc = 'upper left')
plt.show()
"""
散點圖:
繪製3月分與10月分的氣溫的散點圖
x軸爲帶中文的時間
y軸爲氣溫
"""
from matplotlib import pyplot as plt
from matplotlib import font_manager
my_font = font_manager.FontProperties(fname ="C:/Windows/Fonts/simsun.ttc")
y_3 = [11,17,16,11,12,11,12,6,6,7,8,9,12,15,14,17,18,21,16,17,20,14,15,15,15,19,21,22,22,22,23]
y_10 =[26,26,28,19,21,17,16,19,18,20,20,19,22,23,17,20,21,20,22,15,11,15,5,13,17,10,11,13,12,13,6]
x_3 = range(1,32)
x_10 = range(51,82)
plt.figure(figsize=(20,8),dpi = 80)
plt.scatter(x_3,y_3,label = "3月分")
plt.scatter(x_10,y_10,label = "10月分")
_x = list(x_3) + list(x_10)
_xticks_label = ["3月{}號".format(i) for i in range(1,32)]
_xticks_label+= ["10月{}號".format(i-50) for i in range(51,82)]
plt.xticks(_x[::3],_xticks_label[::3],fontproperties = my_font,rotation = 45)
plt.xlabel("時間",fontproperties = my_font)
plt.ylabel("溫度",fontproperties =my_font)
plt.title("標題",fontproperties = my_font)
plt.legend(prop = my_font,loc = 'upper left')
plt.show()
"""
豎狀條形圖:
繪製電影與其票房的條形圖
x軸爲電影名稱
y軸爲票房
"""
from matplotlib import pyplot as plt
from matplotlib import font_manager
my_font = font_manager.FontProperties(fname= "C:/Windows/Fonts/simsun.ttc")
x = ["戰狼2","速度與激情8","功夫瑜伽","西遊伏妖篇","變形金剛5:\n最後的騎士","摔跤吧!爸爸","加勒比海盜5:\n死無對證","金剛:骷髏島","極限特工:\n終極迴歸","生化危機6:\n終章","乘風破浪","神偷奶爸3","智取威虎山","大鬧天竺","金剛狼3:\n殊死一戰","蜘蛛俠:\n英雄歸來","悟空傳","銀河護衛隊2","情聖","新木乃伊"]
y = [56.01,26.94,17.53,16.49,15.45,12.96,11.8,11.61,11.28,11.12,10.49,10.3,8.75,7.55,7.32,6.99,6.88,6.86,6.58,6.23]
plt.figure(figsize=(20,8),dpi = 80)
plt.xticks(range(len(x)),x,fontproperties = my_font,rotation = 45)
plt.bar(range(len(x)),y)
plt.xlabel("電影名稱",fontproperties = my_font)
plt.ylabel("電影票房",fontproperties = my_font)
plt.title("標籤",fontproperties = my_font)
plt.show()
"""
橫狀條形圖:
繪製電影與其票房的條形圖
x軸爲票房
y軸爲電影名稱
"""
from matplotlib import pyplot as plt
from matplotlib import font_manager
my_font = font_manager.FontProperties(fname= "C:/Windows/Fonts/simsun.ttc")
x = [56.01,26.94,17.53,16.49,15.45,12.96,11.8,11.61,11.28,11.12,10.49,10.3,8.75,7.55,7.32,6.99,6.88,6.86,6.58,6.23]
y = ["戰狼2","速度與激情8","功夫瑜伽","西遊伏妖篇","變形金剛5:最後的騎士","摔跤吧!爸爸","加勒比海盜5:死無對證","金剛:骷髏島","極限特工:終極迴歸","生化危機6:終章","乘風破浪","神偷奶爸3","智取威虎山","大鬧天竺","金剛狼3:殊死一戰","蜘蛛俠:英雄歸來","悟空傳","銀河護衛隊2","情聖","新木乃伊"]
plt.figure(figsize=(20,8),dpi = 80)
plt.yticks(range(len(y)),y,fontproperties = my_font)
plt.xticks(range(1,int(max(x)+2)))
plt.barh(range(len(y)),x,height=0.5,color = 'orange')
plt.xlabel("電影票房",fontproperties = my_font)
plt.ylabel("電影名稱",fontproperties = my_font)
plt.title("標籤",fontproperties = my_font)
plt.grid()
plt.show()
"""
多重柱狀圖:
假設你知道了列表a中電影分別在
2017-09-14(b_14),
2017-09-15(b_15),
2017-09-16(b_16)
三天的票房,爲了展示列表中電影本身的票房以及同其他電影的數據對比情況,
應該如何更加直觀的呈現該數據?
a = ["猩球崛起3:終極之戰","敦刻爾克","蜘蛛俠:英雄歸來","戰狼2"]
b_16 = [15746,312,4497,319]
b_15 = [12357,156,2045,168]
b_14 = [2358,399,2358,362]
"""
from matplotlib import pyplot as plt
from matplotlib import font_manager
my_font = font_manager.FontProperties(fname="C:/Windows/Fonts/simsun.ttc")
x = ["猩球崛起3:終極之戰","敦刻爾克","蜘蛛俠:英雄歸來","戰狼2"]
y_16 = [15746,312,4497,319]
y_15 = [12357,156,2045,168]
y_14 = [2358,399,2358,362]
plt.figure(figsize=(20,8),dpi=80)
plt.xticks(range(len(x)),x,fontproperties = my_font)
x_14 = list(range(len(x)))
x_15 = [i+0.2 for i in x_14]
x_16 = [i+0.4 for i in x_14]
plt.bar(x_14,y_14,width = 0.2,color = 'orange',label = "2017-09-14")
plt.bar(x_15,y_15,width = 0.2,color = 'r',label = "2017-09-15")
plt.bar(x_16,y_16,width = 0.2,color = 'g',label = " 2017-09-16")
plt.xlabel("電影名稱",fontproperties = my_font)
plt.ylabel("票房",fontproperties = my_font)
plt.legend(loc = 'upper right')
plt.grid()
plt.show()
"""
直方圖:
假設你獲取了250部電影的時長(列表a中),希望統計出這些電影時長的分佈狀態
(比如時長爲100分鐘到120分鐘電影的數量,出現的頻率)等信息,你應該如何呈現
這些數據?
a=[131, 98, 125, 131, 124, 139, 131, 117, 128, 108, 135, 138, 131, 102, 107, 114, 119, 128, 121, 142, 127, 130, 124, 101, 110, 116, 117, 110, 128, 128, 115, 99, 136, 126, 134, 95, 138, 117, 111,78, 132, 124, 113, 150, 110, 117, 86, 95, 144, 105, 126, 130,126, 130, 126, 116, 123, 106, 112, 138, 123, 86, 101, 99, 136,123, 117, 119, 105, 137, 123, 128, 125, 104, 109, 134, 125, 127,105, 120, 107, 129, 116, 108, 132, 103, 136, 118, 102, 120, 114,105, 115, 132, 145, 119, 121, 112, 139, 125, 138, 109, 132, 134,156, 106, 117, 127, 144, 139, 139, 119, 140, 83, 110, 102,123,107, 143, 115, 136, 118, 139, 123, 112, 118, 125, 109, 119, 133,112, 114, 122, 109, 106, 123, 116, 131, 127, 115, 118, 112, 135,115, 146, 137, 116, 103, 144, 83, 123, 111, 110, 111, 100, 154,136, 100, 118, 119, 133, 134, 106, 129, 126, 110, 111, 109, 141,120, 117, 106, 149, 122, 122, 110, 118, 127, 121, 114, 125, 126,114, 140, 103, 130, 141, 117, 106, 114, 121, 114, 133, 137, 92,121, 112, 146, 97, 137, 105, 98, 117, 112, 81, 97, 139, 113,134, 106, 144, 110, 137, 137, 111, 104, 117, 100, 111, 101, 110,105, 129, 137, 112, 120, 113, 133, 112, 83, 94, 146, 133, 101,131, 116, 111, 84, 137, 115, 122, 106, 144, 109, 123, 116, 111,111, 133, 150]
"""
from matplotlib import pyplot as plt
from matplotlib import font_manager
my_font = font_manager.FontProperties(fname= "C:/Windows/Fonts/simsun.ttc")
a=[131, 98, 125, 131, 124, 139, 131, 117, 128, 108, 135, 138, 131, 102, 107, 114, 119, 128, 121, 142, 127, 130, 124, 101, 110, 116, 117, 110, 128, 128, 115, 99, 136, 126, 134, 95, 138, 117, 111,78, 132, 124, 113, 150, 110, 117, 86, 95, 144, 105, 126, 130,126, 130, 126, 116, 123, 106, 112, 138, 123, 86, 101, 99, 136,123, 117, 119, 105, 137, 123, 128, 125, 104, 109, 134, 125, 127,105, 120, 107, 129, 116, 108, 132, 103, 136, 118, 102, 120, 114,105, 115, 132, 145, 119, 121, 112, 139, 125, 138, 109, 132, 134,156, 106, 117, 127, 144, 139, 139, 119, 140, 83, 110, 102,123,107, 143, 115, 136, 118, 139, 123, 112, 118, 125, 109, 119, 133,112, 114, 122, 109, 106, 123, 116, 131, 127, 115, 118, 112, 135,115, 146, 137, 116, 103, 144, 83, 123, 111, 110, 111, 100, 154,136, 100, 118, 119, 133, 134, 106, 129, 126, 110, 111, 109, 141,120, 117, 106, 149, 122, 122, 110, 118, 127, 121, 114, 125, 126,114, 140, 103, 130, 141, 117, 106, 114, 121, 114, 133, 137, 92,121, 112, 146, 97, 137, 105, 98, 117, 112, 81, 97, 139, 113,134, 106, 144, 110, 137, 137, 111, 104, 117, 100, 111, 101, 110,105, 129, 137, 112, 120, 113, 133, 112, 83, 94, 146, 133, 101,131, 116, 111, 84, 137, 115, 122, 106, 144, 109, 123, 116, 111,111, 133, 150]
"""
組數:將數據分組,當數據在100個以內時,通常將數據分爲12個組
組距:指每個小組的兩個端點的劇
組數 = 極差/組距 = (max(x)-min(x))/bin_width 組距由自己設定,最好能被極差整除
"""
d = 6
num_bins = (max(a)-min(a))//d
plt.figure(figsize=(20,8),dpi=80)
plt.xticks(range(min(a),max(a)+d,d))
plt.grid()
plt.hist(a,num_bins,normed= True,color='orange')
plt.xlabel("電影時長",fontproperties = my_font)
plt.ylabel("電影頻率",fontproperties = my_font)
plt.title("標籤",fontproperties = my_font)
plt.show()