I have a 2D plot placed on one of the walls of a 3D plot that doesn't seem to reflect any changes from set_data(), I would like to understand what I'm doing wrong here.
Here is a sample code showing the 3D plot with the 2D 'projection' plot in question.
The output is shown here:
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
import matplotlib.pyplot as plt
plt.rcParams['legend.fontsize'] = 10
fig = plt.figure()
ax = fig.gca(projection='3d')
# Test data for projection onto xz plane
t = linspace(0,10, num=20)
z = np.sin(t)
# Plot projection
projx, = ax.plot(np.linspace(-1,0, num=len(z)), z, 'r', zdir='y', zs=1)
# Labels and scaling
ax.set_xlabel('M_x')
ax.set_ylabel('M_y')
ax.set_zlabel('M_z')
ax.set_xlim(-1, 1)
ax.set_ylim(-1, 1)
ax.set_zlim(-1, 1)
# Update projection data
projx.set_data([0],[0])
# See if actually updated data
print(projx.get_xdata())
# Draw and display window
plt.draw()
ax.legend()
plt.show()
I imagine that this line:
projx.set_data([0],[0])
would make the projection plot virtually empty. Instead, the sine wave remains.
Furthermore, the printout yields [0] as expected, so the set_data() call was successful, but for some reason the plot doesn't get drawn with the new data.
Shouldn't the set_data() changes be reflected when drawn afterwards?
There is a way to update a Line3D object by directly setting its vertices. Not sure, if this might have any negative side effects, though.
import numpy as np
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.gca(projection='3d')
# Test data for projection onto xz plane
t = np.linspace(0,10, num=20)
z = np.sin(t)
# Plot projections
projx, = ax.plot(np.linspace(-1,0, num=len(z)), z, 'r', zdir='y', zs=1, label="changed")
projy, = ax.plot(np.linspace(-1,0, num=len(z)), z, 'b', zdir='x', zs=-1, label="not changed")
# Labels and scaling
ax.set_xlabel('M_x')
ax.set_ylabel('M_y')
ax.set_zlabel('M_z')
ax.set_xlim(-1, 1)
ax.set_ylim(-1, 1)
ax.set_zlim(-1, 1)
#update vertices of one Line3D object
projx._verts3d = [0, 0.2, 0.7], [1, 1, 1], [0.5, 0.2, 0.7]
ax.legend()
plt.show()
Sample output:
However, since one cannot omit any of the x, y, and z arrays, there is no real advantage over plotting it as a 3D array with one array being a constant.
Related
I have three variables for my plot and I colour by the fourth variable. I have made a scatter plot via the following code, but I want a contour plot. My code:
import numpy as np
import matplotlib.pyplot as plt
a=np.linspace(4.0,14.0,3)
b=np.linspace(0.5,2.5,3)
c=np.linspace(0.0,1.0,3)
d=np.random.rand(len(a),len(b),len(c)) #colour by this variable
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
z,y,x=np.meshgrid(c,a,b)
img = ax.scatter(x, y, z, c=d, cmap='RdGy')
fig.colorbar(img, pad=0.2).set_label('colour')
ax.set_xlabel('c')
ax.set_ylabel('a')
ax.set_zlabel('b')
I want a filled contour instead of scatter plot. I know mayavi.mlab has this feature, but I cannot import mlab for some reason. Is there an alternative, or is there a better way of presenting this data?
Here is how I would present this 3-dimensional data. Each plot is a cross-section through the cube. This makes sense intuitively.
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(4.0, 14.0, 3)
y = np.linspace(0.5, 2.5, 3)
z = np.linspace(0.0, 1.0, 3)
data = np.random.rand(len(x), len(y), len(z))
fig, axes = plt.subplots(len(z), 1, figsize=(3.5, 9),
sharex=True,sharey=True)
for i, (ax, d) in enumerate(zip(axes, data.swapaxes(0, 2))):
ax.contour(x, y, d)
ax.set_ylabel('y')
ax.grid()
ax.set_title(f"z = {z[i]}")
axes[-1].set_xlabel('x')
plt.tight_layout()
plt.show()
My advice: 3D plots are rarely used for serious data visualization. While they look cool, it is virtually impossible to read any data points with any accuracy.
Same thing goes for colours. I recommend labelling the contours rather than using a colour map.
You can always use a filled contour plot to add colours as well.
I haven't found an answer to this yet: I have a grid defined in a text file with four columns: (lon,lat,depth,slip). Each row is a grid point.
I can generate a scatter plot of these points using the following simple code:
# Main imports:
import numpy as np
from pylab import *
from mpl_toolkits.mplot3d import Axes3D
# Read the grid:
points = np.loadtxt("grid.txt")
# Retrieve parameters from the grid:
lon = points[:,0]
lat = points[:,1]
depth = points[:,2]
slip = points[:,3]
# 3-D plot of the model:
fig = figure(1)
ax = fig.add_subplot(111, projection='3d')
p = ax.scatter(lon, lat, depth, c=slip, vmin=0, vmax=max(slip), s=30, edgecolor='none', marker='o')
fig.colorbar(p)
title("Published finite fault in 3-D")
ax.set_xlabel("Longitude [degrees]")
ax.set_ylabel("Latitude [degrees]")
ax.set_zlabel("Depth [km]")
ax.invert_zaxis()
jet()
grid()
show()
And I get the following figure:
What I want to do is to be able to interpolate those points to create a "continuous" surface grid and plot it in both 2-D and 3-D plots. Therefore, somehow I've to consider all (lon,lat,depth,slip) in the interpolation. I'd appreciate your suggestions. Thanks in advance!
I'm a bit late, but if your data grid is properly ordered, you could resolve your iusse using plot_surface reshaping your 1D data to 2D.
An example supposing you're using a 10x10 grid:
# Main imports:
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm
# Set the fourth dimension`
color_dimension = slip.reshape(10,10)
# normalize the colours
minn, maxx = color_dimension.min(), color_dimension.max()
norm = matplotlib.colors.Normalize(minn, maxx)
# color map
m = plt.cm.ScalarMappable(norm=norm, cmap='hot')
m.set_array([])
fcolors = m.to_rgba(color_dimension)
# plot
fig = plt.figure()
ax = fig.gca(projection='3d')
#reshape 1D data to 2D
g=ax.plot_surface(lat.reshape(10, 10), lon.reshape(10, 10), depth.reshape(10, 10), cmap='hot',rstride=1, cstride=1, facecolors=fcolors, vmin=minn, vmax=maxx, shade=False)
cbar=fig.colorbar(g,aspect=50)
cbar.set_label('slip', rotation=270, fontsize=14)
title("Published finite fault in 3-D")
ax.set_xlabel("Longitude [degrees]")
ax.set_ylabel("Latitude [degrees]")
ax.set_zlabel("Depth [km]")
ax.invert_zaxis()
plt.show()
I am trying to visualize differences between images in 3D, in order to more easily differentiate between positive and negative differences.
I have succeeded with a basic plot of an image, however, between the values matplotlib is interpolating values. I need these to be step changes between pixels.
I am often testing with very low-resolution images, for example, 16 by 16, so the interpolation has a large effect.
Numpy file of 16 by 16 image:
https://wetransfer.com/downloads/c916f76e0d86a61c00c2ed4cfe4ae97520190210192200/60d87c
One way to solve this would be to repeat the values however, this seems very inefficient and requires cleaning up the ticks after.
Code to generate above image:
import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
SubIm = np.load("Subtract_Image.npy")
def ImPlot2D3D(img, cmap=plt.cm.jet):
Z = img[::1, ::1]
fig = plt.figure(figsize=(14, 7))
# 2D Plot
ax1 = fig.add_subplot(1, 2, 1)
im = ax1.imshow(Z, cmap=cmap)
ax1.set_title('2D')
ax1.grid(False)
# 3D Plot
ax2 = fig.add_subplot(1, 2, 2, projection='3d')
X, Y = np.mgrid[:Z.shape[0], :Z.shape[1]]
ax2.plot_surface(X, Y, Z, cmap=cmap)
ax2.set_title('3D')
plt.show()
ImPlot2D3D(SubIm)
I've looked into 3D bar charts but they all use binning schemes and I can't make it work for an image.
Eventually managed to answer my own question.
A brute force method to solve this is to repeat the values in the array, hence making the interpolation between values that 'matplotlib' does, less impactful and better approximating a step change.
This can be achieved using numpy.repeat. As this is a 3D array we must iterate over one axis than the other. Otherwise, the array will be flattened repeated and this flat array returned.
Result:
def ImPlot2D3D(img, cmap=plt.cm.jet, step=False, ratio=10):
if step:
img = (img.repeat(ratio, axis=0)).repeat(ratio, axis=1)
Z = img[::1, ::1]
fig = plt.figure(figsize=(14, 7))
# 2D Plot
ax1 = fig.add_subplot(1, 2, 1)
im = ax1.imshow(Z, cmap=cmap)
ax1.set_title('2D')
ax1.grid(False)
# 3D Plot
ax2 = fig.add_subplot(1, 2, 2, projection='3d')
X, Y = np.mgrid[:Z.shape[0], :Z.shape[1]]
ax2.plot_surface(X, Y, Z, cmap=cmap)
ax2.set_title('3D')
# Scale the ticks back down to original values
if step:
ticks_x = ticker.FuncFormatter(lambda x, pos: '{0:g}'.format(x / ratio))
ticks_y = ticker.FuncFormatter(lambda y, pos: '{0:g}'.format(y / ratio))
ax1.xaxis.set_major_formatter(ticks_x)
ax1.yaxis.set_major_formatter(ticks_y)
ax2.xaxis.set_major_formatter(ticks_x)
ax2.yaxis.set_major_formatter(ticks_y)
plt.show()
import matplotlib.ticker as ticker
import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
SubIm = np.load("Subtract_Image.npy")
ImPlot2D3D(SubIm, step=True)
I search for the correct implementation for this a long time now.
I have a 3D delaunay triangulation and want to plot this in 2D.
In 3D i manage to do so:
I need a 2D plot though. What i get using matplotlib.tripcolor method or the matplotlib.collections.PolyCollection is:
How do i plot this in 2D without the top and back triangles all mixed up? With all methods tried so far, some triangles are hidden by triangles that should be in the back of the structure.
I see, that the methods just do not have the information necessary to plot in the correct order, since i have to provide 2D arrays already. The depth information is lost.
Does anybody know how to do this?
Thanks a lot!
You can mimic a 2D plot with Axes3d by setting an orthographic projection, initialising the view to face the desired plane, and removing unwanted plot elements along the axis orthogonal to the chosen plane of view. In addition, you can plot 2D elements using the zdir keyword argument.
Here's one of the matplotlib 3D plot examples I modified to demonstrate
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
from matplotlib import cm
import numpy as np
# Enable orthographic projection
# https://stackoverflow.com/questions/23840756/how-to-disable-perspective-in-mplot3d
from mpl_toolkits.mplot3d import proj3d
def orthogonal_proj(zfront, zback):
a = (zfront+zback)/(zfront-zback)
b = -2*(zfront*zback)/(zfront-zback)
return np.array([[1,0,0,0],
[0,1,0,0],
[0,0,a,b],
[0,0,-0.000001,zback]])
proj3d.persp_transformation = orthogonal_proj
fig = plt.figure()
ax = fig.gca(projection='3d')
# Init view to YZ plane
ax.view_init(azim=0, elev=0)
# Hide the X axis
ax.w_xaxis.line.set_lw(0.)
ax.set_xticks([])
# Change YZ plane colour to white
ax.w_xaxis.set_pane_color((1.0, 1.0, 1.0, 1.0))
# Make data.
X = np.arange(-5, 5, 0.25)
Y = np.arange(-5, 5, 0.25)
X, Y = np.meshgrid(X, Y)
R = np.sqrt(X**2 + Y**2)
Z = np.sin(R)
# Plot the surface.
surf = ax.plot_surface(X, Y, Z, cmap=cm.coolwarm,
linewidth=0, antialiased=False)
ax.set_zlim(-1.1, 1.1)
ax.set_ylabel('y')
ax.set_zlabel('z')
# Plot 2D elements with zdir argument
# https://stackoverflow.com/questions/29549905/pylab-3d-scatter-plots-with-2d-projections-of-plotted-data
stepsize = 0.1
t = np.arange(-4, 4+stepsize, step=stepsize)
ax.plot(t, 0.5*np.sin(t), 'k', zdir='x', linewidth=1.0)
ax.text(0, 0, 1, 'Text', zdir='y', ha='center', va='top')
plt.show()
I have this code which contains a 3D plot. I run the code in Spyder; I want to know if it is possible to make this plot a rotating one (360 degrees) and save it.
Thanks!
P.s. Sorry if it is a silly question, but I am a newby in Python.
import matplotlib.pyplot as plt
import numpy as np
from scipy import array
jet = plt.get_cmap('jet')
from matplotlib import animation
fig = plt.figure()
ax = fig.gca(projection='3d')
X = np.linspace(70,40,4)
Y = np.linspace(5,2,4)
X,Y= np.meshgrid(X, Y)
Z = array ([
[1223.539555, 1428.075086,1714.479425, 2144.053223],
[1567.26647,1829.056119,2990.416079,2745.320067],
[2135.163957,2491.534201, 2990.416079,3738.761638],
[3257.280827, 3800.655101, 4561.372117, 5702.458776],
])
surf = ax.plot_surface(X, Y, Z, rstride = 1, cstride = 1, cmap = jet,linewidth = 0,alpha= 1)
ax.set_zlim3d(0, Z.max())
fig.colorbar(surf, shrink=0.8, aspect=5)
ax.set_xlabel('Axial Length [mm]')
ax.set_ylabel('nbTurns')
ax.set_zlabel('RPM')
plt.show()
You need to define a function in order to get a specific animation. In your case it is a simple rotation:
def rotate(angle):
ax.view_init(azim=angle)
Then use the matplotlib animation:
rot_animation = animation.FuncAnimation(fig, rotate, frames=np.arange(0,362,2),interval=100)
This will call the rotate function with the frames argument as angles and with an interval of 100ms, so this will result in a rotation over 360° with a 2° step each 100ms. To save the animation as a gif file:
rot_animation.save('path/rotation.gif', dpi=80, writer='imagemagick')