简体   繁体   中英

Vertical arrangement in matplotlib subplots

I am trying to plot a figure with 4 subplots. The following code is similar to what I have written.

import numpy as np
import matplotlib.pyplot as plt
def trial(x, r, ax=None):
    y = r*np.sin(x)
    ax[0].plot(x, r*x)
    ax[0].set_title('x')
    ax[1].plot(x, y)
    ax[1].set_title('sinx')

x = np.linspace(0, 4*np.pi)    
fig, (ax1, ax2) = plt.subplots(2,2)
trial(x, 1, ax1)
trial(x, 2, ax2)

This is the output I get.

这是我得到的输出。

If I only plot ax1(after removing last index from subplots), x and sin(x) are vertically arranged. But as soon as I add ax2, they become horizontally arranged. I want x and sin(x) to be vertically arranged with both ax1 and ax2. What correction should I make?

Let's try:

fig, axes = plt.subplots(2,2)
trial(x, 1, axes[:,0])
trial(x, 2, axes[:,1])

Output:

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM