这章文章主要讲解到了:折线图、条形图、直方图、散点图、饼图、热力图
安装matplotlib库,可以使用命令 pip install
然后应用matplotlib库
'''引用库''' from matplotlib import pyplot as plt
#首先引用库 from matplotlib import pyplot as plt #然后要有x,y的数据 dev_x = [1,2,3,4,5] dev_y = [20,40,60,80,100] #生成折线图plt.后缀就是决定是什么图 plt.plot(dev_x,dev_y) #显示所有图片 plt.show()
显示结果
引用上面代码,做出演示
#首先引用库 from matplotlib import pyplot as plt #然后要有x,y的数据 dev_x = [1,2,3,4,5] dev_y = [25,48,69,87,100] #生成折线图plt.后缀就是决定是什么图 plt.plot(dev_x,dev_y) '''添加标题,横纵表,纵坐标''' #横坐标.xlabel plt.xlabel("天数") #纵坐标.ylabel plt.ylabel("完成度") #标题.title plt.title("下载完成效率图") #显示所有图片 plt.show()
依旧是引用上面的代码
#首先引用库 from matplotlib import pyplot as plt #然后要有x,y的数据 dev_x = [1,2,3,4,5] dev_y = [20,45,62,85,100] #生成折线图plt.后缀就是决定是什么图 plt.plot(dev_x,dev_y) '''新增数据''' dev_x_1 = [1,2,3,4,5] dev_y_1 = [25,45,55,89,100] plt.plot(dev_x_1,dev_y_1) #添加标题,横纵表,纵坐标 #横坐标.xlabel plt.xlabel("天数") #纵坐标.ylabel plt.ylabel("完成度") #标题.title plt.title("下载完成效率图") #显示所有图片 plt.show()
代码简洁版
from matplotlib import pyplot as plt dev_x = [1,2,3,4,5] dev_y = [20,40,60,80,100] plt.plot(dev_x,dev_y) dev_x_1 = [1,2,3,4,5] dev_y_1 = [25,45,55,89,100] plt.plot(dev_x_1,dev_y_1) plt.xlabel("天数") plt.ylabel("完成度") plt.title("下载完成效率图") plt.show()
显示结果:
通过legend函数为折线打上标签
from matplotlib import pyplot as plt import pylab as mp1 mp1.rcParams['font.sans-serif'] = ['SimHei'] dev_x = [1,2,3,4,5] dev_y = [20,40,60,80,100] plt.plot(dev_x,dev_y,label="迅雷")#label为折现命名,然调用legend函数时不需要命名 dev_x_1 = [1,2,3,4,5] dev_y_1 = [25,45,55,89,100] plt.plot(dev_x_1,dev_y_1,label="百度云盘") plt.xlabel("天数") plt.ylabel("完成度") plt.legend()#调用函数,参数在label已经输入 plt.title("下载完成效率图") plt.show()
color | 指定图形的颜色 |
---|---|
marker | 指定每个数据点的标记 |
linestyle | 指定图形的形状 |
pyplot自带样式引用 | plt.style.use(‘自带样式名称’) |
from matplotlib import pyplot as plt import pylab as mp1 mp1.rcParams['font.sans-serif'] = ['SimHei'] dev_x = [1,2,3,4,5] dev_y = [20,40,60,80,100] plt.plot(dev_x,dev_y,label="迅雷",color="blue",marker=".",linestyle="-") dev_x_1 = [1,2,3,4,5] dev_y_1 = [25,45,55,89,100] plt.plot(dev_x_1,dev_y_1,label="百度云盘",color="red",marker=".",linestyle="--") plt.xlabel("天数") plt.ylabel("完成度") plt.legend() plt.title('下载效率图') plt.show()
from matplotlib import pyplot as plt import pylab as mp1 mp1.rcParams['font.sans-serif'] = ['SimHei'] dev_x = [1,2,3,4,5] dev_y = [20,40,60,80,100] plt.plot(dev_x,dev_y,label="迅雷",color="blue",marker=".",linestyle="-") dev_x_1 = [1,2,3,4,5] dev_y_1 = [25,45,55,89,100] plt.plot(dev_x_1,dev_y_1,label="百度云盘",color="red",marker=".",linestyle="--") plt.xlabel("天数") plt.ylabel("完成度") plt.legend() plt.title('下载效率图') plt.grid(True) plt.show()
from matplotlib import pyplot as plt import pylab as mp1 mp1.rcParams['font.sans-serif'] = ['SimHei'] x_data = ['小刘','小张','小王','小李','小刚','小三'] y_data = [3,4,2,3,5,5] plt.bar(x_data,y_data)#这个位置改动了 plt.title('工作表') plt.xlabel('员工姓名') plt.ylabel('工作天数') plt.show()
很多操作与折线图操作类似,这里就不做重复操作了
#条形图bar from matplotlib import pyplot as plt import pylab as mp1 mp1.rcParams['font.sans-serif'] = ['SimHei'] import numpy as np #导入包 # np.random.randint()区间之内的整数值 y_data = [np.random.randint(100) + i*20 for i in range(6)] x_data = range(6) x_c_data = ['小刘','小温','小罗','小张','小培','小林'] plt.title('员工入职图') plt.bar(x_data,y_data) #xitcks('参数','参数','属性') plt.xticks(x_data,x_c_data) plt.xlabel('姓名') plt.ylabel('工作天数') plt.show()
将bar函数改为barh
import numpy as np from matplotlib import pyplot as plt import pylab as mp1 mp1.rcParams['font.sans-serif'] = ['SimHei'] x_data = ['JavaScript', 'HTML/CSS', 'SQL', 'Python', 'Java', 'Bash/Shell/PowerShell', 'C#', 'PHP', 'C++','TypeScript'] y_data = [59219, 55466, 47544, 36443, 35917, 27097, 23030, 20524, 18523, 18017] plt.barh(x_data, y_data) plt.title('编程语言流行度') plt.xlabel('编程语言') plt.ylabel('流行度') plt.tight_layout()#tight_layout会自动调整子图参数,使之填充整个图像区域 plt.show()
reverse()函数
import numpy as np from matplotlib import pyplot as plt import pylab as mp1 mp1.rcParams['font.sans-serif'] = ['SimHei'] x_data = ['JavaScript', 'HTML/CSS', 'SQL', 'Python', 'Java', 'Bash/Shell/PowerShell', 'C#', 'PHP', 'C++','TypeScript'] y_data = [59219, 55466, 47544, 36443, 35917, 27097, 23030, 20524, 18523, 18017] #水平翻转操作 x_data.reverse() y_data.reverse() plt.barh(x_data, y_data) plt.title('编程语言流行度') plt.xlabel('编程语言') plt.ylabel('流行度') plt.tight_layout() plt.show()
直方图介绍:
直方图,又称质量分布图,用于表示数据的分布情况,是一种常见的统计图表。一般用横轴表示数据区间,纵轴表示分布情况,柱子越高,则落在该区间的数量越大。直方图和条形图的外观相似。区别主要有以下几点:
直方图的适用环境:关系与数量之间的分布
这里区间划分是自己明确指定划分区间范围
from matplotlib import pyplot as plt import pylab as mp1 mp1.rcParams['font.sans-serif'] = ['SimHei'] x_data = [1,2,3,4,5,6,7] y_data = [1,2,3,4,5,6,7] plt.hist(x_data,y_data) plt.show()
输出结果以及图像解释:
其中如果我们不想要某个区间的数据,可以直接去掉
这里是bins根据区间划分的数量 自动划分
from matplotlib import pyplot as plt import pylab as mp1 mp1.rcParams['font.sans-serif'] = ['SimHei'] x_data = [1,2,3,4,5,6,7] plt.hist(x_data,bins=2)#将x轴应用区间划分,这里划分了两个区间 plt.show()
区间划分的格式是[1,2),[2,3),[3,4)依次这样的格式
from matplotlib import pyplot as plt import pylab as mp1 mp1.rcParams['font.sans-serif'] = ['SimHei'] x_data = [1,2,3,4,5,6,7] plt.hist(x_data,bins=2,edgecolor='red')#这个位置参数里面是颜色 plt.show()
平均值显示其他的图形也可以使用!!!
from matplotlib import pyplot as plt import pylab as mp1 mp1.rcParams['font.sans-serif'] = ['SimHei'] x_data = [1,2,3,4,5,6,7] plt.hist(x_data,bins=2,edgecolor='black')#这个位置参数里面是颜色 avg = 4#平均值变量 plt.axvline(avg,color='red',label='平均值')#平均值垂直线段引用 plt.legend() plt.show()
from matplotlib import pyplot as plt import pylab as mp1 mp1.rcParams['font.sans-serif'] = ['SimHei'] plt.style.use('fivethirtyeight')#自带格式 x_data = [1,2,3,4,5] y_data = [10,25,15,30,5] plt.scatter(x_data,y_data)#这里变化scatter plt.title('商品单价数量') plt.xlabel('数量') plt.ylabel('金额') plt.tight_layout() plt.show()
属性 | 含义 |
---|---|
s | 点的大小 |
c | 点的颜色 |
marker | 点的形状 |
edgecolors | 点的边缘颜色 |
linewidth | 边缘线的粗细度 |
alpha | 透明度 |
'''在这串代码中设置''' plt.scatter(x_data,y_data,s=100, c='red', marker='*')
这是我设置的 自己可以根据需求设置
将参数c改为第二数据
from matplotlib import pyplot as plt import pylab as mp1 mp1.rcParams['font.sans-serif'] = ['SimHei'] plt.style.use('fivethirtyeight') x_data = [1,2,3,4,5] y_data = [10,25,15,30,5] y_data_1 = [20,23,11,50,45] plt.scatter(x_data,y_data,s=100, c=y_data_1, marker='*')#将参数c设置成为y_data_1 plt.title('商品单价数量') plt.xlabel('数量') plt.ylabel('金额') plt.tight_layout() plt.show()
为了让图形传递信息更加的准确,添加一个色卡标明颜色深浅对应关系
from matplotlib import pyplot as plt import pylab as mp1 mp1.rcParams['font.sans-serif'] = ['SimHei'] plt.style.use('fivethirtyeight') x_data = [1,2,3,4,5] y_data = [10,25,15,30,5] y_data_1 = [20,23,11,50,45] plt.scatter(x_data,y_data,s=100, c=y_data_1, marker='*') #色卡的添加 card = plt.colorbar() card.set_label('利润率') plt.title('商品单价数量') plt.xlabel('数量') plt.ylabel('金额') plt.tight_layout() plt.show()
from matplotlib import pyplot as plt import numpy as np#引用numpy这个包 import pylab as mp1 mp1.rcParams['font.sans-serif'] = ['SimHei'] plt.style.use('fivethirtyeight') #将数据np.array() x_data = np.array([1,2,3,4,5]) y_data = np.array([10,25,15,30,5]) y_data_1 = np.array([20,23,11,50,45]) #将s参数改为y_data_1*10 plt.scatter(x_data,y_data,s=y_data_1*10, c=y_data_1, marker='*') card = plt.colorbar() card.set_label('利润率') plt.title('商品单价数量') plt.xlabel('数量') plt.ylabel('金额') plt.tight_layout() plt.show()
from matplotlib import pyplot as plt import numpy as np import pylab as mp1 mp1.rcParams['font.sans-serif'] = ['SimHei'] plt.style.use('fivethirtyeight') #数据1 x_data = np.array([1,2,3,4,5]) y_data = np.array([10,25,15,30,5]) y_data_1 = np.array([20,23,11,50,45]) plt.scatter(x_data,y_data,s=y_data_1*10, c=y_data_1, marker='*',label='数据1') #数据2 data_2 = np.array([1.5,2.1,3,4,6.5]) data_2_x = np.array([19,29,39,49,59]) data_2_y = np.array([15,25,35,45,55]) plt.scatter(data_2,data_2_x,s=data_2_y*10,c=data_2_y,marker='v',label='数据2') #色卡 card = plt.colorbar() card.set_label('利润率') plt.legend() plt.title('商品单价数量') plt.xlabel('数量') plt.ylabel('金额') plt.tight_layout() plt.show()
from matplotlib import pyplot as plt import pylab as mp1 mp1.rcParams['font.sans-serif'] = ['SimHei'] #数据《王者荣耀》英雄出场率 hero_data = ['孙悟空','孙尚香','诸葛亮','盾山','澜'] hero_odds = [20,21,29,15,15] plt.pie(hero_odds)#使用pie plt.tight_layout() plt.show()
'''在pie中的加上labels=“名字列表”''' plt.pie(hero_odds,labels=hero_data)
得到:
from matplotlib import pyplot as plt import pylab as mp1 mp1.rcParams['font.sans-serif'] = ['SimHei'] #数据《王者荣耀》英雄出场率 hero_data = ['孙悟空','孙尚香','诸葛亮','盾山','澜'] hero_odds = [20,21,29,15,15] ''' 加上autopct="%1.1f%%" ''' plt.pie(hero_odds,labels=hero_data,autopct='%1.1f%%') plt.tight_layout() plt.show()
参数 | 含义 |
---|---|
counterclock | 饼图旋转方向 顺时针(False)逆时针(True) |
startangle | 扇形放角度 |
from matplotlib import pyplot as plt import pylab as mp1 mp1.rcParams['font.sans-serif'] = ['SimHei'] #数据《王者荣耀》英雄出场率 hero_data = ['孙悟空','孙尚香','诸葛亮','盾山','澜'] hero_odds = [20,21,29,15,15] #这个位置添加参数!!! plt.pie(hero_odds,labels=hero_data,autopct='%1.1f%%',counterclock=False, startangle=90) plt.title('英雄登场率') plt.tight_layout() plt.show()
plt.pie(hero_odds,labels=hero_data,autopct='%1.1f%%',counterclock=False, startangle=90,explode=[0.1,0,0,0,0])
1、表格形热力图,也称色块图(2个分类字段和1个数值字段,分类字段来确定x,y轴)
2、非表格型热力图(3个数值字段,2个数值字段分别确定x,y轴,1个数值数值字段确定着色)
热力图适合查看总体情况、发现异常值、显示多个变量之间的差异,以及查看它们之间的相关性
将一个二维数组传入imshow就可以绘制一个热力图
from matplotlib import pyplot as plt import numpy as np import pylab as mp1 mp1.rcParams['font.sans-serif'] = ['SimHei'] harvest = np.array([[0.8, 2.4, 2.5, 3.9, 0.0, 4.0, 0.0], [2.4, 0.0, 4.0, 1.0, 2.7, 0.0, 0.0], [1.1, 2.4, 0.8, 4.3, 1.9, 4.4, 0.0], [0.6, 0.0, 0.3, 0.0, 3.1, 0.0, 0.0], [0.7, 1.7, 0.6, 2.6, 2.2, 6.2, 0.0], [1.3, 1.2, 0.0, 0.0, 0.0, 3.2, 5.1], [0.1, 2.0, 0.0, 1.4, 0.0, 1.9, 6.3]]) plt.imshow(harvest)#热力图imshow plt.tight_layout() plt.show()
from matplotlib import pyplot as plt import numpy as np import pylab as mp1 mp1.rcParams['font.sans-serif'] = ['SimHei'] harvest_x = ['成都','甘肃','泸州','阿坝','西昌','乐山','眉山'] harvest_y = ['平原','山脉','高原','丘陵','盆地','山脊','林原'] harvest = np.array([[0.8, 2.4, 2.5, 3.9, 0.0, 4.0, 0.0], [2.4, 0.0, 4.0, 1.0, 2.7, 0.0, 0.0], [1.1, 2.4, 0.8, 4.3, 1.9, 4.4, 0.0], [0.6, 0.0, 0.3, 0.0, 3.1, 0.0, 0.0], [0.7, 1.7, 0.6, 2.6, 2.2, 6.2, 0.0], [1.3, 1.2, 0.0, 0.0, 0.0, 3.2, 5.1], [0.1, 2.0, 0.0, 1.4, 0.0, 1.9, 6.3]]) '''这个地方加入x,y''' plt.xticks(np.arange(len(harvest_x)), labels=harvest_x, rotation=45, rotation_mode="anchor", ha="right") plt.yticks(np.arange(len(harvest_y)), labels=harvest_y) plt.imshow(harvest) plt.title('地热散布分析') plt.tight_layout() plt.show()
参数 | 详情 |
---|---|
rotation | 文字旋转度数 |
rotation_mode | 旋转模式 |
ha | 旋转方向 |
from matplotlib import pyplot as plt import numpy as np import pylab as mp1 mp1.rcParams['font.sans-serif'] = ['SimHei'] harvest_x = ['成都','甘肃','泸州','阿坝','西昌','乐山','眉山'] harvest_y = ['平原','山脉','高原','丘陵','盆地','山脊','林原'] harvest = np.array([[0.8, 2.4, 2.5, 3.9, 0.0, 4.0, 0.0], [2.4, 0.0, 4.0, 1.0, 2.7, 0.0, 0.0], [1.1, 2.4, 0.8, 4.3, 1.9, 4.4, 0.0], [0.6, 0.0, 0.3, 0.0, 3.1, 0.0, 0.0], [0.7, 1.7, 0.6, 2.6, 2.2, 6.2, 0.0], [1.3, 1.2, 0.0, 0.0, 0.0, 3.2, 5.1], [0.1, 2.0, 0.0, 1.4, 0.0, 1.9, 6.3]]) plt.xticks(np.arange(len(harvest_x)), labels=harvest_x, rotation=45, rotation_mode="anchor", ha="right") plt.yticks(np.arange(len(harvest_y)), labels=harvest_y) plt.imshow(harvest) plt.colorbar()#在代码中加入colorbar就行了 plt.title('地热散布分析') plt.tight_layout() plt.show()
from matplotlib import pyplot as plt import numpy as np import pylab as mp1 mp1.rcParams['font.sans-serif'] = ['SimHei'] harvest_x = ['成都','甘肃','泸州','阿坝','西昌','乐山','眉山'] harvest_y = ['平原','山脉','高原','丘陵','盆地','山脊','林原'] harvest = np.array([[0.8, 2.4, 2.5, 3.9, 0.0, 4.0, 0.0], [2.4, 0.0, 4.0, 1.0, 2.7, 0.0, 0.0], [1.1, 2.4, 0.8, 4.3, 1.9, 4.4, 0.0], [0.6, 0.0, 0.3, 0.0, 3.1, 0.0, 0.0], [0.7, 1.7, 0.6, 2.6, 2.2, 6.2, 0.0], [1.3, 1.2, 0.0, 0.0, 0.0, 3.2, 5.1], [0.1, 2.0, 0.0, 1.4, 0.0, 1.9, 6.3]]) plt.xticks(np.arange(len(harvest_x)), labels=harvest_x, rotation=45, rotation_mode="anchor", ha="right") plt.yticks(np.arange(len(harvest_y)), labels=harvest_y) '''加上块中的数值''' for i in range(len(harvest_x)): for j in range(len(harvest_y)): text = plt.text(j, i, harvest[i, j], ha="center", va="center", color="w") plt.imshow(harvest) plt.colorbar() plt.title('地热散布分析') plt.tight_layout() plt.show()
AttributeError: module ‘requests’ has no attribute ‘get’”,python中”requests模块“get”属性缺失“的错误该如何解决? | |
---|---|
解决方法:
检查是不是自己写的代码文件命名为requests.py如果是的话,修改文件名,即可。 | |
---|---|
解决方法:
在代码中加入
import pylab as mp1#文字问题使用 mp1.rcParams['font.sans-serif'] = ['SimHei']#文字问题使用
@想努力学习变强的尾巴
欢迎各位在评论区讨论留言,遇到问题一起解决!