论文绘图之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)

你可能感兴趣的:(Python,工具,python)