0

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

3
  • Maybe use fig = plt.figure(figsize=(20,10))? Commented Jan 30, 2021 at 15:37
  • Then the right image is a square, which will be correct, but it is still over the first image. I'll edit my question. Commented Jan 30, 2021 at 16:01
  • @Red-Cloud : Does my answer solve your problem ? Commented Jan 30, 2021 at 19:04

1 Answer 1

2
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

Sign up to request clarification or add additional context in comments.

1 Comment

thanks. My projection='3d' was set too early

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.