4

I am trying to produce a 3D plot with two colors. One color for values above zero and another color for values below zero. I just cannot figure out how to implement this in the code.

Here is my current code:

from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm
import matplotlib.pyplot as plt
import matplotlib.tri as mtri
import pandas as pd
import numpy as np

df = pd.DataFrame.from_csv('...temp\\3ddata.csv', sep = ',', index_col  = False)

fig = plt.figure()
ax = fig.gca(projection='3d')

x = df['P']
y = df['T']
z = df['S']

ax.plot(x, y, zs= 0, zdir = 'z', c= 'r')

ax.scatter(x,y, z, c = 'b', s = 20)

ax.set_xlabel('Pressure (Bar)')
ax.set_zlabel('Residual subcooling (J*Kg-1*K-1')
ax.set_ylabel('Temperaure of LNG (K)')

plt.show()

3DPLOT

I've read a few examples but couldn't apply them to my data. Is it possible to do this with a for loop?

or something along the lines of:

use_c = {<=0: "red", >=0 "blue"}

#ax.plot(x, y, zs= 0, zdir = 'z', c= 'r')

ax.scatter(x,y, z, c = [use_c[x[0]] for x in z], s = 20)

Something that links the value of z to a color.

Joey
  • 914
  • 4
  • 16
  • 37

3 Answers3

5

I have figured it out alas.

I needed to specify a variable for c then reference this in the ax.scatter(*kwargs):

c = (z<=0)

ax.scatter(x,y,z, c = c, cmap = 'coolwarm', size = 30)

this gives me a nice plot as follows: enter image description here

Joey
  • 914
  • 4
  • 16
  • 37
4

if you pass to scatter a boolean array as the value of the optional argument c, you have your points in two colors from the extremes of the current colormap

ax.scatter(x,y, z, c = z<0, s = 20)

You may want to set the edge width to zero, otherwise all the dots look almost black.

gboffi
  • 22,939
  • 8
  • 54
  • 85
1

Here is some example code which shows how to apply color map with a scatter plot.

import matplotlib.cm as cm
plt.scatter(x, y, c=t, cmap=cm.colormap_name)

Where t is some value you want to set the color based on.

You can find a full list of colormaps here:

http://matplotlib.org/examples/color/colormaps_reference.html

Taken from:

Scatter plot and Color mapping in Python

Community
  • 1
  • 1
bamdan
  • 836
  • 7
  • 21
  • Unfortunately these are for 2D scatter plots. – Joey Apr 04 '16 at 14:55
  • Sorry about that, I presumed it works in same way. Seems it's a bit more tricky. Have you seen this question? http://stackoverflow.com/questions/15053575/python-3d-scatterplot-colormap-issue – bamdan Apr 04 '16 at 15:17