0

I have a code block that produces a 3d graph

ax = plt.axes(projection = '3d')
ax.plot3D(outputs_real, outputs_imaginary, inputs)
ax.set_xlabel('Real Component')
ax.set_ylabel('Imaginary Component')
ax.set_zlabel('Inputs')
plt.show()

enter image description here and a second code block that makes a 2d graph using 2 of the above's axes.

ax2 = plt.plot(outputs_real, outputs_imaginary)
ax2.set_xlabel('Real Component')
ax2.set_ylabel('Imaginary Component')

But for some reason when I try to plot the second one I'll get this error

AttributeError: 'list' object has no attribute 'set_xlabel'

and the second graph will map onto the first like this: enter image description here

I want the two graphs to be separate

Details:

  1. This is being done in a Jupyter notebook
  2. outputs_real, outputs_imaginary, and inputs are all lists
  • `ax2 = plt.plot(...)` is never correct. If you want to create subplots, you need something like [this tutorial example](https://matplotlib.org/stable/gallery/mplot3d/mixed_subplots.html) – JohanC Dec 23 '21 at 20:14
  • See also [How to scale 3D and 2D subplots so that a corresponding axis has the same length?](https://stackoverflow.com/questions/66154633/how-to-scale-3d-and-2d-subplots-so-that-a-corresponding-axis-has-the-same-length) and [remove 3D plot's white spaces in mixed 2D/3D subplots](https://stackoverflow.com/questions/44917886/matplotlib-remove-3d-plots-white-spaces-in-mixed-2d-3d-subplots) – JohanC Dec 23 '21 at 20:19

2 Answers2

0

From matplotlib documentation plt.plot returns a list of lines representing the plotted data.

If you want to change the x and y label use plt.xlabel('text') and plt.ylabel('text') as following:

plt.plot(outputs_real, outputs_imaginary)
plt.xlabel('Real Component')
plt.ylabel('Imaginary Component')

An example to create a 2D and 3D separate graphs:

import numpy as np
from matplotlib import pyplot as plt

# create 3d data
z = np.linspace(0, 15, 1000)
x = np.sin(z)
y = np.cos(z)

# plot 3D
fig = plt.figure(figsize=(10,5))
ax = fig.add_subplot(111, projection='3d')
ax.plot3D(x, y, z)
ax.set_title("3D plot")
ax.set_xlabel("X")
ax.set_ylabel("Y")
ax.set_zlabel("Z")
plt.show()

# plot 2D
plt.plot(x, y)
plt.title("2D plot")
plt.xlabel("X")
plt.ylabel("Y")
plt.show()

This will create the following two plots:

3D

2D

RoseGod
  • 1,206
  • 1
  • 9
  • 19
0

One option is to create a new figure. After you have called the first plt.show() create a new figure and ax with

plt.figure(2)
ax2 = plt.axes()

You can also plot the two figures simultaneously if you want, by not calling the first plt.show() and only calling plt.show() after creating both of the figures.

Ilmard
  • 86
  • 4