0

I want to plot a line in 3D Space and color regions of high curvature. Right now I have a workaround using a discrete scatter plot:

import matplotlib as mpl
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.cm as cmx

mpl.rcParams['legend.fontsize'] = 10

data = np.loadtxt('data',usecols=range(0,4))
x = data[:,0]
y = data[:,1]
z = data[:,2]
cs = data[:,3]

colorsMap='jet'
cm = plt.get_cmap(colorsMap)
cNorm = mpl.colors.Normalize(vmin=min(cs), vmax=max(cs))
scalarMap = cmx.ScalarMappable(norm=cNorm, cmap=cm)

fig = plt.figure()
scalarMap.set_array(cs)
fig.colorbar(scalarMap)


ax = fig.gca(projection='3d')
ax.scatter(x, y, z, c=scalarMap.to_rgba(cs), label='scatter curve')
ax.legend()

plt.show()

But I would rather have a continuous line plot.Is there a way to do that?

dan-ros
  • 143
  • 3

1 Answers1

2

Depending on how many data points you have you might be able to get your way around this. For instance, consider the generated 3D spiral data below in substitution to your data.txt

import matplotlib as mpl
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.cm as cmx

mpl.rcParams['legend.fontsize'] = 10

theta = np.linspace(-4 * np.pi, 4 * np.pi, 1000)
z = np.linspace(-2, 2, 1000)
r = z**2 + 1
x = r * np.sin(theta)
y = r * np.cos(theta)
cs = 1/r

colorsMap='jet'
cm = plt.get_cmap(colorsMap)
cNorm = mpl.colors.Normalize(vmin=min(cs), vmax=max(cs))
scalarMap = cmx.ScalarMappable(norm=cNorm, cmap=cm)

fig = plt.figure()
scalarMap.set_array(cs)


ax = fig.gca(projection='3d')
ax.scatter(x, y, z, c=scalarMap.to_rgba(cs), marker='_', s=1)
plt.colorbar(scalarMap)

plt.show()

enter image description here

If the sampling frequency of your data points is not as "tight", then this won't look as nice. However, you could use this accepted answer to improve upon this.

Community
  • 1
  • 1
pysolver
  • 525
  • 6
  • 17
  • actually I wanted to get rid of the ax.scatter. My data points are in fact not tight enough for getting a continuous line by simply changing the marker to "_" – dan-ros Mar 29 '16 at 07:28
  • Then you should look at the linked answer at the bottom of my answer. There is no much more you can do if you really want a continuous line and there is no purpose on me replicating that here. Let me know if you have questions though – pysolver Mar 29 '16 at 13:45