[Python] How to add deep colored edges to the bars of a histogram with the same filled color...

Discussão em 'Python' iniciado por Stack, Abril 16, 2025.

  1. Stack

    Stack Membro Participativo

    I was using matplotlib to plot a histogram. Usually, I use edgecolor to specify the color of the edges. And if I don't, there will be no edges.

    But, I just created a pdf of a histogram which has deep colored edges with the same filled color of the bars, however I did not set the edgecolor parameter. Strangely, if I change the format to png, the final plot will not have colored edges.

    [​IMG]

    The plotting code is :

    x1 = np.random.normal(0, 0.8, 1000)
    x2 = np.random.normal(-2, 1, 1000)
    x3 = np.random.normal(3, 2, 1000)

    fig = plt.figure()
    ax = fig.add_subplot()
    plt.hist(data, alpha=0.3, histtype='barstacked', bins=40)
    fig.savefig('barstacked.pdf')
    fig.savefig('barstacked.png', dpi=750)


    Even stranger, if I set histtype='stepfilled', the histograms in both pdf and png formats have no colored edges.

    [​IMG]

    fig = plt.figure()
    ax = fig.add_subplot()
    plt.hist(data, alpha=0.3, histtype='stepfilled', bins=40)
    fig.savefig('stepfilled.pdf')
    fig.savefig('stepfilled.png', dpi=750)


    Does anyone know what parameters can be set to get this colored edge in matplotlib, or is this automatically obtained when rendering pdf?

    Continue reading...

Compartilhe esta Página