1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

[Python] Show the marker and the line at the same time at the legend of boxplot

Discussão em 'Python' iniciado por Stack, Setembro 13, 2024.

  1. Stack

    Stack Membro Participativo

    I want to show marker without the line, mean, median line in the legend. I don't know how to remove the line in the marker

    I try to add the handlelength=0. in the legend, but it will remove all the line in the legend.

    #draw boxplot
    medianprops = dict(linestyle='dashdot', linewidth=1, color='darkgoldenrod')
    meanprops = dict(linestyle=(0,(3, 1, 1, 1)), linewidth=1, color='fuchsia')
    # extract the characteristic in the boxplot
    B = ax[1].boxplot(data, vert=0, showfliers=False, medianprops=medianprops, showmeans=True, meanline=True, meanprops=meanprops)
    lower_bound, upper_bound = [item.get_xdata()[1] for item in B['whiskers']]
    boxes = [item.get_xdata() for item in B['boxes']][0]

    # cal the condition of outlier
    q1 = np.percentile(data, 25)
    q3 = np.percentile(data, 75)
    iqr = q3 - q1
    upper_box = boxes[2]
    mild_c = 1.5 * iqr + upper_box
    extr_c = 3.0 * iqr + upper_box
    # catagorize outlier
    mild_outlier = data[data >= mild_c]
    mild_outlier = mild_outlier[mild_outlier < extr_c]
    extreme_outlier = data[data >= extr_c]
    # draw the figure
    ax[1].set_xlabel('Fuel Efficiency (mpg)')
    ax[1].set_title('boxplot of Fuel Efficincies(mpg)')
    marker1 = ax[1].plot(mild_outlier, np.ones_like(mild_outlier), marker='+', color='blue', linestyle = None)
    marker2 = ax[1].plot(extreme_outlier, np.ones_like(extreme_outlier), marker='+', color='red')
    print(B)
    # draw label
    ax[1].legend([B["medians"][0], B["means"][0], marker1[0]], ['median', 'mean', 'marker'], loc='upper right')

    plt.show()


    enter image description here

    Continue reading...

Compartilhe esta Página