背景介绍今天我们来学习一下,使用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()
多条折线# 导入库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()
散点图# 导入库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()
两个散点图# 导入库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()
柱状图# 导入库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()
色彩斑斓的柱状图通过给与三个或多个颜色连续性填充 # 导入库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()
其他的填充形状 '/', '\', '|', '-', '+', 'x','o', 'O', '.', '*'
饼图# 导入库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)
空心饼图# 导入库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))
箱线图/小提琴图# 导入库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])
常见的图可能就这些了,没有介绍的,之后再介绍一下!
|