python拆堆和堆叠的操作_[python | matplotlib]多列堆栈图,Pythonmatplotlib,堆叠

本文介绍了如何使用Matplotlib库在Python中创建条形图,通过官网示例展示了M1IP、M2IP和M2LP的运行时间对比,并详细解释了如何设置标签、颜色、位置和自定义文本。最终将图表保存为图片并展示出来。
摘要由CSDN通过智能技术生成

官网大法好:

点击直达官网

参考:

我的:

import matplotlib

import matplotlib.pyplot as plt

import numpy as np

labels = ['C3', 'C4', 'C5', 'C6']

im_ip = [20, 34, 30, 35]

cg_ip = [25, 32, 34, 20]

cg_lp = [2, 3, 4, 2]

x = np.arange(len(labels)) # the label locations

width = 0.35 # the width of the bars

fig, ax = plt.subplots()

#x操作水平位置

rects1 = ax.bar(x - width, im_ip, width,color='cornflowerblue', label='M1 IP')

rects2 = ax.bar(x, cg_ip, width, color='orange',label='M2 IP')

#bottom操作堆叠位置

rects3 = ax.bar(x, cg_lp, width,bottom=cg_ip,color='yellow', label='M2 LP')

# Add some text for labels, title and custom x-axis tick labels, etc.

ax.set_ylabel('Runtime (s)')

#ax.set_title('Scores by group and gender')

ax.set_xticks(x)

ax.set_xticklabels(labels)

ax.legend(loc='upper left')

def autolabel(rects):

"""Attach a text label above each bar in *rects*, displaying its height."""

for rect in rects:

height = rect.get_height()

ax.annotate('{}'.format(height),

xy=(rect.get_x() + rect.get_width() / 2, height),

xytext=(0, 3), # 3 points vertical offset

textcoords="offset points",

ha='center', va='bottom')

#autolabel(rects1)

#autolabel(rects2)

fig.tight_layout()

plt.savefig('demo.png')#保存

plt.show()

效果

b5628fbe8bb1864f8dd24694a1466c2b.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值