簡體   English   中英

如何在matplotlib中以對數刻度和其他線性顯示間隔

[英]how to show an interval in logarithmic scale and other linear in matplotlib

我正在使用 matplotlib,我想繪制一個圖表,其中 y 軸的負數部分具有較大的數字,而正數部分具有較小的值,而正數部分更有價值。 所以我更喜歡以對數刻度繪制 y 軸的負部分,以線性(正常)刻度繪制正部分。 在這里,我用對數比例顯示整個情節,這是我的代碼:

    plt.plot(time_stamps,objs,'-rD', markevery=markers_on )
    markers_on=  list(i for i in range(len(upper_bound)))
    plt.plot(time_stamps,upper_bound,'-bD', markevery=markers_on )
    markers_on=  list(i for i in range(len(best_P_list)))
    plt.plot(exce_time_list,best_P_list,'-gD', markevery=markers_on)
    plt.xlabel("x", fontsize=10)
    plt.ylabel("y ", fontsize=10)
    plt.yscale('symlog')
    plt.show() 

在此處輸入圖像描述

如何以線性比例顯示 y 軸的正部分?

我完全修改了我的答案,因為我找到了這個 SO 答案,它解釋了如何划分軸以表現不同。

現在,我們仍然需要區分不同的值和步驟,但它更清晰。

import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import make_axes_locatable
import numpy as np


vals = np.array([-1e10, -1e7, 1e3, 1e2, -1e8, -1e1, 1e3, 500])
steps = np.array([0, 5, 1, 4, 9, 20, 7, 15])


def plot_diff_scales(vals, steps):
    fig, ax = plt.subplots()
    ax.plot(steps[vals >= 0], vals[vals >= 0], "-gD")  # only positive values
    ax.set_yscale("linear")
    ax.spines["bottom"].set_visible(False)  # hide bottom of box
    ax.get_xaxis().set_visible(False)  # hide x-axis entirely
    ax.set_ylabel("Linear axis")
    # Dividing the axes
    divider = make_axes_locatable(ax)
    ax_log = divider.append_axes("bottom", size=2, pad=0, sharex=ax)
    # Acting on the log axis
    ax_log.set_yscale("symlog")
    ax_log.spines["top"].set_visible(False)  # hide top of box
    ax_log.plot(steps[vals < 0], vals[vals < 0], "-bD")  # only negative values
    ax_log.set_xlabel("X axis")
    ax_log.set_ylabel("Symlog axis")
    # Show delimiter
    ax.axhline(y=0, color="black", linestyle="dashed", linewidth=1)
    # Plotting proper
    fig.tight_layout()
    plt.show()


plot_diff_scales(vals, steps)

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM