Python:柱状-折线图

写论文,需要画数据分析图:

  • 用柱状图描述算法执行时间
  • 用折线图描述性能改进

示例代码:

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.pyplot import MultipleLocator

Security = ["128", "192", "256"]

Alg1 = {
    "Others": [55, 99, 170],
    "Ours": [33, 44, 55],
    "Ratio": [23.4, 45.6, 78.9]
    }

################################################################

# 双轴:https://zhuanlan.zhihu.com/p/343719623
# 图形:https://blog.csdn.net/weixin_45891612/article/details/129082746
# 颜色:https://blog.csdn.net/weixin_43697287/article/details/88876680
# 花纹:https://matplotlib.org/2.0.2/examples/pylab_examples/hatch_demo.html
# 图例:https://blog.csdn.net/Wannna/article/details/102751689

# 柱状图坐标
size = len(Security)
x = np.arange(size)
width_bar = 0.2
Alg_x = x
Ours_x = x + width_bar
Security_x = x + width_bar/2

# 刻度间隔
y1_major_locator=MultipleLocator(25)
y2_major_locator=MultipleLocator(10)

bar_color_1 = '#ff8080'
bar_color_2 = '#8080ff'
plot_color = '#111111'

################################################################

fig = plt.figure()
ax1 = fig.add_subplot(111)

ax1.bar(Alg_x, Alg1["Others"], width=width_bar, color=bar_color_1, hatch="--", label="Others")
ax1.bar(Ours_x, Alg1["Ours"], width=width_bar, color=bar_color_2, hatch="//", label="Ours")
ax1.set_ylim(bottom=0, top=250)
ax1.yaxis.set_major_locator(y1_major_locator)

ax1.set_ylabel('Running Time (clocks)')
ax1.set_xlabel('XXX (bits)')
ax1.legend(loc=2)

ax2 = ax1.twinx() # this is the important function

ax2.plot(Security_x, Alg1["Ratio"], color=plot_color, marker="v", label="Relative Improvement")
ax2.set_ylim(bottom=0, top=100)
ax2.yaxis.set_major_locator(y2_major_locator)
ax2.set_ylabel('Percentage (%)')
ax2.legend(loc=1)

for i in range(size):  ##向曲线上的点添加数据标签,利用text函数
       plt.text(Security_x[i], Alg1["Ratio"][i]+1.3, Alg1["Ratio"][i], ha='center', va='bottom', fontsize=10, color=plot_color) 

plt.xticks(Security_x, labels=Security)
plt.title("Alg1")
plt.show()

绘制结果应当存储为 .eps 格式,pdflatex 将它编译为 .pdf 矢量图片。

在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值