Red-Cloud
Red-Cloud

Reputation: 458

Python subplot and image size

What I am doing wrong to get the two figures side by side and both 10x10? One on top of the other would also be okay.

import matplotlib.pyplot as plt
import numpy as np

def f(x, y):
    return np.sin(x)*np.sin(y)

x = np.linspace(-np.pi, np.pi, 100)
y = np.linspace(-np.pi, np.pi, 100)

X, Y = np.meshgrid(x, y)
Z = f(X, Y)

fig = plt.figure(figsize=(20,10))
ax1 = fig.add_subplot(1,2,1)#    subplot(nrows, ncols, plot_number)
ax1 = plt.axes(projection='3d')
ax1.view_init(20, 25)
ax1.plot_surface(X, Y, Z, cmap='RdGy', edgecolor='none')
ax1.set_xlabel('x')
ax1.set_ylabel('y')
ax1.set_zlabel('z')

ax2 = fig.add_subplot(1,2,2)
ax2.contourf(X, Y, Z, 40, cmap='RdGy')
ax2.axis('equal')

plt.show()

enter image description here

Upvotes: 0

Views: 297

Answers (1)

Jay Patel
Jay Patel

Reputation: 1420

import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d import Axes3D

def f(x, y):
    return np.sin(x)*np.sin(y)

x = np.linspace(-np.pi, np.pi, 100)
y = np.linspace(-np.pi, np.pi, 100)

X, Y = np.meshgrid(x, y)
Z = f(X, Y)

fig = plt.figure(figsize=(20,10))

ax = fig.add_subplot(1, 2, 1, projection='3d')
ax.view_init(20, 25)
ax.plot_surface(X, Y, Z, cmap='RdGy', edgecolor='none')
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z')

ax = fig.add_subplot(1, 2, 2)
ax.contourf(X, Y, Z, 40, cmap='RdGy')
plt.tight_layout()
plt.show()

enter image description here

You can also plot both in single plot, refer below code.

import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d import Axes3D

def f(x, y):
    return np.sin(x)*np.sin(y)

x = np.linspace(-np.pi, np.pi, 100)
y = np.linspace(-np.pi, np.pi, 100)

X, Y = np.meshgrid(x, y)
Z = f(X, Y)

fig = plt.figure(figsize=(15,10))

ax = fig.add_subplot(1, 1, 1, projection='3d')
ax.view_init(20, 25)
ax.plot_surface(X, Y, Z, cmap='autumn_r', lw=0.5,edgecolor='black', rstride=1, cstride=1)
ax.contour(X, Y, Z, 10, lw=3, cmap="autumn_r", linestyles="solid", offset=-1)
ax.contour(X, Y, Z, 10, lw=3, colors="k", linestyles="solid")
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z')

enter image description here

If a add transparency to the surface facets then I can see the contours, but it looks really cluttered (see code and image below)

import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d import Axes3D

def f(x, y):
    return np.sin(x)*np.sin(y)

x = np.linspace(-np.pi, np.pi, 100)
y = np.linspace(-np.pi, np.pi, 100)

X, Y = np.meshgrid(x, y)
Z = f(X, Y)

fig = plt.figure(figsize=(15,10))

ax = fig.add_subplot(1, 1, 1, projection='3d')
ax.view_init(45, 35)
ax.plot_surface(X, Y, Z, cmap='autumn_r', lw=0.5, rstride=1, cstride=1, alpha=0.5)
ax.contour(X, Y, Z, 10, lw=3, cmap="autumn_r", linestyles="solid", offset=-1)
ax.contour(X, Y, Z, 10, lw=3, colors="b", linestyles="solid")
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z')

enter image description here

Upvotes: 2

Related Questions