5

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.

2

3 Answers 3

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

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

Comments

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.

Comments

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

2 Comments

Unfortunately these are for 2D scatter plots.
Sorry about that, I presumed it works in same way. Seems it's a bit more tricky. Have you seen this question? stackoverflow.com/questions/15053575/…

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.