分享

一文学会Python绘制常见二维图

 禁忌石 2022-04-30 发布于浙江

背景介绍

今天我们来学习一下,使用Python如何绘制常见的二维图形。

教程代码可直接复制,粘贴到python的IDE中进行运行即可出图。

软件介绍

[软件名称]:Anaconda | Spyder

[软件安装]:可以参考下方这个推文

Anaconda的下载和安装

基础绘图

折线图

# 导入库import matplotlib.pyplot as plt# 设置数据date = [0,3,7,10,15,20,21,25,26,30]body_temp = [36.8, 37.2, 37.6,37.8,38.5, 38.2, 38.0, 37.5, 37.6, 36.8]# 绘制折线图plt.plot(date,body_temp, # 设置颜色 color = 'r', # 设置线型 linestyle = '--', # 设置标记 marker = 'o')# 设置x标签plt.xlabel('Date (day)')# 设置y标签plt.ylabel('Body temperature (℃)')# 设置标题plt.title('The change of body temperature')# 设置x轴的标签plt.xticks([0,10,20,30])# 设置y轴的标签plt.yticks([36,37,38,39])plt.show()
文章图片1

多条折线

# 导入库import matplotlib.pyplot as plt# 设置数据date = [0,3,7,10,15,20,21,25,26,30]body_temp = [36.8, 37.2, 37.6,37.8,38.5, 38.2, 38.0, 37.5, 37.6, 36.8]body_temp2 = [36.3, 36.8, 36.9, 37.1,37.5, 38.2, 37.9, 37.8, 37.7, 36.4]# 绘制第一条线plt.plot(date,body_temp,         color = 'r',         marker = '+',         label = 'Line1')# 绘制第二条线plt.plot(date,body_temp2,         color = 'g',         marker = '^',         label = 'Line2')plt.xlabel('Date (day)')plt.ylabel('Body temperature (℃)')plt.title('The change of body temperature')plt.legend()
文章图片2

散点图

# 导入库import matplotlib.pyplot as plt# 设置数据date = [0,3,7,10,15,20,21,25,26,30]body_temp = [36.8, 37.2, 37.6,37.8,38.5, 38.2, 38.0, 37.5, 37.6, 36.8]plt.scatter(date,body_temp, color = 'r')plt.show()
文章图片3

两个散点图

# 导入库import matplotlib.pyplot as plt# 导入库import matplotlib.pyplot as plt# 设置数据date = [0,3,7,10,15,20,21,25,26,30]body_temp = [36.8, 37.2, 37.6,37.8,38.5, 38.2, 38.0, 37.5, 37.6, 36.8]body_temp2 = [36.3, 36.8, 36.9, 37.1,37.5, 38.2, 37.9, 37.8, 37.7, 36.4]# 绘制第一条线plt.scatter(date,body_temp,         color = 'r',         marker = '+',         label = 'Line1')# 绘制第二条线plt.scatter(date,body_temp2,         color = 'g',         marker = '^',         label = 'Line2')plt.xlabel('Date (day)')plt.ylabel('Body temperature (℃)')plt.title('The change of body temperature')plt.legend()
文章图片4

柱状图

# 导入库import matplotlib.pyplot as plt# 设置数据date = [0,1,2,3,4,5,6,7,8,9]body_temp = [36.8, 37.2, 37.6,37.8,38.5, 38.2, 38.0, 37.5, 37.6, 36.8]plt.bar(date,body_temp, color = 'r')plt.ylim(36,39)plt.xlabel('Date (day)')plt.ylabel('Body temperature (℃)')plt.title('The change of body temperature')plt.legend()
文章图片5

色彩斑斓的柱状图

通过给与三个或多个颜色连续性填充

# 导入库import matplotlib.pyplot as plt# 设置数据date = [0,1,2,3,4,5,6,7,8,9]body_temp = [36.8, 37.2, 37.6,37.8,38.5, 38.2, 38.0, 37.5, 37.6, 36.8]plt.bar(date,body_temp,        # 填充三种颜色         color = ['r','g','b'],         # 填充形状         hatch = ['/'])plt.ylim(36,39)plt.xlabel('Date (day)')plt.ylabel('Body temperature (℃)')plt.title('The change of body temperature')plt.legend()
文章图片6

其他的填充形状

'/', '\', '|', '-', '+', 'x','o', 'O', '.', '*'
文章图片7
文章图片8
文章图片9
文章图片10

饼图

# 导入库import matplotlib.pyplot as plt# 设置数据value = [100,200,300,400,500]label = ['a','b','c','d','e']# 绘图plt.pie(x = value,labels = label,        # 设置颜色        colors = ['y','g','c','gray','b'],        # 添加阴影        shadow = True,        # 添加百分比        autopct = '%1.1f%%',        # 设置标签距离        pctdistance = 0.8,        # 设置角度        startangle = 90)
文章图片11

空心饼图

# 导入库import matplotlib.pyplot as plt# 设置数据value = [100,200,300,400,500]label = ['a','b','c','d','e']# 绘图plt.pie(x = value,labels = label, colors = ['y','g','c','gray','b'], autopct = '%1.1f%%', pctdistance = 0.8, startangle = 90, # 设置空心距离 wedgeprops = dict(width = 0.5))
文章图片12

箱线图/小提琴图

# 导入库import matplotlib.pyplot as plt# 设置数据data1 = [8,3,19,12,14,56,40,36,27,10,11]data2 = [1,3,15,12,15,30,13,16,27,10,11]data3 = [8,3,10,12,14,56,67,36,27,10,100]#箱子图plt.boxplot([data1,data2,data3],            labels = ['data1','data2','data3'])#小提琴图plt.violinplot([data1,data2,data3])
文章图片13
文章图片14

常见的图可能就这些了,没有介绍的,之后再介绍一下!

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约