在matplotlib中安排从grid上的函数调用子图的图 [英] Arrange plots that have subplots called from functions on grid in matplotlib

查看:283
本文介绍了在matplotlib中安排从grid上的函数调用子图的图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有一个函数(比如函数 FUN1 )创建一个情节与subplots。子图 FUN1 创建的数量可能会有所不同,并且图本身非常复杂。我还有另外两个函数 FUN2 FUN3 ,这些函数也创建了不同结构的图表。



是否有简单的方法来定义/排列整个GRID ,例如一个简单的3行1列样式,并且只需传递

  FUN1  - > GRID(第1行,第1列)
FUN2 - > GRID(第2行,第1列)
FUN3 - > GRID(第3行,第1列)

之后,由 FUN1 在第1行绘制,第2行由FUN2生成的绘图等等,之前没有在FUN中指定子绘图条件?

解决方案

使用matplotlib创建图的常用方法是首先创建一些轴,然后绘制到这些轴。轴可以使用 plt.subplots figure.add_subplot plt.subplot2grid 或更复杂,使用 GridSpec

一旦创建了这些轴,就可以将这些轴赋予函数,这些函数将内容绘制到轴上。下面是一个例子,其中创建了6个轴,并使用了3个不同的函数来绘制它们。

  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))。
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 + XY ** 2
bx.streamplot(X,Y,U,V,color = U,linewidth = 2,cmap =autumn)

def func3(ax):
data = [sort(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])$ ​​b $ b 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()


I am looking for something similar to arrangeGrob in R:

I have a function (say, function FUN1) that creates a plot with subplots. The number of subplots FUN1 creates may vary and the plot itself is quite complex. I have two other functions FUN2 and FUN3 which also create plots of varying structure.

Is there a simple way to define/arrange an overall GRID, for example a simple 3 rows 1 column style and simply pass

FUN1 --> GRID(row 1, col 1)
FUN2 --> GRID(row 2, col 1)
FUN3 --> GRID(row 3, col 1)

afterwards such that the complicated plot generated by FUN1 gets plotted in in row 1, the plot generated by FUN2 in row 2 and so on, without specifying the subplot criteria in the FUNs before?

解决方案

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()

这篇关于在matplotlib中安排从grid上的函数调用子图的图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆