matplotlib學習筆記(代碼)

"""
折線圖:
繪製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')
"""
#方法2
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)]
#隨機產生120個在20到35之間的數字
#print(y)

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軸的刻度,使其能顯示中文

_x = list(x)[::3]
#[::3]取步長爲3


_xticks_labels = ["10點{}分".format(i) for i in range(60)]
#['10點0分', '10點1分', ... , '10點58分', '10點59分']
print(_xticks_labels)


_xticks_labels += ["11點{}分".format(i) for i in range(60)]
#['10點0分', '10點1分', ... , '10點58分', '10點59分', '11點0分', '11點1分', ... ,'11點58分', '11點59分']
print(_xticks_labels)

#plt.xticks(_x,_xticks_labels[::3],rotation = 45)
plt.xticks(_x,_xticks_labels[::3],rotation = 45,fontproperties = my_font)
"""  方法2 """


#_x爲中文在刻度上的位置,而_xticks_labels爲對應刻度位置上的內容
# rotation參數是內容的旋轉的角度(因爲中文的內容太大,會由重疊)




#設置x軸的名稱
#plt.xlabel("時間")
plt.xlabel("時間",fontproperties = my_font)

#設置y軸的名稱
#plt.ylabel("溫度 單位(‘C)")
plt.ylabel("溫度 單位(‘C)",fontproperties = my_font)

#設置折線圖的名稱
plt.title("溫度折線圖",fontproperties = my_font)

#繪製網格
plt.grid(alpha = 0.8)
#網格刻度對應於x,y軸的刻度,參數alpha是調節網格的透明度的,1是完全不透明到0

#添加圖例
plt.legend(prop = my_font,loc = 'upper left')
#要顯示中文,添加參數prop = my_font,參數loc可以設置圖裏的位置,如左上(upper left),右上(upper right)



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軸刻度
_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)

#設置x軸
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)

#設置x軸
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')
#橫狀條形圖的柱的寬度由參數height來調節,而不是width
# 若在給參數width賦值會報錯,可以按cltr+B查看,第二個參數就是width,即是這裏的x
#再給width賦值會造成二次賦值的錯誤

#添加描繪信息
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)

#調整x軸
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]    #加的是width的值
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)

#設置x軸刻度
plt.xticks(range(min(a),max(a)+d,d))    #由最小到最大,每隔d設置一刻度

#設置網格
plt.grid()


#畫出直方圖
plt.hist(a,num_bins,normed= True,color='orange')
#a爲數據,num_bins爲組數,normed爲是否畫成頻率直方圖,是爲True

#添加描述信息
plt.xlabel("電影時長",fontproperties = my_font)
plt.ylabel("電影頻率",fontproperties = my_font)
plt.title("標籤",fontproperties = my_font)

plt.show()

頻率直方圖

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章