Matplotlib mixed subheadings with graph and pcolor (grid) to have the same x axis length when the color bar is on

I am wondering if it is possible to have a subtitle with a mixture of graphs drawn using graph () and drawings drawn using pcolormesh () that will have the same x axis length.

I attached 2 images, one when the pcolormesh () submarine does not have a color panel, when the x axes have the same length enter image description here and the one that has a color panel when the pcolormesh () subheading is shortened so that it contains the color panel.enter image description here

I would like to have a colorbar, but it should extend the length of the entire graph and not shorten the subtitle.

Is this possible without subclassing any of the current classes?

+4
1

, !

, , , pcolormesh. , , . .

, :

import matplotlib.pyplot as plt
import numpy as np
np.random.seed(1977)

# Generate some interesting-looking random data...
num = 200
grid = np.random.normal(0, 1, (20, num)).cumsum(axis=1).cumsum(axis=0)
x = np.linspace(0, 360, num)
y1 = np.random.normal(0, 1, num).cumsum()
y2 = np.random.normal(0, 1, num).cumsum()

# Plot on three seperate axes
fig, axes = plt.subplots(nrows=3, sharex=True)
axes[0].plot(x, y1)
axes[1].plot(x, y2)
im = axes[2].imshow(grid, extent=[0, 360, 0, 20], aspect='auto')
fig.colorbar(im)

plt.show()

enter image description here


, :

import matplotlib.pyplot as plt
import numpy as np
np.random.seed(1977)

# Generate some interesting-looking random data...
num = 200
grid = np.random.normal(0, 1, (20, num)).cumsum(axis=1).cumsum(axis=0)
x = np.linspace(0, 360, num)
y1 = np.random.normal(0, 1, num).cumsum()
y2 = np.random.normal(0, 1, num).cumsum()

# Plot on three seperate axes
fig, axes = plt.subplots(nrows=3, sharex=True)
axes[0].plot(x, y1)
axes[1].plot(x, y2)
im = axes[2].imshow(grid, extent=[0, 360, 0, 20], aspect='auto')
fig.colorbar(im, orientation='horizontal')

plt.show()

enter image description here


. , .

( : tight_layout . , tight_layout . , subplots_adjust.)

import matplotlib.pyplot as plt
import numpy as np
np.random.seed(1977)

# Generate some interesting-looking random data...
num = 200
grid = np.random.normal(0, 1, (20, num)).cumsum(axis=1).cumsum(axis=0)
x = np.linspace(0, 360, num)
y1 = np.random.normal(0, 1, num).cumsum()
y2 = np.random.normal(0, 1, num).cumsum()

# Plot on three seperate axes
fig, axes = plt.subplots(nrows=3, sharex=True)
axes[0].plot(x, y1)
axes[1].plot(x, y2)
im = axes[2].imshow(grid, extent=[0, 360, 0, 20], aspect='auto')

# Make some room for the colorbar
fig.subplots_adjust(left=0.07, right=0.87)

# Add the colorbar outside...
box = axes[2].get_position()
pad, width = 0.02, 0.02
cax = fig.add_axes([box.xmax + pad, box.ymin, width, box.height])
fig.colorbar(im, cax=cax)

plt.show()

enter image description here

+8

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


All Articles