Matplotlib: plotting transparent histogram with non transparent edge

前端 未结 3 490
粉色の甜心
粉色の甜心 2021-02-01 15:46

I am plotting a histogram, and I have three datasets which I want to plot together, each one with different colours and linetype (dashed, dotted, etc). I am also giving some tra

3条回答
  •  刺人心
    刺人心 (楼主)
    2021-02-01 16:32

    I think perhaps the dumbest solution is the easiest and best? Just replot the histograms, without inner color and without alpha. That should replot just the histogram lines.

    Additionally you might want to eliminate the chance of having the lines/dashes/dots not overlap by removing them completely from the first histogram plot

    import matplotlib as mpl
    import matplotlib.pyplot as plt
    import numpy as np
    
    x = np.random.random(20)
    y = np.random.random(20)
    z = np.random.random(20)
    
    fig = plt.figure()
    ax = fig.add_subplot(111)
    
    ax.hist(x, bins=np.arange(0, 1, 0.1), edgecolor='None', alpha = 0.5, color= 'b')
    ax.hist(y, bins=np.arange(0, 1, 0.1), edgecolor='None', alpha = 0.5, color= 'r')
    ax.hist(z, bins=np.arange(0, 1, 0.1), edgecolor="None", alpha = 0.5, color= 'k')
    
    
    ax.hist(x, bins=np.arange(0, 1, 0.1), ls='dashed', lw=3, facecolor="None")
    ax.hist(y, bins=np.arange(0, 1, 0.1), ls='dotted', lw=3, facecolor="None")
    ax.hist(z, bins=np.arange(0, 1, 0.1), lw=3, facecolor="None")
    
    plt.show()
    

    without the second orders the graph should look like your figure without any borders. With the bottom 3 histogram commands borders are added to produce the image bellow. Works on Python3.4 Win7

    [cut because apparently I don't have enough rep to post pictures]

提交回复
热议问题