欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

matplotlib学习笔记(代码)

程序员文章站 2022-07-12 09:36:00
...
"""
折线图:
绘制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()

matplotlib学习笔记(代码)

"""
散点图:
绘制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()

matplotlib学习笔记(代码)

"""
竖状条形图:
绘制电影与其票房的条形图
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()

matplotlib学习笔记(代码)

"""
横状条形图:
绘制电影与其票房的条形图
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()

matplotlib学习笔记(代码)

"""
多重柱状图:
假设你知道了列表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()

matplotlib学习笔记(代码)

"""
直方图:
假设你获取了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()

matplotlib学习笔记(代码)