Issue
I'm having trouble with a 3D matplotlib plot showing subsequent 2D line profiles with time. The plot ends up exactly how I want it to look except that the 2D profiles are overlapping each other in the wrong order, which confuses anyone who views the plot. The profiles are being plotted correctly along the time axis, but it's almost like they are added to the plot in the opposite order. I think has something to do with the "zorder" term in matplotlib, but I've been unsuccessful in fixing this problem. Anyone have any suggestions?
from mpl_toolkits.mplot3d import Axes3D
from matplotlib.lines import Line2D
from matplotlib.collections import LineCollection
import matplotlib.pyplot as plt
profiles = []
times = []
index = 0
for item in intensities:
integrated_profile = integrate(item)
profiles.append(list(zip(x_coordinates, integrated_profile)))
timestamps = int(index+1)
times.append(timestamps)
index += 1
profiles = np.array(profiles)
times = np.array(times)
profile_collection = LineCollection(profiles, colors=color_list) #color_list not shown here
fig = plt.figure(dpi=100, figsize=(18,6))
ax = fig.add_subplot(111, projection='3d',proj_type='ortho')
ax.add_collection3d(profile_collection, zs=times, zdir='y')
# I deleted a lot more lines of code which are just formatting the plot
Solution
I found a workaround. This is actually an easier way to create this type of plot. Instead of storing the 2D profiles in a collection, I just looped through and added them individually to the 3D plot. A collection has on fixed zorder value, so you cannot change what I was wanting to change. When you loop through and plot each 2D profile individually, you can set the zorder parameter according to how you want it. Here's what I did:
profiles = []
times = []
index = 0
for item in intensities:
integrated_profile = integrate(item)
profiles.append(integrated_profile)
timestamps = int(index+1)
times.append(timestamps)
index += 1
profiles = np.array(profiles)
times = np.array(times)
fig = plt.figure(dpi=100, figsize=(18,6))
ax = fig.add_subplot(111, projection='3d',proj_type='ortho')
index2 = 0
for i in profiles:
ax.plot(x_coordinates, profiles[index2], zs=times[index2], zdir='y',
zorder=(len(profiles) - index2), color=next(colors))
index2 += 1
Answered By - jaroh24
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.