0

I am trying to understand how unpacking could work with matplotlib in regards to condensing and simplifying code. Say:

P = array([[0, 0, 0], [1, 1, 1], [2, 2, 2], [3, 3, 3],[4, 4, 4]])

and I wanted to plot four points at each of these coordinates ((0,0,0), (1,1,1), etc). I have seen * be used before with regard to unpacking and then plotting data points but I don't know what I'm doing wrong. I have the following code:

import numpy as np
import matplotlib.pyplot as plt
import mpl_toolkits.mplot3d.axes3d as p3
import matplotlib.animation as animation
import matplotlib as mpl

fig = plt.figure()

ax = p3.Axes3D(fig)
ax.set_xlim3d([0, 5])
ax.set_xlabel('X')

ax.set_ylim3d([0, 5])
ax.set_ylabel('Y')

ax.set_zlim3d([0, 5])
ax.set_zlabel('Z')
ax.set_title('3D Test')

P = array([[0, 0, 0], [1, 1, 1], [2, 2, 2], [3, 3, 3],[4, 4, 4]])

[ax.plot(*p, marker='o', ls='None')[0] for i in range(4)]

I'd thought that p could be unpacked into the coordinates and then plotted but it's not working at the moment. Any ideas on where I am going wrong?

At the moment this is being produced

enter image description here

1 Answer 1

1

I'm not sure what you intend by "I wanted to plot four points at each of these coordinates" (what's the point of 4 overlapping points?), but...


Let's transpose first:

ax.plot(*P.T, marker='o', ls='None')

Output:

enter image description here

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

2 Comments

So why does transposing work? Is it because the way the unpacking works is such that you don't enter the coordinates of the individual point but instead the unpacking provides all the x,y and z values?
Because you need xs, ys, zs (which is what I'm guessing you meant).

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.