Matplotlib: plotting transparent histogram with non transparent edge

前端 未结 3 513
粉色の甜心
粉色の甜心 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:36

    I needed a solution which did not require me to set the color explicitly, i.e., I wanted to still use the default color cycle. The following solution builds on @ljetibo's idea to draw the histogram twice using @ali_m's idea to extract the state of the color cycle:

    # increment and get the "props" cycle (and extract the color)
    color = next(ax._get_lines.prop_cycler)["color"]
    # 1. draw: inner area with alpha
    ax.hist(data, color=color, alpha=0.3)
    # 2. draw: only edges with full color
    ax.hist(data, color=color, edgecolor=color, fc="None", lw=1)
    

    Note that specifying color=color in addition to edgecolor seems to be necessary. Otherwise the hist call itself will increment the color cycle again.

提交回复
热议问题