Home  >  Q&A  >  body text

python - matplotlib pie how to set alpha

matplotlib.pyplot.bar has the alpha parameter, but it seems that an error will be reported when using it in pie. The built-in color is too ugly! Ask God for enlightenment!

黄舟黄舟2660 days ago989

reply all(1)I'll reply

  • 我想大声告诉你

    我想大声告诉你2017-06-12 09:27:48

    Because I don’t have the code, I’m not sure about your actual situation, but here are some examples with alpha values ​​set

    • polar_bar_demo.py uses bar.set_alpha(...)

    • matplotlib.patches.Patch uses fc=(0, 0, 1, 0.5)
      Basically, it is to find an object to set the alpha value such as XXX.set_aplha() or to give a four-digit tuple when passing parameters to set the facecolor fc value.

    See code

    # Pie chart, where the slices will be ordered and plotted counter-clockwise:
    labels = 'Frogs', 'Hogs', 'Dogs', 'Logs'
    sizes = [15, 30, 45, 10]
    explode = (0, 0.1, 0, 0)  # only "explode" the 2nd slice (i.e. 'Hogs')
    
    fig1, ax1 = plt.subplots()
    patches, texts, autotexts = ax1.pie(sizes, explode=explode, labels=labels, autopct='%1.1f%%',
            shadow=True, startangle=90)
    
    for i, p in enumerate(patches):
        p.set_alpha(0.25*i)
    
    plt.show()
    

    patches/wedges set_alpha is fine.

    See more examples: Wedge

    reply
    0
  • Cancelreply