matplotlib loop make subplot for each category

前端 未结 2 1188
自闭症患者
自闭症患者 2021-01-31 23:38

I am trying to write a loop that will make a figure with 25 subplots, 1 for each country. My code makes a figure with 25 subplots, but the plots are empty. What can I change to

2条回答
  •  滥情空心
    2021-02-01 00:14

    I don't remember that well how to use original subplot system but you seem to be rewriting the plot. In any case you should take a look at gridspec. Check the following example:

    import matplotlib.pyplot as plt
    import matplotlib.gridspec as gridspec
    
    fig = plt.figure()
    
    gs1 = gridspec.GridSpec(5, 5)
    countries = ["Country " + str(i) for i in range(1, 26)]
    axs = []
    for c, num in zip(countries, range(1,26)):
        axs.append(fig.add_subplot(gs1[num - 1]))
        axs[-1].plot([1, 2, 3], [1, 2, 3])
    
    plt.show()
    

    Which results in this:

    Just replace the example with your data and it should work fine.

    NOTE: I've noticed you are using xrange. I've used range because my version of Python is 3.x. Adapt to your version.

提交回复
热议问题