Is it possible to automatically generate multiple subplots in matplotlib?

。_饼干妹妹 提交于 2019-12-10 10:42:10

问题


Is it possible to automatically generate multiple subplots in matplotlib? An example of the process I want to automate is:

import matplotlib.pyplot as plt
figure = plt.figure()
ax1 = figure.add_subplot(2, 3, 1)
ax2 = figure.add_subplot(2, 3, 2)
ax3 = figure.add_subplot(2, 3, 3)
ax4 = figure.add_subplot(2, 3, 4)
ax5 = figure.add_subplot(2, 3, 5)
ax6 = figure.add_subplot(2, 3, 6)

The subplots need unique names, as this will allow me to do stuff like:

for ax in [ax1, ax2, ax3, ax4, ax5, ax6]:
    ax.set_title("example")

Many thanks.

Addition: Are there any functions that automate the generation of multiple subplots? What if I needed to repeat the above process 100 times? Would I have to type out every ax1 to ax100?


回答1:


You can use:

fig, axs = plt.subplots(2,3)

axs will be an array containing the subplots.

Or unpack the array instantly:

fig, ((ax1, ax2, ax3), (ax4, ax5, ax6)) = plt.subplots(2,3)


来源:https://stackoverflow.com/questions/27185139/is-it-possible-to-automatically-generate-multiple-subplots-in-matplotlib

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!