论文绘图之SciencePlots

想在论文中插入精美的图片,偶然发现一个哈佛博士写的开源库,格式非常Nice,记录一下

# install command
# pip install -i https://pypi.tuna.tsinghua.edu.cn/simple SciencePlots==1.0.9
# https://github.com/garrettj403/SciencePlots/tree/master

import numpy as np
import matplotlib.pyplot as plt

print(plt.style.available)


def model(x, p):
    return x ** (2 * p + 2) / (2 + x ** (2 * p))


x = np.linspace(0.75, 1.25, 201)

# with plt.style.context(['science', 'no-latex']):
#     fig, ax = plt.subplots()
#     for p in [10, 15, 20, 30, 50, 100]:
#         ax.plot(x, model(x, p), label=p)
#     ax.legend(title='Order')
#     ax.set(xlabel='Voltage (mV)')
#     ax.set(ylabel='Current (μA)')
#     ax.autoscale(tight=True)
#     fig.savefig('fig1.png', dpi=300)


with plt.style.context(['science', 'ieee', 'no-latex']):
    fig, ax = plt.subplots()
    for p in [10, 20, 50]:
        ax.plot(x, model(x, p), label=p)
    ax.legend(title='Order')
    ax.set(xlabel='Voltage (mV)')
    ax.set(ylabel='Current (μA)')
    ax.autoscale(tight=True)
    fig.savefig('fig2.png', dpi=300)

# 折线图
with plt.style.context(['science', 'ieee', 'no-latex']):
    fig, ax = plt.subplots()
    # for p in [10, 20, 50]:
    #     ax.plot(x, model(x, p), label=p)
    app = [78, 80, 79, 81, 91, 95, 96]
    x = np.linspace(100, 800, 7)
    ax.plot(x, app, label="RFMUL")

    app = [60, 70, 75, 80, 90, 85, 88]
    x = np.linspace(100, 800, 7)
    ax.plot(x, app, label="RFMUL+")

    app = [75, 65, 70, 78, 88, 80, 78]
    x = np.linspace(100, 800, 7)
    ax.plot(x, app, label="RFMP-Growth")

    ax.legend(title='Algorithm')
    ax.set(ylabel='Memory (MB)')
    ax.set(xlabel='β (M)')
    ax.autoscale(tight=True)
    fig.savefig('fig3.png', dpi=300)

# 柱状图
with plt.style.context(['science', 'high-contrast', 'no-latex']):

    labels = ['L1', 'L2', 'L3', 'L4', 'L5']
    data_a = [22, 34, 30, 35, 23]
    data_b = [25, 32, 34, 20, 25]
    data_c = [12, 20, 24, 17, 16]
    x = np.arange(len(labels))
    width = .25
    # plots
    fig, ax = plt.subplots(figsize=(5, 3), dpi=200)
    bar_a = ax.bar(x - width / 2, data_a, width, label='categoryA')
    bar_b = ax.bar(x + width / 2, data_b, width, label='categoryB')
    bar_c = ax.bar(x + width * 3 / 2, data_c, width, label='categoryC')
    ax.set_xticks(x + .1)
    ax.set_xticklabels(labels, size=10)
    ax.set(ylabel='Memory (MB)')
    ax.set(xlabel='Parameter')
    # ax.set(xlabel='β (M)')
    ax.legend()

    text_font = {'size': '10', 'weight': 'bold', 'color': 'black'}
    ax.text(.03, .9, "", transform=ax.transAxes, fontdict=text_font, zorder=4)
    ax.text(.87, -.08, '', transform=ax.transAxes,
            ha='center', va='center', fontsize=5, color='black', fontweight='bold', family='Roboto Mono')
    plt.savefig('fig4.png', dpi=300, bbox_inches='tight')

https://blog.csdn.net/weixin_58092393/article/details/125335751
https://www.runoob.com/matplotlib/matplotlib-subplots.html

未完待续…

# pip install -i https://pypi.tuna.tsinghua.edu.cn/simple SciencePlots==1.0.9
# https://github.com/garrettj403/SciencePlots/tree/master
# https://matplotlib.org/

import numpy as np
import matplotlib.pyplot as plt

print(plt.style.available)


def model(x, p):
    return x ** (2 * p + 2) / (2 + x ** (2 * p))


x = np.linspace(0.75, 1.25, 201)

# with plt.style.context(['science', 'no-latex']):
#     fig, ax = plt.subplots()
#     for p in [10, 15, 20, 30, 50, 100]:
#         ax.plot(x, model(x, p), label=p)
#     ax.legend(title='Order')
#     ax.set(xlabel='Voltage (mV)')
#     ax.set(ylabel='Current (μA)')
#     ax.autoscale(tight=True)
#     fig.savefig('fig1.png', dpi=300)


with plt.style.context(['science', 'ieee', 'no-latex']):
    fig, ax = plt.subplots()
    for p in [10, 20, 50]:
        ax.plot(x, model(x, p), label=p)
    ax.legend(title='Order')
    ax.set(xlabel='Voltage (mV)')
    ax.set(ylabel='Current (μA)')
    ax.autoscale(tight=True)
    fig.savefig('fig2.png', dpi=300)

with plt.style.context(['science', 'ieee', 'no-latex']):
    fig, axs = plt.subplots(2, 2, figsize=(4, 4))
    # for p in [10, 20, 50]:
    #     ax.plot(x, model(x, p), label=p)

    # app = [78, 80, 79, 81, 91, 95, 96]
    # x = np.linspace(100, 800, 7)
    # ax.plot(x, app, label="RFMUL")
    #
    # app = [60, 70, 75, 80, 90, 85, 88]
    # x = np.linspace(100, 800, 7)
    # ax.plot(x, app, label="RFMUL+")

    app = [65, 68, 70, 78]
    x = [25, 50, 75, 100]
    axs[0, 0].plot(x, app, label="ML-RFM", color='red')

    # ax.legend(title='Algorithm')
    axs[0, 0].set(ylabel='${Runtime(s)}$')
    axs[0, 0].set(xlabel='%${transactions, μ=}$$10^{6}$')
    axs[0, 0].autoscale(tight=True)
    axs[0, 0].set_title("(a) Fruithut")
    # axs[0, 0].set_aspect(1./axs[0, 0].get_data_ratio(), adjustable='box')   # 坐标轴正方形

    app = [200, 244, 320, 420]
    # x = np.linspace(100, 800, 7)
    x = [25, 50, 75, 100]
    axs[0, 1].plot(x, app, label="ML-RFM", color='red')
    axs[0, 1].set(ylabel='${Runtime(s)}$')
    axs[0, 1].set(xlabel='%${transactions, μ=}$$10^{4}$')
    axs[0, 1].autoscale(tight=True)
    axs[0, 1].set_title("(b) Chainstore")

    app = [78, 80, 85, 88]
    # x = np.linspace(100, 800, 7)
    x = [25, 50, 75, 100]
    axs[1, 0].plot(x, app, label="ML-RFM", color='red')
    axs[1, 0].set(ylabel='${Runtime(s)}$')
    axs[1, 0].set(xlabel='%${transactions, μ=}$$10^{4}$')
    axs[1, 0].autoscale(tight=True)
    axs[1, 0].set_title("(c) Connect")

    app = [1234, 1547, 1874, 1998]
    # x = np.linspace(100, 800, 7)
    x = [25, 50, 75, 100]
    axs[1, 1].plot(x, app, label="ML-RFM", color='red')
    axs[1, 1].set(ylabel='${Runtime(s)}$')
    axs[1, 1].set(xlabel='%${transactions, μ=}$$10^{4}$')
    axs[1, 1].autoscale(tight=True)
    axs[1, 1].set_title("(d) Liquor")

    fig.tight_layout()  # 调整整体空白
    # plt.figure(figsize=(6, 6.5))
    plt.subplots_adjust(wspace=0.5, hspace=0.5)  # 调整子图间距

    fig.savefig('fig3.png', dpi=300)

with plt.style.context(['science', 'high-contrast', 'no-latex']):

    labels = ['1', '2', '3', '4', '5', '6']
    data_a = [111, 30, 18, 18, 3, 3]
    data_b = [731, 414, 242, 153, 106, 76]
    # data_c = [12, 20, 24, 17, 16]
    x = np.arange(len(labels))
    width = .25
    # plots
    fig, ax = plt.subplots(figsize=(5, 3), dpi=200)
    bar_a = ax.bar(x - width / 2, data_a, width, label='RFM')
    bar_b = ax.bar(x + width / 2, data_b, width, label='ML-RFM')
    # bar_c = ax.bar(x + width * 3 / 2, data_c, width, label='categoryC')
    ax.set_xticks(x + .1)
    ax.set_xticklabels(labels, size=10)
    ax.set(ylabel='RFM-Count')
    ax.set(xlabel='β (M)')
    # ax.set(xlabel='β (M)')
    ax.legend()

    text_font = {'size': '10', 'weight': 'bold', 'color': 'black'}
    ax.text(.03, .9, "", transform=ax.transAxes, fontdict=text_font, zorder=4)
    ax.text(.87, -.08, '', transform=ax.transAxes,
            ha='center', va='center', fontsize=5, color='black', fontweight='bold', family='Roboto Mono')
    plt.savefig('fig4.png', dpi=300, bbox_inches='tight')


with plt.style.context(['science', 'ieee', 'no-latex']):
    fig, axs = plt.subplots(2, 2, figsize=(4, 4))
    # for p in [10, 20, 50]:
    #     ax.plot(x, model(x, p), label=p)

    # app = [78, 80, 79, 81, 91, 95, 96]
    # x = np.linspace(100, 800, 7)
    # ax.plot(x, app, label="RFMUL")
    #
    # app = [60, 70, 75, 80, 90, 85, 88]
    # x = np.linspace(100, 800, 7)
    # ax.plot(x, app, label="RFMUL+")
    labels = ['25', '50', '75', '100']
    width = .25
    data = [65, 68, 70, 78]
    x = np.arange(len(labels))
    axs[0, 0].bar(x - width / 2, data, width, label="ML-RFM")
    axs[0, 0].set_xticks(x + .1)
    axs[0, 0].set_xticklabels(labels, size=8)
    # ax.legend(title='Algorithm')
    axs[0, 0].set(ylabel='${Runtime(s)}$')
    axs[0, 0].set(xlabel='%${transactions, μ=}$$10^{6}$')
    # axs[0, 0].autoscale(tight=True)
    axs[0, 0].set_title("(a) Fruithut")
    # axs[0, 0].set_aspect(1./axs[0, 0].get_data_ratio(), adjustable='box')   # 坐标轴正方形

    app = [200, 244, 320, 420]
    # x = np.linspace(100, 800, 7)
    x = [25, 50, 75, 100]
    axs[0, 1].plot(x, app, label="ML-RFM", color='red')
    axs[0, 1].set(ylabel='${Runtime(s)}$')
    axs[0, 1].set(xlabel='%${transactions, μ=}$$10^{4}$')
    axs[0, 1].autoscale(tight=True)
    axs[0, 1].set_title("(b) Chainstore")

    app = [78, 80, 85, 88]
    # x = np.linspace(100, 800, 7)
    x = [25, 50, 75, 100]
    axs[1, 0].plot(x, app, label="ML-RFM", color='red')
    axs[1, 0].set(ylabel='${Runtime(s)}$')
    axs[1, 0].set(xlabel='%${transactions, μ=}$$10^{4}$')
    axs[1, 0].autoscale(tight=True)
    axs[1, 0].set_title("(c) Connect")

    app = [1234, 1547, 1874, 1998]
    # x = np.linspace(100, 800, 7)
    x = [25, 50, 75, 100]
    axs[1, 1].plot(x, app, label="ML-RFM", color='red')
    axs[1, 1].set(ylabel='${Runtime(s)}$')
    axs[1, 1].set(xlabel='%${transactions, μ=}$$10^{4}$')
    axs[1, 1].autoscale(tight=True)
    axs[1, 1].set_title("(d) Liquor")

    fig.tight_layout()  # 调整整体空白
    # plt.figure(figsize=(6, 6.5))
    plt.subplots_adjust(wspace=0.5, hspace=0.5)  # 调整子图间距

    fig.savefig('fig5.png', dpi=300)

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Loganer

感谢

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值