Arrange plots that have subplots called from functions on grid in matplotlib

南楼画角 提交于 2019-12-09 04:11:31

The usual way to create plots with matplotlib would be to create some axes first and then plot to those axes. The axes can be set up on a grid using plt.subplots, figure.add_subplot, plt.subplot2grid or more sophisticated, using GridSpec.

Once those axes are created, they can be given to functions, which plot content to the axes. The following would be an example where 6 axes are created and 3 different functions are used to plot to them.

import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
import numpy as np

def func1(ax, bx, cx):
    x = np.arange(3)
    x2 = np.linspace(-3,3)
    y1 = [1,2,4]
    y2 = [3,2.5,3.4]
    f = lambda x: np.exp(-x**2)
    ax.bar(x-0.5, y1, width=0.4)
    ax.bar(x, y2, width=0.4)
    bx.plot(x,y1, label="lab1")
    bx.scatter(x,y2, label="lab2")
    bx.legend()
    cx.fill_between(x2, f(x2))

def func2(ax, bx):
    x = np.arange(1,18)/1.9
    y = np.arange(1,6)/1.4
    z = np.outer(np.sin(x), -np.sqrt(y)).T
    ax.imshow(z, aspect="auto", cmap="Purples_r")
    X, Y = np.meshgrid(np.linspace(-3,3),np.linspace(-3,3))
    U = -1-X**2+Y
    V = 1+X-Y**2
    bx.streamplot(X, Y, U, V, color=U, linewidth=2, cmap="autumn")

def func3(ax):
    data = [sorted(np.random.normal(0, s, 100)) for s in range(2,5)]
    ax.violinplot(data)


gs = gridspec.GridSpec(3, 4, 
                width_ratios=[1,1.5,0.75,1],  height_ratios=[3,2,2] )

ax1 = plt.subplot(gs[0:2,0])
ax2 = plt.subplot(gs[2,0:2])
ax3 = plt.subplot(gs[0,1:3])
ax4 = plt.subplot(gs[1,1])
ax5 = plt.subplot(gs[0,3])
ax6 = plt.subplot(gs[1:,2:])

func1(ax1, ax3, ax5)
func3(ax2)
func2(ax4, ax6)

plt.tight_layout()
plt.show()

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