龙空技术网

Python绘制堆积图,想学的赶紧来看看

MedVis 457

前言:

今天朋友们对“python面积图堆叠图”都比较注意,看官们都需要知道一些“python面积图堆叠图”的相关内容。那么小编同时在网上汇集了一些对于“python面积图堆叠图””的相关知识,希望姐妹们能喜欢,我们一起来学习一下吧!

背景介绍

今天我们来学习一下Python绘制堆积图,也就是一个X,两个Y数据。来吧,试着画一画。

软件介绍

[软件名称]:Anaconda | Spyder

绘图介绍堆积柱状图

# 导入库import matplotlib.pyplot as plt# 设置数据day = [1,2,3,4,5,6,7]my_body_weight = [75,75.1,74.9,74,73.9,73,70]other_body_weight = [56.3,56,55.9,54,53.9,53,50]# 堆积柱状图plt.bar(day,my_body_weight,        color = "r",        label = "my body weight")plt.bar(day,other_body_weight,        color = "b",        label = "other body weight")plt.xlabel("Day")plt.ylabel("Weight (kg)")plt.ylim(0,100)plt.legend()

如果想调换两个柱子的位置,可以直接使用bottom参数将一列放在底层即可

# 导入库import matplotlib.pyplot as plt# 设置数据day = [1,2,3,4,5,6,7]my_body_weight = [75,75.1,74.9,74,73.9,73,70]other_body_weight = [56.3,56,55.9,54,53.9,53,50]# 堆积柱状图plt.bar(day,my_body_weight,        color = "r",        label = "my body weight")plt.bar(day,other_body_weight,        color = "b",        # 将上述数据置于底层        bottom = my_body_weight,        label = "other body weight")plt.xlabel("Day")plt.ylabel("Weight (kg)")plt.ylim(0,160)# 设置间距plt.yticks([0,40,80,120,160])plt.legend()
堆积条形图
# 导入库import matplotlib.pyplot as plt# 设置数据day = [1,2,3,4,5,6,7]my_body_weight = [75,75.1,74.9,74,73.9,73,70]other_body_weight = [56.3,56,55.9,54,53.9,53,50]# 堆积柱状图plt.barh(day,my_body_weight,        color = "r",        label = "my body weight")plt.barh(day,other_body_weight,        color = "b",        # 将上述数据置于底层        left= my_body_weight,        label = "other body weight")plt.ylabel("Day")plt.xlim(0,160)plt.xlabel("Weight (kg)")# 使用loc确定legend的位置# 其余的参数还有:# (‘best’, ‘upper right’, ‘upper left’,#  ‘lower left’, ‘lower right’, #  ‘right’, ‘center left’, ‘center , #  right’, ‘lower center’, #  ‘upper center’, ‘center’) plt.legend(loc = "lower left")

怎么样,来学习下呗

标签: #python面积图堆叠图