Enumerate plots in matplotlib figure

前端 未结 3 887
孤独总比滥情好
孤独总比滥情好 2020-12-20 14:36

In a matplotlib figure I would like to enumerate all (sub)plots with a), b), c) and so on. Is there a way to do this automatically?

So far I use the individual plots

3条回答
  •  温柔的废话
    2020-12-20 15:15

    A super quick way to do this is to take advantage of the fact that chr() casts integers to characters. Since a-z fall in the range 97-122, one can do the following:

    import matplotlib.pyplot as plt
    
    fig,axs = plt.subplots(2,2)
    for i,ax in enumerate(axs.flat, start=97):
      ax.plot([0,1],[0,1])
      ax.text(0.05,0.9,chr(i)+')', transform=ax.transAxes)
    

    which produces:

提交回复
热议问题