I have already posted an example similar to the following one regarding another issue here:
Displaying Contours in front of Surface in matplotlib
I am posting it again regarding a different question:
from mpl_toolkits.mplot3d import axes3d
import matplotlib.pyplot as plt
from matplotlib import cm
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
fig = plt.figure()
ax = fig.gca(projection='3d')
X = np.array([[200,800,1500,2000,3000],[200,700,1500,2000,3000],[200,800,1500,2000,3000],[200,800,1500,2000,3000]])
Y = np.array([[50,50,50,50,50],[350,350,350,350,350],[500,500,500,500,500],[1000,1000,1000,1000,1000]])
Z = np.array([[0,0,33,64,71],[44,62,69,74,76],[59,67,72,75,77],[63,68,73,76,77]])
ax.plot_surface(X, Y, Z, rstride=1, cstride=1, alpha=0.5)
cset = ax.contour(X, Y, Z, zdir='x', offset=200, cmap=cm.coolwarm)
levels = [500,700,1000,2000,3000]
ax.set_xticks(levels)
ax.set_xlabel('X')
ax.set_xlim(200, 3000)
ax.set_ylabel('Y')
ax.set_ylim(0, 1000)
ax.set_zlabel('Z')
ax.set_zlim(0, 100)
plt.show()
Is it possible to have my contours plot a number of contours equal to the adjacent axis values:
e.g. 3D_Surface_from_code_above For the contours in my Y-Z plane to the left of the picture. Instead of having 7 contours displaying (which I'm not sure what X value they correspond too) would it be possible to have one for each X tick value ? i.e. 500, 700, 1000, 2000, 3000.
I hope that make sense, it would allow the viewer to follow the contour corresponding to X = 700 and see how Z varies with respect to Y for this fixed value of X. This would allow me to set the contours for values of X which are of particular interest to me.
Thank you for your help.
You can set the levels directly as an argument of the contours function as
levels = [500,700,1000,2000,3000]
cset = ax.contour(X, Y, Z, levels, zdir='x', offset=200, cmap=cm.coolwarm)
Related
Is there a way to create a bar chart using matplotlib such that the bars are located at a specific x,y and at a specific angle? In the screenshot below, I just drew thick lines (to represent thin bars) in PowerPoint on top of the scatterplot.
It doesn't have to be a barchart necessarily, I just don't know the name of a plot that is like this. I thought about trying to mimic this with a quiver plot but wasn't sure how. Reason for wanting this is densely spaced points that have variable values (not monotonically increasing like in this example), and just coloring the scatter plot isn't visually elucidating trends of interest, even with different colormaps.
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(25)
y = -x
z = x
plt.scatter(x, y, c=z, cmap='viridis')
I don't know of a canned way to do this, but you could, in a pinch, create your own function that draws rectangles to create this plot. For example:
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.patches import Rectangle
x = np.arange(25)
y = -x
z = x
plt.scatter(x, y, c=z, cmap='viridis')
def slanted_bars(x, y, z, angle, ax):
for xi, yi, zi in zip(x, y, z):
ax.add_patch(Rectangle((xi, yi), 1, zi, angle))
fig, ax = plt.subplots(1, 1)
ax.scatter(x, y, c=z, cmap='viridis')
slanted_bars(x, y, z, -45, ax)
You'd have to play with the color and shape of the rectangles to get something appealing, but it can do what you want.
I have been trying to get the "example" picture (generated with a 3D graphic calc) using Python for a few days now, but keep running into troubles getting the segments of the plot other than the peak in the middle to show up to scale.
I am using this code:
import numpy as np
from mpl_toolkits.mplot3d import axes3d
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111, projection="3d")
X, Y = np.mgrid[-1:1:30j, -1:1:30j]
Z = (2*X*Y) + (1/np.sqrt(X**2+Y**2))
ax.plot_surface(X, Y, Z, cmap="autumn_r", lw=0.5, rstride=1, cstride=1)
ax.contour(X, Y, Z, 10, lw=3, cmap="autumn_r", linestyles="solid", offset=-1)
ax.contour(X, Y, Z, 10, lw=3, colors="k", linestyles="solid")
plt.show()
Which produces this graph.
It is close, but it should look more like this one. When I lower the 30j in attempts to bring it down and hope the flares on the sides are more pronounced, it gets rid of the entire peak. I am trying to get to this.
What if you try the following line?
X, Y = np.mgrid[-7:7:100j, -7:7:100j]
I have four dimensional data (x, y, z displacements; and respective voltages) which I wish to plot in a 3d scatterplot in python. I've gotten the 3d plot to render, but I want to have the colour of the points change using a colourmap, dependent upon the magnitude of the point's voltage.
I've tried a few things, but can't seem to get it to work I'm getting the error ValueError: Cannot convert argument type <type 'numpy.ndarray'> to rgba array. I'm not sure exactly how to convert what I need to convert, so if anybody could please offer some help, I'd be most appreciative.
My code is here:
fig = plt.figure()
from mpl_toolkits.mplot3d import Axes3D
cmhot = plt.cm.get_cmap("hot")
ax = fig.add_subplot(111, projection='3d',)
ax.scatter(x, y, z, v, s=50, c = cmhot)
plt.show()
ax.scatter can take a color parameter c which is a sequence (e.g. a list or an array) of scalars, and a cmap parameter to specify a color map. So to make the colors vary according to the magnitude of the voltages, you could define:
c = np.abs(v)
This makes positive and negative voltages have the same color. If instead you wished each color (positive or negative) to have its own color, you could just use c = v.
For example,
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
x, y, z, v = (np.random.random((4,100))-0.5)*15
c = np.abs(v)
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
cmhot = plt.get_cmap("hot")
cax = ax.scatter(x, y, z, v, s=50, c=c, cmap=cmhot)
plt.show()
I am trying to make a 3D plot from x, y, z points list, and I want to plot color depending on the values of a fourth variable rho.
Currently I have ;
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot3D(cell_x, cell_y, cell_z, linestyle='None', marker='o', markersize = 5, antialiased=True)
ax.set_xlim3d(0.45, 0.55)
ax.set_ylim3d(0.45, 0.55)
ax.set_zlim3d(0.45, 0.55)
How to add cell_rho (my fourth array) as the color of my x, y, z points ? (for example for a jet colormap).
Thank you very much.
EDIT : I can't use scatter plots because for my 18000 points scatter plots are very slow compared to plot3d with markers only.
If you want to display a simple 3D scatterplot, can't you just use scatter?
E.g.,
x, y, z = randn(100), randn(100), randn(100)
fig = plt.figure()
from mpl_toolkits.mplot3d import Axes3D
ax = fig.add_subplot(111, projection='3d')
ax.scatter(x, y, z, c=randn(100))
plt.show()
(I'm running the above code under python -pylab.)
It seems, on the contrary, that with plot3D you must convert your fourth dimension to RGB tuples.
I am trying to plot a surface using matplotlib using the code below:
from matplotlib import cm
import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d import axes3d, Axes3D
import pylab as p
vima=0.5
fig = plt.figure()
ax = fig.gca(projection='3d')
X = np.arange(0, 16.67, vima)
Y = np.arange(0, 12.5, vima)
X, Y = np.meshgrid(X, Y)
Z = np.sqrt(((1.2*Y+0.6*X)**2+(0.2*Y+1.6*X)**2)/(0.64*Y**2+0.36*X**2))
surf = ax.plot_surface(X, Y, Z,rstride=1, cstride=1, alpha=1,cmap=cm.jet, linewidth=0)
fig.colorbar(surf, shrink=0.5, aspect=5)
plt.show()
If you run it you will see a blue surface, but I want to use the whole color range of jet... I know there is a class "matplotlib.colors.Normalize", but I don't know how to use it. Could you please add the necessary code in order to do it?
I realise that the poster's issue has already been resolved, but the question of normalizing the colors was never dealt with. Since I've figured out how I thought I'd just drop this here for anyone else who might need it.
First you create a norm and pass that to the plotting function, I've tried to add this to the OP's code.
from matplotlib import cm
import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d import axes3d, Axes3D
import pylab as p
import matplotlib
vima=0.5
fig = plt.figure()
ax = fig.gca(projection='3d')
X = np.arange(0, 16.67, vima)
Y = np.arange(0, 12.5, vima)
X, Y = np.meshgrid(X, Y)
Z = np.sqrt(((1.2*Y+0.6*X)**2+(0.2*Y+1.6*X)**2)/(0.64*Y**2+0.36*X**2))
Z = np.nan_to_num(Z)
# Make the norm
norm = matplotlib.colors.Normalize(vmin = np.min(Z), vmax = np.max(Z), clip = False)
# Plot with the norm
surf = ax.plot_surface(X, Y, Z,rstride=1, cstride=1, norm=norm, alpha=1,cmap=cm.jet, linewidth=0)
fig.colorbar(surf, shrink=0.5, aspect=5)
plt.show()
The norm works the same way for the "imshow" command.
As JoshAdel noted in a comment (credit belongs to him), it appears that the surface plot is improperly ranging the colormap when a NaN is in the Z array. A simple work-around is to simply convert the NaN's to zero or very large or very small numbers so that the colormap can be normalized to the z-axis range.
from matplotlib import cm
import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d import axes3d, Axes3D
import pylab as p
vima=0.5
fig = plt.figure()
ax = fig.gca(projection='3d')
X = np.arange(0, 16.67, vima)
Y = np.arange(0, 12.5, vima)
X, Y = np.meshgrid(X, Y)
Z = np.sqrt(((1.2*Y+0.6*X)**2+(0.2*Y+1.6*X)**2)/(0.64*Y**2+0.36*X**2))
Z = np.nan_to_num(Z) # added this line
surf = ax.plot_surface(X, Y, Z,rstride=1, cstride=1, alpha=1,cmap=cm.jet, linewidth=0)
fig.colorbar(surf, shrink=0.5, aspect=5)
plt.show()
Replying to an old question, I know, but the answers posted were at least in my case somewhat unsatisfactory. For those still stumbling here, I give a solution that worked for me.
Firstly, I did not want use zeros to replace NaNs, as for me they represent points with missing or undefined data. I'd rather not have anything plotted at these points. Secondly, the whole z range of my data was way above zero, so dotting the plot with zeros would result in an ugly and badly scaled plot.
Solution given by leifdenby was quite close, so +1 for that (though as pointed out, the explicit normalisation does not add to the earlier solution). I just dropped the NaN-to-zero replacement, and used the functions nanmin and nanmax instead of min and max in the color scale normalisation. These functions give the min and max of an array but simply ignore all NaNs. The code now reads:
# Added colors to the matplotlib import list
from matplotlib import cm, colors
import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d import axes3d, Axes3D
import pylab as p
vima=0.5
fig = plt.figure()
ax = fig.gca(projection='3d')
X = np.arange(0, 16.67, vima)
Y = np.arange(0, 12.5, vima)
X, Y = np.meshgrid(X, Y)
Z = np.sqrt(((1.2*Y+0.6*X)**2+(0.2*Y+1.6*X)**2)/(0.64*Y**2+0.36*X**2))
# MAIN IDEA: Added normalisation using nanmin and nanmax functions
norm = colors.Normalize(vmin = np.nanmin(Z),
vmax = np.nanmax(Z))
# Added the norm=norm parameter
surf = ax.plot_surface(X, Y, Z,rstride=1, cstride=1, alpha=1, norm=norm, cmap=cm.jet, linewidth=0)
fig.colorbar(surf, shrink=0.5, aspect=5)
plt.show()
Running this, I get a correctly scaled plot, with the (0, 0) datapoint missing. This is also the behaviour that I find most preferable, as the limit (x, y) to (0, 0) does not seem to exist for the function in question.
This has been my first contribution to StackOverflow, I hope it was a good one (wink).