代码之家  ›  专栏  ›  技术社区  ›  tandem

增加Y刻度和自定义X轴之间的间距

  •  0
  • tandem  · 技术社区  · 6 年前

    我试图建立一个顶部有指数函数,底部有效用函数的图。顶部的y轴表示延迟,x轴表示拥塞;同样,在第二个图中,y轴表示吞吐量,x轴表示拥塞。

    在我不能得到的情况下,如何将x轴设置成百分比,并且有一种方法来叠加这两个图。

    #!/usr/bin/env python3
    
    import numpy as np
    import math
    import matplotlib.mlab as mlab
    import matplotlib.pyplot as plt
    import matplotlib
    
    fig = plt.figure()
    x = np.arange(1,9,1)
    y = [math.exp(_) for _ in x]
    ax = fig.add_subplot(211)
    ax.plot(x, y)
    ax.set_ylabel('Y_plot1')
    ax.set_xlabel('X_plot1')
    ax.set_yticks([],[])
    ax.spines['right'].set_visible(False)
    ax.spines['top'].set_visible(False)
    ax.xaxis.set_ticks_position('bottom')
    ax.yaxis.set_ticks_position('left')
    ax.yaxis.set_tick_params(which='major', direction='out')
    
    ax.set_ymargin(1)
    
    ax1 = fig.add_subplot(212)
    mu = 5
    variance = 1
    sigma = math.sqrt(variance)
    x_normal = np.linspace(mu - 3*sigma, mu + 3*sigma, 100)
    y_normal = mlab.normpdf(x_normal, mu, sigma)
    #y_normal += 1000
    x_normal = [0, 0] + list(x_normal)
    y_normal = [0, 0] + list(y_normal)
    ax1.plot(x_normal, y_normal)
    ax1.set_ylabel('Y_plot2')
    ax1.set_xlabel('X_plot2')
    ax1.set_yticks([],[])
    ax1.spines['right'].set_visible(False)
    ax1.spines['top'].set_visible(False)
    ax1.xaxis.set_ticks_position('bottom')
    ax1.yaxis.set_ticks_position('left')
    ax1.set_ymargin(1)
    fig.tight_layout()
    fig.savefig('bw-latency' +'.pdf',format='pdf',bbox_inches='tight', pad_inches=0.1, dpi=1000)
    plt.clf()
    plt.close()
    
    1 回复  |  直到 6 年前
        1
  •  0
  •   f.wue    6 年前

    若要将X轴转换为%,则可以正常化。 x_normal 并调整 xticks :

    x_normal = x_normal/(max(x_normal)-min(x_normal)) + min(x_normal)
    ax1.plot(x_normal, y_normal)
    ax1.set_xticks(np.linspace(0,1,5))
    ax1.set_xticklabels([str(int(i*100)) for i in np.linspace(0,1,5)])
    

    要叠加两个图形,请查看: https://matplotlib.org/gallery/api/two_scales.html

    我是你的案子:

    ax3 = ax1.twinx()
    y = [math.exp(_) for _ in x_normal]
    ax3.plot(x_normal, y,color="r")
    

    编辑: 这是你想要的输出吗?: enter image description here

    这是对我有用的代码:

    def plot_percentage(x, y, ax):
        x = x/max(x)
        ax.plot(x, y)
        ax.set_xticks(np.linspace(0, 1, 10))
        ax.set_xticklabels([str(int(i*100)) for i in np.linspace(0,1, 10)])
    
    fig = plt.figure()
    x = np.arange(1,9,1)
    y = [math.exp(_) for _ in x]
    ax = fig.add_subplot(211)
    plot_percentage(x, y, ax)
    ax.set_ylabel('Y_plot1')
    ax.set_xlabel('X_plot1')
    ax.set_yticks([],[])
    ax.spines['right'].set_visible(False)
    ax.spines['top'].set_visible(False)
    ax.xaxis.set_ticks_position('bottom')
    ax.yaxis.set_ticks_position('left')
    ax.yaxis.set_tick_params(which='major', direction='out')
    
    ax.set_ymargin(1)
    
    ax1 = fig.add_subplot(212)
    mu = 5
    variance = 1
    sigma = math.sqrt(variance)
    x_normal = np.linspace(mu - 3*sigma, mu + 3*sigma, 100)
    y_normal = mlab.normpdf(x_normal, mu, sigma)
    #y_normal += 1000
    x_normal = [0, 0] + list(x_normal)
    y_normal = [0, 0] + list(y_normal)
    plot_percentage(x_normal, y_normal, ax1)
    
    ax3 = ax1.twinx()
    y = [math.exp(_) for _ in x_normal]
    plot_percentage(x_normal, y, ax3)
    plt.show()