柱状图和折线图绘制,放在一个图上。

import matplotlib.pyplot as plt
import numpy as np

# 数据
models = ['v8n', 'C2f-F', 'AIFI', 'LSCD', 'F-AF', 'F-AF-LsCD']
params = [3006623, 2301423, 2934175, 2362648, 2228975, 1585000]
flops = [8.1, 6.3, 8.0, 6.5, 6.2, 4.6]

# 创建图形和轴
fig, ax1 = plt.subplots() #figsize=(10,8)

# 绘制柱状图
bars=ax1.bar(models, params, color='blue', label='Parameters',alpha=1)
ax1.set_xlabel('Models')
ax1.set_ylabel('Parameters')
ax1.tick_params(axis='y')
ax1.set_ylim(1.0 * 10**6, 3.5 * 10**6)

#增加数值
for bar in bars:
    yval=bar.get_height()
    ax1.text(bar.get_x() , yval, round(yval, 2), va='bottom')

# 创建第二个轴用于折线图
ax2 = ax1.twinx()
ax2.plot(models, flops, color='red', marker='o', linestyle='-', label='FLOPS')
ax2.set_ylabel('FLOPS')
ax2.tick_params(axis='y')
ax2.set_ylim(4.5, 8.5)
ax2.set_yticks(np.linspace(4, 10,4))

#增加数值
for i ,txt in enumerate(flops):
    ax2.annotate(txt,(models[i],flops[i]),color='white')


# 显示图例
ax1.legend(loc='upper left',fontsize=7,bbox_to_anchor=(0.8, 0.93))
ax2.legend(loc='upper right',fontsize=7,bbox_to_anchor=(1, 1))

# 显示图形
plt.savefig('./models.jpg',dpi=2000)
plt.show()


#D:\Desktop\plt.py

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值