"""
折线图:
绘制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()
plt.pcolormesh(x1, x2, grid_hat, cmap=cm_light, alpha=0.8)
pcolormesh方法(用来绘制分类背景图)
pcolormesh(X,Y,Z cmap=None, vmin=None, vmax=None)
X,Y:
指的是二维网格面每一个点的横纵坐标,此处的X,Y必须是网格采样(mgrid,meshgrid)
Z:
(X,Y)坐标处的颜色值
cmap:着色方案