So I am sampling from a 3D sphere and want to display it and despite the plt.axis('equal') command it still looks elliptic rather than spheric. Here is my code:
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
def sphere_sampler(dimension=2,sample_size=1):
phi_1=np.random.uniform(low=0,high=np.pi,size=sample_size)
phi_2=np.random.uniform(low=0,high=2*np.pi,size=sample_size)
sample=np.empty((sample_size,dimension))
sample[:,0]=np.cos(phi_1)
sample[:,1]=np.sin(phi_1)*np.cos(phi_2)
sample[:,2]=np.sin(phi_1)*np.sin(phi_2)
return sample
pre_sample=sphere_sampler(3,1000)
sample=pre_sample.reshape(pre_sample.shape[0],3)
fig=plt.figure()
ax = fig.gca(projection='3d')
ax.scatter(sample[:,0],sample[:,1],sample[:,2])
ax.set_xlim(-1,1)
ax.set_ylim(-1,1)
ax.set_zlim(-1,1)
plt.axis('equal')
plt.show()
Which part of it I am doing wrong? It looks like that something is wrong with display. How can I make the show() method to not to change the scale?
instead of plt.axis('equal'), use:
ax.set_aspect("equal")
Related
I made a 3D plot using the following code in python. Here three arrays x, y and z are used for the plot. I want to show the last point of the arrays (or the end point of the 3D line) in the plot. I used the approach I would use in 2d plotting, i.e., I asked for plotting only the last points of each array using this command ax.plot(x[-1],y[-1],z[-1],'o'). But it doesn't work.
import numpy as np
from matplotlib import pyplot as plt
from mpl_toolkits.mplot3d import Axes3D # noqa: F401 unused import
x=np.linspace(0,2*np.pi)
y=np.sin(x)
z=np.cos(x)
fig = plt.figure()
ax = fig.gca(projection='3d')
ax.plot(x, y, z, lw=1)
ax.plot(x[-1],y[-1],z[-1],'o') # This line doesn't work
plt.show()
import numpy as np
from matplotlib import pyplot as plt
from mpl_toolkits.mplot3d import Axes3D # noqa: F401 unused import
x=np.linspace(0,2*np.pi)
y=np.sin(x)
z=np.cos(x)
fig = plt.figure()
ax = fig.gca(projection='3d')
ax.plot(x, y, z, lw=1)
ax.scatter(x[-1],y[-1],z[-1],'-') # This should do the job
plt.show()
Add Color and Label
ax.scatter(x[-1],y[-1],z[-1],'-',c="yellow",label="End Point")
plt.legend()
plt.show()
Additional explanation on why you were having an error:
You were telling python to draw you a ax.plot for 1 point. Which is impossible, because you cant draw a line using 1 point only. Therefore, you tell it to draw a scatter.
I want a very simple plot:
import matplotlib.pyplot as plt
import numpy as np
for t in np.linspace(0,2*np.pi,100):
plt.plot(np.cos(t), np.sin(t), color='blue', linestyle='-', linewidth=7)
plt.show()
But nothing is appearing. I just get an empty plot. Where is my error?
Just plot the whole arrays:
import matplotlib.pyplot as plt
import numpy as np
t = np.linspace(0,2*np.pi,100):
plt.plot(np.cos(t), np.sin(t), color='blue', linestyle='-',linewidth=7)
plt.show()
Each call to plt.plot within the for loop is plotting a separate 'line' that consists on only a single point.
if you want the code to work you should plot points instead of lines.
for t in np.linspace(0,2*np.pi,100): plt.plot(np.cos(t), np.sin(t), 'k.')
I am using matplotlib to get a water fall figure, but the results look very strange. Anyone have any idea what could be wrong with it?
Here I attached the figures. The second one is the same data but in an ordinary plot. In the waterfall figure, why the color is not fully filled?
Here is the code:
def water_fall_1(x,y,Z):
#x=[...]
#y=[...]
#Z=[[z1],[z2],...z[ny]]
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.collections import PolyCollection
from matplotlib.colors import colorConverter
from mpl_toolkits.mplot3d import Axes3D
figs=[]
for jc in range(len(y)):
figs.append(list(zip(x,Z[jc])))
x=np.array(x)
y=np.array(y)
Z=np.array(Z)
xmin=np.floor(np.min((x.astype(np.float))))
xmax=np.ceil(np.max((x.astype(np.float))))
ymin=np.min((y.astype(np.float)))
ymax=np.max((y.astype(np.float)))
zmin=(np.min((Z.astype(np.float))))
zmax=np.max((Z.astype(np.float)))
fig=plt.figure()
ax = Axes3D(fig)
poly = PolyCollection(figs, facecolors=colorConverter.to_rgba("r", alpha=0.5))
ax.add_collection3d(poly, zs=y.astype(np.float), zdir='y')
ax.set_xlim(xmin,xmax)
ax.set_ylim(ymin,ymax)
ax.set_zlim(zmin,zmax)
ax.set_xlabel('$\omega$')
ax.set_ylabel('$T$')
#ax.set_zlabel('$\\frac{1}{2}$')
plt.show()
The curve is fully filled. I.e. the surface in between the points of the curve is red.
Consider the following example:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.collections import PolyCollection
from mpl_toolkits.mplot3d import Axes3D
bottom=-0.3
x = np.linspace(0,6, num=50)
z = np.sinc(x-4)
verts = zip(x,z)
#verts=verts + [(x.max(),bottom),(x.min(),bottom)]
fig=plt.figure()
ax = Axes3D(fig)
poly = PolyCollection([verts], facecolors="r", alpha=0.5)
ax.add_collection3d(poly, zs=1, zdir='y')
ax.set_xlim(x.min(),x.max())
ax.set_ylim(0,2)
ax.set_zlim(bottom,z.max())
plt.show()
which produces the following plot, where everything between the points of the curve is filled as expected.
If we now want to have the area between the curve and some bottom line filled, we would need to add some points,
verts=verts + [(x.max(),bottom),(x.min(),bottom)]
such that the bottom line is part of the curve and can thus be filled as well.
I'm using matplotlib to produce a 3d trisurf graph. I have everything working except that I would like to invert the y-axis, so that the origin is 0,0 not 0,100. I've looked through the matplotlib axes3d API and cannot figure out how to do this. Here is my code:
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
from matplotlib import cm
# my data, xs=xaxis, ys=yaxis, zs=zaxis
mortar_xs = []
cycles_ys = []
score_zs = []
#... populate my data for the 3 arrays: mortar_xs, cycles_ys, score_zs
# plot
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot_trisurf(mortar_xs,cycles_ys,score_zs,cmap=cm.coolwarm)
ax.set_zlim(bottom=0.0,top=1.0)
ax.legend()
ax.set_xlabel("# Mortar")
ax.set_ylabel("# Goals")
ax.set_zlabel("# Score")
plt.show()
My graph produced is the following, but I need the '# Goals' or the y-axis inverted, so that the origin is 0,0 not 0,100. If possible, I would like to do this without changing my data.
tmdavison's comment is what I was looking for:
ax.set_ylim(0,100)
Or
ax.set_ylim(100,0)
The simplest method would be to use ax.invert_yaxis()
Im making a density plot with matplotlib and I would also like to get rug plot under it. good example to make density plot is here How to create a density plot in matplotlib?
but I couldn't find any good example for rug plot. in R it can be done easly by rug(data).
You can plot markers at each datapoint.
from scipy import stats
import numpy as np
import matplotlib.pyplot as plt
sample = np.hstack((np.random.randn(30), np.random.randn(20)+5))
density = stats.kde.gaussian_kde(sample)
fig, ax = plt.subplots(figsize=(8,4))
x = np.arange(-6,12,0.1)
ax.plot(x, density(x))
ax.plot(sample, [0.01]*len(sample), '|', color='k')
You can find an example here!
ax = fig.add_subplot(111)
ax.plot(x1, np.zeros(x1.shape), 'b+', ms=20) # rug plot
x_eval = np.linspace(-10, 10, num=200)
ax.plot(x_eval, kde1(x_eval), 'k-', label="Scott's Rule")
ax.plot(x_eval, kde1(x_eval), 'r-', label="Silverman's Rule")
Seems to be the core of it!
You can also use Seaborn.distplot, which wraps histogram, KDE and rugs altogether. Figures made by Seaborn are also prettier by default.
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
sample = np.hstack((np.random.randn(30), np.random.randn(20)+5))
fig, ax = plt.subplots(figsize=(8,4))
sns.distplot(sample, rug=True, hist=False, rug_kws={"color": "g"},
kde_kws={"color": "k", "lw": 3})
plt.show()
Here's the answer for people just looking for a rugplot to use on a matplotlib axis: you can use a seaborn function.
import seaborn as sns
sns.rugplot(xdata, height=0.025, axis=ax, color='k')
This looks much nicer than a pure-matplotlib kludge because the rug is aligned to (flush with) the x-axis.