简体   繁体   中英

Plotting Complex values with Number of iterations in Python

I am solving Newton ralphson problem in which I am getting complex values of the first derivative, I have to plot the values of the first derivative vs the number of iterations, How do I plot the graph?

I am new to python so I am a little confused.

Edit: I want to plot the real part on the X-axis, the Imaginary on the Y, and the number of iterations on z.

Updated per your comment:

import matplotlib.pyplot as plt

# Define the first_derivative and iterations data
first_derivative = [1 + 1j, 2 + 2j, 3 + 3j, 4 + 4j, 5 + 5j]
iterations = [0, 1, 2, 3, 4]

# Split the first_derivative into real and imaginary parts
x = [val.real for val in first_derivative]
y = [val.imag for val in first_derivative]

# Create a figure and axis for the 3D plot
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

# Plot the data as a scatter plot
ax.scatter(x, y, iterations, c='r', marker='o')

# Set the x, y, and z labels
ax.set_xlabel('Real Part')
ax.set_ylabel('Imaginary Part')
ax.set_zlabel('Number of Iterations')

# Show the plot
plt.show()

在此处输入图像描述


Old Code

Using the matplotlib library:

import matplotlib.pyplot as plt

# Example values of first derivative and the number of iterations
first_derivative = [1 + 1j, 2 + 2j, 3 + 3j, 4 + 4j, 5 + 5j]
iterations = [0, 1, 2, 3, 4]

# Plot the values of first derivative vs number of iterations
plt.plot(iterations, first_derivative, 'ro')
plt.xlabel('Number of iterations')
plt.ylabel('First derivative')
plt.title('First derivative vs Number of iterations')
plt.show()

the ro argument passed to the plot function is a format string that specifies the format of the data points to be plotted. The format string consists of two characters:

r : specifies the color of the data points as red .

o : specifies the shape of the data points as circles .

You can use different format strings to specify different colors and shapes for the data points. For example, bx would specify blue crosses , g^ would specify green triangles , and so on.

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM