How can I build multiple shapes on the same line with matplotlib?

In mine Ipython Notebook, my script turns out a series of several digits, for example: enter image description here

The problem is that these numbers take too much space, and I produce many of these combinations. It is very difficult for me to move between these numbers.

I want to make part of the plot on the same line. How can I do it?

UPDATE:

Thanks for the fjarri suggestion, I changed the code and it works for building on one line.

Now I want to plot them on different lines (default option). What should I do? I tried some, but not sure if this is the right way.

def custom_plot1(ax = None):
    if ax is None:
        fig, ax = plt.subplots()
    x1 = np.linspace(0.0, 5.0)
    y1 = np.cos(2 * np.pi * x1) * np.exp(-x1)
    ax.plot(x1, y1, 'ko-')
    ax.set_xlabel('time (s)')
    ax.set_ylabel('Damped oscillation')

def custom_plot2(ax = None):
    if ax is None:
        fig, ax = plt.subplots()
    x2 = np.linspace(0.0, 2.0)
    y2 = np.cos(2 * np.pi * x2)
    ax.plot(x2, y2, 'r.-')
    ax.set_xlabel('time (s)')
    ax.set_ylabel('Undamped')

# 1. Plot in same line, this would work
fig = plt.figure(figsize = (15,8))
ax1 = fig.add_subplot(1,2,1, projection = '3d')
custom_plot1(ax1)
ax2 = fig.add_subplot(1,2,2)
custom_plot2(ax2)

# 2. Plot in different line, default option
custom_plot1()
custom_plot2()

enter image description here

+4
source share
1 answer

.

plt.plot(data1)
plt.show()
plt.subplot(1,2,1)
plt.plot(data2)
plt.subplot(1,2,2)
plt.plot(data3)
plt.show()

( , )

2, : :

# 1. Plot in same line, this would work
fig = plt.figure(figsize = (15,8))
ax1 = fig.add_subplot(1,2,1, projection = '3d')
custom_plot1(ax1)
ax2 = fig.add_subplot(1,2,2)
custom_plot2(ax2)

# 2. Plot in same line, on two rows
fig = plt.figure(figsize = (8,15))                  # Changed the size of the figure, just aesthetic
ax1 = fig.add_subplot(2,1,1, projection = '3d')     # Change the subplot arguments
custom_plot1(ax1)
ax2 = fig.add_subplot(2,1,2)                        # Change the subplot arguments
custom_plot2(ax2)

( , " " ), , , .

: subplot(rows, cols, axnum) rows , . cols , . axnum , .

, , β†’ subplot(1,2,...)

, , , 2 β†’ subplot(2,1,...)

gridspec http://matplotlib.org/users/gridspec.html

+5

Source: https://habr.com/ru/post/1620126/


All Articles