from matplotlib.pyplot as plt
# # figsize:传入元组,英寸单位的宽和高
# dpi:图像的分辨率
fig = plt.figure(figsize=(10,10),dpi=60)
print(fig)
# 展示图片
fig.show()
Figure(640x480)
# 添加一个子图
plt.subplot(221)
plt.show()
# 创建多个子图
ax1 = plt.subplot(2,2,1) # 2*2,最多4个图形,index=1,选择了第一个
ax2 = plt.subplot(2,2,2)
ax3 = plt.subplot(2,2,3)
ax4 = plt.subplot(2,2,4)
plt.show()
ax5 = plt.subplot(2,2,5)
plt.show()
ValueError: num must be 1 <= num <= 4, not 5
# 2*2,nrows子图的行数,ncols子图的列数,sharex,sharey共享刻度
plt.subplots(2,2,sharex=True,sharey=True)
plt.show()
fig, axs = plt.subplots(2,2,sharex=True,sharey=True)
# 选中第一张图
axs[0,0].scatter(range(5))
# 选中第二张图
axs[0,1].scatter(range(5),list(range(5))[::-1])
plt.show()
plt.subplots_adjust(wspace=0,hspace=0)
plt.show()
matplotlib能够绘制折线图,散点图,条形图,直方图,饼图等等
from matplotlib import pyplot as plt
x = [1,4,7,8,9]
y = [5,6,3,5,6]
plt.plot(x,y)
plt.show()
plt.plot(x,y,color='r',linestyle='--',marker='4')
plt.show()
# 假设一天中每隔两个小时的气温分别是[15,13,14.5,17,20,25,26,26,27,22,18,15],绘制图形
from matplotlib import pyplot as plt
# 每隔两个小时:2,4,6,8....24
x = list(range(2,26,2))
y = [15,13,14.5,17,20,25,26,26,27,22,18,15]
plt.plot(x,y)
plt.show()
方法 | 描述 |
---|---|
plt.figure(figsize=None,dpi=None) | 生成新的图片,figsieze:图片大小,dpi:透明度 |
plt.savefig(fname) | 保存图片 |
plt.xticks(ticks=None) | 设置x轴刻度的值 |
plt.yticks(ticks=None) | 设置y轴刻度的值 |
plt.xlable(xlable) | 设置x轴标签 |
plt.ylable(ylable) | 设置y轴标签 |
plt.title() | 设置图片标题 |
plt.grid() | 根据x轴和y轴的数值展示轴网格 |
from matplotlib import pyplot as plt
# 每隔两个小时:2,4,6,8....24
x = list(range(2,26,2))
y = [15,13,14.5,17,20,25,26,26,27,22,18,15]
# 设置画布大小
plt.figure(figsize=(14,8))
# 设置x轴刻度:以0h,0.5h,1h,1.5h....23.5h显示
# ticks:刻度
# lables:刻度标签
# rotation:旋转角度
x_t = [i/2 for i in range(0,48)]
x_l = ['{}h'.format(i/2) for i in range(0,48)]
plt.xticks(x_t,x_l,rotation=45)
# 设置y轴刻度:最大值,最小值
y_t = range(min(y),max(y))
plt.yticks(y_t)
# 添加x轴标签
plt.xlable('time')
# 添加y轴标签
plt.ylable('temperature')
# 添加标题
plt.title('temperature change')
# 添加网格
plt.grid()
# 绘制图形
plt.plot(x,y)
# 保存图片:要在show之前保存,否则就是空白图
plt.savefig('mat.png')
# 展示图片
plt.show()
# 如果列表a表示10点到12点的每一分钟的气温,如何绘制折线图观察每分钟气温的变化情况
from matplotlib import pyplot as plt
import random
x = range(0,120)
# 生成120个随机整数
y = [random.randint(30,38) for i in range(120)]
# 刻度:0,10,20,30....110
x_t = range(0,120,10)
# 显示x刻度标签:十点0分,十点10分....十一点0分,十一点10分....十一点50分
x_l = ['十点{}分'.format(i) for i in range(0,60,10)]
x_l += ['十一点{}分'.format(i) for i in range(0,60,10)]
plt.xticks(x_l,x_t,rotation=45)
plt.plot(x,y)
plt.show()
import matplotlib
font = {
'family':'Arial Unicode MS',
'weight':'bold',
'size':12
}
matplotlib.rc("font",**font)
import matplotlib.pyplot as plt
# plt.rcParams['font.family'] = 'Arial Unicode MS'
plt.rcParams['font.sans-serif'] = 'Arial Unicode MS'
# 解决坐标轴负数的负号显示问题
plt.rcParams['axes.unicode_minus'] = False
from matplotlib import pyplot as plt
from matplotlib.font_manager import FontProperties
font = FontProperties(fname=r"c:\windows\fonts\simsun.ttc", size=14)
x = [1,2,3,4]
y = [3,2,1,3]
plt.plot(x,y)
plt.xlabel("x轴标签",fontproperties=font)
plt.show()
from matplotlib import font_manager
a = sorted([f.name for f in font_manager.fontManager.ttflist])
for i in a:
print(i)
'''
假设大家在30岁的时候,根据自己的实际情况,统计出来了你和同桌从11岁到30岁每年交的男女朋友的数量如列表a和b,请绘制出该数据的折线图,以便分析自己和同桌每年交男女朋友的数量走势
a = [1,0,1,1,2,4,3,2,3,4,4,5,6,5,4,3,3,1,1,1]
b = [1,0,3,1,2,2,2,3,1,1,1,1,1,2,1,1,2,3,2,2]
要求:
• y轴表示个数
• x轴表示岁数,比如11岁,12岁
'''
from matplotlib import pyplot as plt
# 显示中文
plt.rcParams['font.sans-serif'] = 'Arial Unicode MS'
# 构建x,y
my_y = [1,0,1,1,2,4,3,2,3,4,4,5,6,5,4,3,3,1,1,1]
my_cls_y = [1,0,3,1,2,2,2,3,1,1,1,1,1,2,1,1,2,3,2,2]
x = range(11,31)
# x刻度、标签
x_t = [i for i in range(11,31)]
x_l = ['{}岁'.format(i) for i in range(11,31)]
plt.xticks(x_t,x_l,ratation=45)
# 绘图
plt.plot(x,my_y,label='自己')
plt.plot(x,my_cls_y,label='同桌')
# 添加图例
# 1、plt.plot()添加label属性
# 2、plt.legend()局部指定字体,要使用prop
plt.legend()
# 展示图片
plt.show()
from matplotlib import pyplot as plt
import random
x = range(10)
y = [random.randint(10,30) for i in range(10)]
plt.plot(x,y,marker='o')
# 数据标签
plt.annotate('28',xy=(0,28),xytext=(-0.3,0),arrowsprops={'width':2})
plt.show()
# arrowsprops参数使用
from matplotlib import pyplot as plt
import random
x = range(10)
y = [random.randint(10,30) for i in range(10)]
plt.plot(x,y,marker='o')
# 数据标签
plt.annotate('This is a zhushi',xy=(0,18),xytext=(2,28),arrowsprops={'facecolor':'r','headlength':30,'headwidth':30,'width':30})
plt.show()
# 实现:将每个点都使用上数据标签
from matplotlib import pyplot as plt
import random
x = range(10)
y = [random.randint(10,30) for i in range(10)]
plt.plot(x,y,marker='o')
# 数据标签
for xy in zip(x,y):
plt.annotate(xy, xy=xy, xytext=xy, arrowprops={'width':2,'facecolor':'y'})
plt.show()
'''
假设通过爬虫你获取到了长沙2019年4,10月份每天白天的最高气温(分别位于列表a,b),那么此时如何寻找出气温和随时间变化的某种规律
a = [11,17,16,11,12,11,12,13,10,14,8,13,12,15,14,17,18,21,16,17,30,14,15,15,15,19,21,22,22,22,23]
b = [26,26,28,19,21,17,16,19,18,20,20,19,22,23,17,20,21,20,22,15,11,15,5,13,15,10,11,13,12,13,6]
'''
from matplotlib import pyplot as plt
y_4 = [11,17,16,11,12,11,12,13,10,14,8,13,12,15,14,17,18,21,16,17,30,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,15,10,11,13,12,13,6]
# 中文显示
plt.rcParams['font.sans-serif'] = 'Arial Unicode MS'
# 4,10月份的分布分开
x_4 = range(1,32)
x_10 = range(51,82)
# 绘制散点图
plt.scatter(x_4,y_4)
plt.scatter(x_10,y_10)
# 刻度:四月1号....四月31号 十月1号....十月31号
x_t = list(x_4) + list(x_10)
x_l = ['四月{}号'.format(i) for i in range(x_4)]
x_l += ['十月{}号'.format(i) for i in range(x_10)]
plt.xticks(x_t[::4], x_l[::4], rotation=45)
# 展示图片
plt.show()
'''
假设你获取到了2019年内地电影票房前20的电影(列表a)和电影票房数据(列表b),那么如何更加直观的展示该数据
a = ["流浪地球","复仇者联盟4:终局之战","哪吒之魔童降世","疯狂的外星人","飞驰人生","蜘蛛侠:英雄远征","扫毒2天地对决","烈火英雄","大黄蜂","惊奇队长","比悲伤更悲伤的故事","哥斯拉2:怪兽之王","阿丽塔:战斗天使","银河补习班","狮子王","反贪风暴4","熊出没","大侦探皮卡丘","新喜剧之王","使徒行者2:谍影行动","千与千寻"]
b = [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,5.22] 单位:亿
'''
from matplotlib import pyplot as plt
# 构建x,height
a = ["流浪地球","复仇者联盟4:终局之战","哪吒之魔童降世","疯狂的外星人","飞驰人生","蜘蛛侠:英雄远征","扫毒2天地对决","烈火英雄","大黄蜂","惊奇队长","比悲伤更悲伤的故事","哥斯拉2:怪兽之王","阿丽塔:战斗天使","银河补习班","狮子王","反贪风暴4","熊出没","大侦探皮卡丘","新喜剧之王","使徒行者2:谍影行动","千与千寻"]
b = [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,5.22]
# 中文显示
plt.rcParams['font.sans-serif'] = 'Arial Unicode MS'
# 放大画布
plt.figure(figsize=(14,8))
# 设置刻度
plt.xticks(rotation=90)
# 绘制柱形图
# width:调整柱子的宽度,默认0.8
# bottom:y的起始值
# align:默认center居中,设置为edge边缘,取决于width的正负值
plt.bar(a,b,width=-0.3,bottom=30,align='edge')
plt.show()
from matplotlib import pyplot as plt
# 构建x,height
fruits = ['水果', '梨子', '车厘子']
Q1_sales = [1000, 800, 3000]
Q2_sales = [1200, 700, 2800]
# 中文显示
plt.rcParams['font.sans-serif'] = 'Arial Unicode MS'
# 绘制条形图
plt.bar(fruits, Q1_sales, bottom=Q2_sales, label='Q1')
plt.bar(fruits, Q2_sales, label='Q2')
# 添加图例
plt.legend()
# 展示图片
plt.show()
在这里插入代码片
from matplotlib import pyplot as plt
# 构建y,width
a = ["流浪地球","复仇者联盟4:终局之战","哪吒之魔童降世","疯狂的外星人","飞驰人生","蜘蛛侠:英雄远征","扫毒2天地对决","烈火英雄","大黄蜂","惊奇队长","比悲伤更悲伤的故事","哥斯拉2:怪兽之王","阿丽塔:战斗天使","银河补习班","狮子王","反贪风暴4","熊出没","大侦探皮卡丘","新喜剧之王","使徒行者2:谍影行动","千与千寻"]
b = [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,5.22]
# 中文显示
plt.rcParams['font.sans-serif'] = 'Arial Unicode MS'
# 绘制水平条形图
plt.barh(a,b)
# 显示图片
plt.show()
'''
练习
某地区连续50年中四月份平均气温数据如下:
temp_li= [6.9,4.1,6.6,5.2,6.4,7.9,8.6,3.0,4.4,6.7,7.1,4.7,9.1,6.8,8.6,5.2,5.8,7.9,5.6,8.8,8.1,5.7,8.4,4.1,6.4,6.2,5.2,6.8,5.6,5.6,6.8,8.2,6.4,4.8,6.9,7.1,9.7,6.4,7.3,6.8,7.1,4.8,5.8,6.5,5.9,7.3,5.5,7.4,6.2,7.7]
根据以上数据,推断该地区四月份平均气温的分布类型
'''
from matplotlib import pyplot as plt
# 显示中文
plt.rcParams['font.sans-serif'] = 'Arial Unicode MS'
temp_li= [6.9,4.1,6.6,5.2,6.4,7.9,8.6,3.0,4.4,6.7,7.1,4.7,9.1,6.8,8.6,5.2,5.8,7.9,5.6,8.8,8.1,5.7,8.4,4.1,6.4,6.2,5.2,6.8,5.6,5.6,6.8,8.2,6.4,4.8,6.9,7.1,9.7,6.4,7.3,6.8,7.1,4.8,5.8,6.5,5.9,7.3,5.5,7.4,6.2,7.7]
# 组距
b = 1
# bins组数:组数=极差/组距,round:四舍五入
bi = round(max(temp_li)-min(temp_li))//b
# 绘制直方图
plt.hist(temp_li, bins=bi, density=True) # bins一定是整数类型,默认为10
# 显示标签
plt.xlabel('区间')
plt.ylabel('频数/频率')
plt.title('直方图')
# 展示图片
plt.show()
'''
将以下frac数据绘制扇形图,并且设置其扇形标签为label
frac = [1/50,6/50,11/50,15/50,9/50,6/50,2/50]
label = ['[3,4]','(4,5]','(5,6]','(6,7]','(7,8]','(8,9]','(9,10]']
'''
from matplotlib import pyplot as plt
frac = [1/50,6/50,11/50,15/50,9/50,6/50,2/50]
label = ['[3,4]','(4,5]','(5,6]','(6,7]','(7,8]','(8,9]','(9,10]']
# 绘制扇形图
# labels:扇形标签
# autopct:百分比数据标签
# explode:设置某几个分块是否要分离饼图,传入为元组
# shadow:是否显示阴影
# textprops:设置字体,传入为字典
explode_li = (0,0.3,0,0,0,0,0.2)
plt.pie(frac, labels=label, autopct='%.2f%%', explode=explode_li, shadow=True, textprops={'size':20})
# 展示图片
plt.show()
from matplotlib import pyplot as plt
import random
'''
随机生成100个 1-100的数据
'''
data = [random.randint(1,100) for i in range(100)]
# 列表添加异常值
data.extend([-100,500])
# 绘制箱形图
# sym:异常点的符号表示,默认原点
# widths:每个盒子的宽度
# meanline/showmeans:都为True的时候,会展示平均线
plt.boxplot(data, sym='^', widths=0.2, meanline=True, showmeans=True)
# 展示图片
plt.show()
from matplotlib import pyplot as plt
import numpy as np
quaters = ['Q1','Q2','Q3','Q4']
sales = [40,91,44,90,40]
# 弧度 = 2*PI=360°
# 求theta,2*PI
theta = np.linspace(0, np.pi*2, 5) # 创建等差数列
# 绘制雷达图
plt.polar(theta, sales)
# 设置刻度标签
# plt.xticks(theta,quaters)
# 填充
plt.fill(theta, sales)
# 展示图片
plt.show()