I was looking for a way to change the color of a 3D plots gridlines and was unable to find a clean and easy way to do so. The only answer I found was here and it came off as a bit complex. I was wondering if there was an easier way to color the gridlines of a 3D plot in matplotlib using mpl_toolkits.mplot3d
simple example found here
import matplotlib as mpl
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
import matplotlib.pyplot as plt
mpl.rcParams['legend.fontsize'] = 10
fig = plt.figure()
ax = fig.gca(projection='3d')
theta = np.linspace(-4 * np.pi, 4 * np.pi, 100)
z = np.linspace(-2, 2, 100)
r = z**2 + 1
x = r * np.sin(theta)
y = r * np.cos(theta)
ax.plot(x, y, z, label='parametric curve')
ax.legend()
plt.show()
The code from the question you found is so complex because it colorizes single lines of the grid.
If the aim is to colorize all gridlines simulatneously, you may just use
plt.rcParams['grid.color'] = "deeppink"
Related
Is there a way to move tick labels in Matplot3dlib like this?
from mpl_toolkits import mplot3d
import numpy as np
import matplotlib.pyplot as plt
x = np.outer(np.linspace(-2, 2, 30), np.ones(30))
y = x.copy().T # transpose
z = np.cos(x ** 2 + y ** 2)
fig = plt.figure()
ax = plt.axes(projection='3d')
ax.plot_surface(x, y, z,cmap='viridis', edgecolor='none')
ax.set_title('Surface plot')
plt.show()
There are some ways using pad parameters.
However, I want to move more precisely like figure in the link above.
Any help appreciated.
-- Addition --
When I changing PAD parameter like the code below, the tick's label is more closer to the axis. However, I want to move it a little bit more to -x direction.
tick's label position changing
from mpl_toolkits import mplot3d
import numpy as np
import matplotlib.pyplot as plt
x = np.outer(np.linspace(-2, 2, 30), np.ones(30))
y = x.copy().T # transpose
z = np.cos(x ** 2 + y ** 2)
fig = plt.figure()
ax = plt.axes(projection='3d')
ax.plot_surface(x, y, z,cmap='viridis', edgecolor='none')
ax.set_title('Surface plot')
ax.tick_params(axis='x', which='major', pad=-5)
plt.show()
I have a data set which maps a tuple of phi and theta to
a value which represents the strength of the signal.
I want to plot these on a sphere. I simply followed
a demo from matplotlib and adjusted the code to my
use case.
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
u = phi
v = theta
vals =vals/vals.max()
Map = cm.coolwarm
facecolors = Map(vals[:])
x = 10 * np.outer(np.cos(u), np.sin(v))
y = 10 * np.outer(np.sin(u), np.sin(v))
z = 10 * np.outer(np.ones(np.size(u)), np.cos(v))
ax.plot_surface(x, y, z, rstride=1, cstride=1, cmap=cm.coolwarm,
linewidth=0, antialiased=False, facecolors=facecolors)
plt.show()
This generates an error message IndexError: index 4 is out of bounds for axis 0 with size 4. I also looked into the source code, which seems
to indicate to me that facecolors isn't formatted correctly, but I'm
struggling to figure out, what formatting is needed exactly.
Any help or other ways to achieve this goal would be greatly
appreciated.
Greetings
If your question is: "How to get rid of this IndexError?", I modified your code and now it works. plot_surface takes X,Y,Z and facecolors as 2D arrays of corresponding values on a 2D grid. Facecolors in your case weren't and this was the source of your error.
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm, colors
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
u, v = np.mgrid[0:np.pi:50j, 0:2*np.pi:50j]
strength = u
norm=colors.Normalize(vmin = np.min(strength),
vmax = np.max(strength), clip = False)
x = 10 * np.sin(u) * np.cos(v)
y = 10 * np.sin(u) * np.sin(v)
z = 10 * np.cos(u)
ax.plot_surface(x, y, z, rstride=1, cstride=1, cmap=cm.coolwarm,
linewidth=0, antialiased=False,
facecolors=cm.coolwarm(norm(strength)))
plt.show()
Result is this image of a sphere.
However, if your data is not on a 2D grid you are in trouble. Additionally if your grid is not regular the sphere you plot will look irregular as well. So if your question is: "How to plot a heatmap on a sphere?", there is already such a question and solution here using Basemap package produces this result:
I want to plot a line in 3D Space and color regions of high curvature. Right now I have a workaround using a discrete scatter plot:
import matplotlib as mpl
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.cm as cmx
mpl.rcParams['legend.fontsize'] = 10
data = np.loadtxt('data',usecols=range(0,4))
x = data[:,0]
y = data[:,1]
z = data[:,2]
cs = data[:,3]
colorsMap='jet'
cm = plt.get_cmap(colorsMap)
cNorm = mpl.colors.Normalize(vmin=min(cs), vmax=max(cs))
scalarMap = cmx.ScalarMappable(norm=cNorm, cmap=cm)
fig = plt.figure()
scalarMap.set_array(cs)
fig.colorbar(scalarMap)
ax = fig.gca(projection='3d')
ax.scatter(x, y, z, c=scalarMap.to_rgba(cs), label='scatter curve')
ax.legend()
plt.show()
But I would rather have a continuous line plot.Is there a way to do that?
Depending on how many data points you have you might be able to get your way around this. For instance, consider the generated 3D spiral data below in substitution to your data.txt
import matplotlib as mpl
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.cm as cmx
mpl.rcParams['legend.fontsize'] = 10
theta = np.linspace(-4 * np.pi, 4 * np.pi, 1000)
z = np.linspace(-2, 2, 1000)
r = z**2 + 1
x = r * np.sin(theta)
y = r * np.cos(theta)
cs = 1/r
colorsMap='jet'
cm = plt.get_cmap(colorsMap)
cNorm = mpl.colors.Normalize(vmin=min(cs), vmax=max(cs))
scalarMap = cmx.ScalarMappable(norm=cNorm, cmap=cm)
fig = plt.figure()
scalarMap.set_array(cs)
ax = fig.gca(projection='3d')
ax.scatter(x, y, z, c=scalarMap.to_rgba(cs), marker='_', s=1)
plt.colorbar(scalarMap)
plt.show()
If the sampling frequency of your data points is not as "tight", then this won't look as nice. However, you could use this accepted answer to improve upon this.
I am plotting the deformation of a 3D truss using matplotlib. Now, what I'm trying to do is show two views (side-by-side) of the exact same axes3D on the same figure, in this case one in isometric view and a side view. Can I do this with a minimal amount of copy-pasting?
I'm not including code as any example from the matplotlib webpage would suffice.
I don't think you can create a copy of the axes and just change its view angle. I think the simplest way would be to create a function, to which you pass the position of the subplot and the viewing angle.
Here's an example, based on the lines3d_demo example from the mpl website
import matplotlib as mpl
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
import matplotlib.pyplot as plt
mpl.rcParams['legend.fontsize'] = 10
fig = plt.figure(figsize=(9,4))
def makeplot(position,angle):
ax = fig.add_subplot(position,projection='3d')
ax.plot(x, y, z, label='parametric curve')
ax.view_init(30, angle)
ax.legend()
return ax
theta = np.linspace(-4 * np.pi, 4 * np.pi, 100)
z = np.linspace(-2, 2, 100)
r = z**2 + 1
x = r * np.sin(theta)
y = r * np.cos(theta)
ax1 = makeplot(121,30)
ax2 = makeplot(122,60)
plt.show()
I am plotting a 3d plot in python 2.7
When I try to plot a 3d plot with color and marker as in 2D plot() function. I come across an error.
So I tried to plot line separately and measured points with markers separately using scatter() function.
When I create legend entries my legend looks like this
But I don't want to have duplicate legend entries instead
I want my legend entries to group with colour, or
Is it possible have both marker and line as a single entry so that there are only 5 entries in my legend
I found a similar question to this (How to make custom legend in matplotlib) but it does not solve my problem
I am appending a code similar to my problem
import matplotlib as mpl
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
import matplotlib.pyplot as plt
mpl.rcParams['legend.fontsize'] = 10
fig = plt.figure()
ax = fig.gca(projection='3d')
theta = np.linspace(-4 * np.pi, 4 * np.pi, 100)
z = np.linspace(-2, 2, 100)
r = z**2 + 1
x = r * np.sin(theta)
y = r * np.cos(theta)
ax.plot(x, y, z, label='parametric curve 1')
ax.scatter(x, y, z, label='parametric curve 1',marker = 'o')
x = r * np.sin(theta + 1)
y = r * np.cos(theta + 1)
ax.plot(x, y, z, label='parametric curve 2')
ax.scatter(x, y, z, label='parametric curve 2',marker = 'o')
ax.legend()
plt.show()
The above code gives me a plot shown below
Plot
But I want my legend to have only two entries
Are you using the standard Matplotlib library to generate these 3D plots? If so, starting from the example in the documentation (http://matplotlib.org/mpl_toolkits/mplot3d/tutorial.html#line-plots) it seems to work fine:
import matplotlib as mpl
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
import matplotlib.pyplot as plt
mpl.rcParams['legend.fontsize'] = 10
fig = plt.figure()
ax = fig.gca(projection='3d')
theta = np.linspace(-4 * np.pi, 4 * np.pi, 100)
z = np.linspace(-2, 2, 100)
r = z**2 + 1
x = r * np.sin(theta)
y = r * np.cos(theta)
ax.plot(x, y, z, label='parametric curve 1', marker='o')
x = r * np.sin(theta + 1)
y = r * np.cos(theta + 1)
ax.plot(x, y, z, label='parametric curve 2', marker='o')
ax.legend()
plt.show()