0

Currently I am trying to get a glimpse of how a certain value is depending from different input parameters. Thus I created a function in python with this parameters as input. Now I'd like to plot the result. I already managed to get a 3D plot, meaning 2 variables can be taken into consideration at once. I hope there are ways to extend this by using colors etc.

Sample code:

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

def calculation(v,w,x,y,z):
  return v+w+x+y+z

#Part for 3D plotting so far:
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

x=np.arange(1, 10, 1)
y=np.arange(1, 10, 1)

X, Y = np.meshgrid(x, y)
zs = np.array([calculation(1, 1, x, y, 1) for x,y in zip(np.ravel(X), np.ravel(Y))])
Z = zs.reshape(X.shape)

ax.plot_surface(X, Y, Z)

plt.show()

Edit: With scatter I have come a little further now, so I can display influence of three different parameters now:

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

x=np.arange(1, 10, 1)
y=np.arange(1, 10, 1)
z=np.arange(1, 10, 1)

X, Y, Z = np.meshgrid(x,y,z)

zs = np.array([calculation(1,1,x,y,z) for x,y,z in zip(np.ravel(X), np.ravel(Y), np.ravel(Z))])
C = zs.reshape(X.shape)

surf = ax.scatter(X, Y, Z, c=C, linewidth=0, antialiased=False, norm=MidpointNormalize(midpoint=0.), cmap=cm.seismic)

fig.colorbar(surf)

plt.show()
clel
  • 175
  • 1
  • 3
  • 8
  • Yes and there are even examples on how to use colors on the matplotlib page as well as in various answers on Stackoverflow. What exactly would you like to know in this question? – ImportanceOfBeingErnest Mar 14 '18 at 19:37
  • I not only want to use colors, but also more dimensions if possible. When there are already answers, could you link a good one? – clel Mar 14 '18 at 20:15
  • https://stackoverflow.com/questions/6539944/color-matplotlib-plot-surface-command-with-surface-gradient – ImportanceOfBeingErnest Mar 14 '18 at 20:21
  • @ImportanceOfBeingErnest Sorry, this is _not_ what I wanted. I already use this kind of colormap, but it only displays the z value, which is already shown by the surface shape. I want to use it to display an additional dimension. Also that does not answer how to calculate that dimension, apart from the fact that I want to integrate 5 dimensions. Please read the question again, if it is still not clear, I can try to specify more. – clel Mar 15 '18 at 11:34
  • No, the linked answer does not show the z value as colors; it shows the gradient in colorcoding. You may choose anything else instead. – ImportanceOfBeingErnest Mar 15 '18 at 11:43
  • Ah, sorry, mixed that up. My problem was a bit different, though, since I want to have more parameters displayed than in the example, so I am also searching for possible concepts to realize it like animations etc. Also I need to have one more parameter on z (instead of the output of the function and plotting the output of the function as color. Unfortunately I don't know how to do that. I guess I will have to use https://matplotlib.org/mpl_toolkits/mplot3d/tutorial.html#scatter-plots – clel Mar 15 '18 at 15:55
  • Sorry, I do not understand what "output on that color" and "parameter on z" mean. – ImportanceOfBeingErnest Mar 15 '18 at 15:58
  • Currently the output from "calculation" is displayed on the z axis. What I want is to use z to display another input parameter and display the output value of the function as a colormap. – clel Mar 15 '18 at 16:32
  • The third argument to `plot_surface` is the z values. Use this to supply your `other input parameter`. Use the `facecolors` argument to supply your `z`. – ImportanceOfBeingErnest Mar 15 '18 at 16:41
  • I think that a plot_surface is not capable of this, as one has to see through 3d data points. See the update of the question with my current solution. Still 2 more dimensions to add, so ideas are welcome. – clel Mar 15 '18 at 16:48
  • Ok, I'm clearly missing the point of the complete question. Good luck with your project. – ImportanceOfBeingErnest Mar 15 '18 at 16:51
  • The point is to show the influence of the parameters on the outcome. – clel Mar 15 '18 at 17:07
  • Possible duplicate of [Color matplotlib plot\_surface command with surface gradient](https://stackoverflow.com/questions/6539944/color-matplotlib-plot-surface-command-with-surface-gradient) – georgeawg Aug 07 '18 at 15:32

0 Answers0