[Python] plot modification to align both x-axes and start from where it exists in list

Discussão em 'Python' iniciado por Stack, Fevereiro 11, 2025.

  1. Stack

    Stack Membro Participativo

    size = 18
    value = [20, 25, 30, 35]
    x_values1 = list(range(0, 100, 5))
    x_values2 = [0.0, 20.0, 40.0, 60.0, 80.0, 90.0, 95.0]
    x_values3 = [80,76,72,68,64,60,56,52,48,44,40,36,32,28,24,20,16,12,8,4]
    y_value1 = [8.226502331074E-07, 2.23276092438077E-06, 4.05102969501409E-06, 6.37286188209644E-06, 9.0948649799175E-06,
    1.1823432395575E-05, 1.39950605433403E-05, 1.52880124352018E-05, 1.59623881451955E-05, 1.66013609598997E-05,
    1.75776082796254E-05, 1.89088470296623E-05, 2.04282721607529E-05, 2.19342009434028E-05, 2.32357328236355E-05,
    2.41468143248193E-05, 2.44765091608964E-05, 2.40357022548111E-05, 2.26629075512208E-05, 2.02551778821116E-05]
    y_value2 = [5.20787001629681E-07, 9.49836092316734E-06, 1.55921680069521E-05, 2.14212986542911E-05,
    2.44079518003369E-05, 2.50048219351487E-05, 2.07892738820319E-05]
    fig = plt.figure(figsize=(10, 10)) #dpi=300)
    ax1 = fig.add_subplot(111)
    ax2 = ax1.twiny()
    prop = ax1._get_lines.prop_cycler
    prop = ax2._get_lines.prop_cycler
    marker1 = itertools.cycle(('o', '*', 'D', 'x', 's', '^', 'v', '<', '>', ','))
    marker2 = itertools.cycle(('o', '*', 'D', 'x', 's', '^', 'v', '<', '>', ','))
    for i in value:
    color = next(prop)['color']
    column = 'Au_%0.3f' %(i/100)
    column1 = 'residual_Ag_%0.3f' %(i/100)
    ax1.plot(x_values1, y_value1, linewidth=3, color=color)
    ax1.scatter(x_values2, y_value2, marker=next(marker1), s=150, color=color)
    ax1.plot([], [], marker=next(marker2), label='x=%0.3f' %(i/100), linewidth=3, markersize=15, color=color)
    ax2.plot(x_values3, y_value1, linewidth=3, color=color)

    ax1.tick_params(axis='both', which='major', labelsize=25, length=10, width=2)
    ax2.tick_params(axis='both', which='major', labelsize=25, length=10, width=2)
    ax1.legend(loc='lower right', fontsize=25)
    ax1.set_xlabel('xlabel1', fontsize=25)
    ax1.set_ylabel('ylabel', fontsize=25)
    ax2.set_xlabel('xlabel1', fontsize=25)
    plt.rcParams["font.family"] = "Arial"
    for axis in ['top', 'bottom', 'right', 'left']:
    ax1.spines[axis].set_linewidth(2)
    ax2.spines[axis].set_linewidth(2)
    ax1.yaxis.get_offset_text().set_fontsize(25)
    plt.tight_layout()
    plt.show()


    when this code is run I get the plot [​IMG]

    This code plots a graph as shown in the figure. I want the x-axes to be aligned and the secondary axis to remain as it is code i.e., in decreasing order.

    Continue reading...

Compartilhe esta Página