matplotlib/mplot3d scatterplot respects masking but surfaceplot does not - why? - python

The source of this post is from an earlier post.
I have X,Y,Z 2D arrays. I only want to see the plot in the first-quadrant where all x, y and z are positive. I use masking on all negative z entries. However it was noticed that scatter plots indeed respect the masking and do not show the negative z-values. However surface-plots still display the negative values.
Please find below the code -
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits import mplot3d
from matplotlib import cm
%matplotlib notebook
x = np.linspace(0,1.5,10)
y = np.linspace(0,1.5,10)
X,Y = np.meshgrid(x,y)
Z = 1-X-Y
Z1 = np.ma.masked_less(Z,0)
X1 = np.ma.masked_array(X,Z1.mask)
Y1 = np.ma.masked_array(Y,Z1.mask)
fig = plt.figure()
# Two plots - one scatter plot, one surface plot
ax1 = fig.add_subplot(121,projection='3d')
ax2 = fig.add_subplot(122,projection='3d')
# Scatter plot
surf1 = ax1.scatter(X1,Y1,Z1,cmap=cm.coolwarm)
ax1.set_xlim(0,1.5)
ax1.set_ylim(0,1.5)
ax1.set_zlim(0,3)
ax1.set_xlabel('x')
ax1.set_ylabel('y')
# Surface plot
surf2 = ax2.plot_surface(X1,Y1,Z1,cmap=cm.coolwarm)
ax2.set_xlim(0,1.5)
ax2.set_ylim(0,1.5)
ax2.set_zlim(0,3)
ax2.set_xlabel('x')
ax2.set_ylabel('y')
fig.colorbar(surf1, ax=ax1, location='top')
fig.colorbar(surf2, ax=ax2, location='top')
fig.savefig('Question10CompareScatterAndSurfacePlots')
The colour-bars make it evident that there are no negative z-points in the scatter plot while the surface plot stretches to negative z. Why is it so?

Related

Python - setting arbitrary contour xy-ratio

I am reading the following discussion:
setting axis scale in matplotlib contour plot
From the discussion above, to get arbitrary ratio, we could use
plt.figure(figsize=(8,2))
# ...
plt.tight_layout()
However, this setting is for figure not for contourf.
I used the above codes in my codes
import matplotlib.pyplot as plt
from matplotlib import cm
import numpy as np
import pandas as pd
import math
rm = pd.read_excel("test_3d.xlsx", header = None)
# find min values of noise
rec = np.shape(rm)
# grid
X = np.arange(1,rec[1]+1,1)
Y = np.arange(1,rec[0]+1,1)
x , y = np.meshgrid(X,Y)
# plots
plt.clf()
con = plt.contourf(x,y,rm, cmap=cm.jet)
plt.figure(figsize=(8,2))
plt.tight_layout()
plt.title('2457MHz')
plt.show()
The result I got is
The ratio of bottom plot is what I want; however, I use plt.figure(figsize=(8,2)), which is not for contourf. Therefore, I did not get the correct result.
Is there any way that I can plot arbitrary ratio for contourf?
Instead of setting the figsize, use Axes.set_aspect to change the aspect ratio of the contour plot's Axes:
fig, ax = plt.subplots()
ax.contourf(x, y, rm, cmap='viridis')
ax.set_aspect(0.25)
If you prefer to stick with the plt syntax, access the Axes using plt.gca:
plt.contourf(x, y, rm, cmap='viridis')
plt.gca().set_aspect(0.25)

Plotting a smooth surface with matplotlib

I am new to using the matplotlib module in Python, especially plotting 3D surfaces. I have generated a data matrix, where the data corresponds to the evaluation of
import numpy as np
z = np.linspace(0,3*np.pi,301)
x = np.linspace(0,2*np.pi,201)
in the function sin(z)sin(x), that is to say
Y = np.sin(z).reshape((301,1))*np.sin(x).reshape((1,201))
I have managed to plot this surface using the code
import matplotlib.pyplot as plt
fig = plt.figure(figsize=(10,6))
ax1 = fig.add_subplot(111, projection='3d')
mycmap = plt.get_cmap('gist_earth')
ax1.set_title('gist_earth color map')
surf1 = ax1.plot_surface(Z, X, Y.T, cmap=mycmap, rstride=2, cstride=2)
ax1.view_init(45, 45)
fig.colorbar(surf1, ax=ax1, shrink=0.5, aspect=5)
The result shows as follow:
Then, I want to do the same but adding a random error term in the data matrix. To do so, the new matrix would be defined as
Y = np.sin(zsam).reshape((301,1))*np.sin(xsam).reshape((1,201)) + np.random.normal(0,0.25,301*201).reshape((301,201))
However, when I try to plot this matrix, I obtain this:
How can I obtain a smooth surface as in the first case? Any help would be appreciated.

How to scale and set up RGB ortogonal axes in a 3D plot?

I'm trying to plot a wave function over one dimension but it has real and imaginary parts, so I did a 3D plot animation of it. This is a screenshot:
The main thing I would like to do is to spread it along the x-axis (which now is vertical) so it doesn't look squeezed. Also, it would be nice to set it up in a set of 3 RGB axes that intersect at the point (0,0,0). In the documentation I couldn't find any straight forward way to do this. I'm attaching the part of the code I'm using to animate it:
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import mpl_toolkits.mplot3d.axes3d as p3
fig = plt.figure()
ax = fig.gca(projection='3d')
line, = ax.plot(REAL[0,:],IMAG[0,:],x,"r",linewidth=0.5)
def animacio(i):
ax.collections.clear()
line.set_data(REAL[i,:],IMAG[i,:])
line.set_3d_properties(x, 'z')
return line,
ani = animation.FuncAnimation(fig,animacio,interval=50, frames=Nt,repeat=True)
nom = 'EvoluciĆ³_'
ani.save(str(nom)+'['+str(V0)+','+str(L)+','+str(l)+','+str(xi)+','+str(sigmax)+','+str(T)+']'+'.mp4', writer="ffmpeg", dpi=300)
plt.show()
print('Animation saved as: '+str(nom)+'['+str(V0)+','+str(L)+','+str(l)+','+str(xi)+','+str(sigmax)+','+str(T)+']'+'.mp4')
You can add colored lines to the plot, just by giving start and end points and assigning a color. The limits for the 'up'-axis can be set by ax.set_zlim. I created a demo curve that roughly resembles yours.
import numpy as np
from mpl_toolkits import mplot3d
import matplotlib.pyplot as plt
x = np.linspace(-10, 10, 1000)
y = np.sin(10*x)/(x*x+1)
z = np.cos(10*x)/(x*x+1)
ax = plt.axes(projection='3d')
ax.plot3D([0,0], [0,0], [-10,10], color='crimson')
ax.plot3D([0,0], [-1,1], [0,0], color='limegreen')
ax.plot3D([-1,1], [0,0], [0,0], color='dodgerblue')
line, = ax.plot3D(y, z, x, color='blueviolet')
ax.set_zlim(-1, 1)
plt.show()
At the left the plot without limiting, at the right with limits:
To get a more elongated view, you could use something like:
plt.gcf().set_size_inches(4, 12)

mplot3D fill_between extends over axis limits

I have questions related to creating a simple lineplot in Python with mplot3D where the area under the plot is filled. I am using Python 2.7.5 on RedHatEnterprise 7.2, matplotlib 1.2.0 and numpy 1.7.2.
Using the code below, I am able to generate a line plot. This is displayed as expected with the beginning / end of the plot set by the limits of the imported data set.
I am then trying to fill the area between the line plot and -0.1 using the answer given by Bart from Plotting a series of 2D plots projected in 3D in a perspectival way. This works, however, the filled area is continued beyond the limits of the data set. This is also the case when running the example from the link.
This screen shot shows the plot generated with filled area extending beyond the set axis limits.
How do I achieve that the filled area is only the range of the data set or the axis limits whichever is smaller?
How do I add a legend for those plots onto the figure?
Code as follows:
from numpy import *
import matplotlib.pylab as plt
from mpl_toolkits.mplot3d import Axes3D
x,y = genfromtxt("data.dat",unpack=True)
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.add_collection3d(plt.fill_between(x,y,-0.1, color='orange', alpha=0.3,label="filled plot"),1, zdir='y')
ax.plot(x,y,1,zdir="y",label="line plot")
ax.legend()
ax.set_xlim3d(852.353,852.359)
ax.set_zlim3d(-0.1,5)
ax.set_ylim3d(0,2)
ax.get_xaxis().get_major_formatter().set_useOffset(False)
plt.show()
I don't know how to put fill_between working the way you want it to, but I can provide an alternative using a 3D polygon:
from numpy import *
import matplotlib.pylab as plt
from mpl_toolkits.mplot3d import Axes3D
from mpl_toolkits.mplot3d.art3d import Poly3DCollection # New import
#x,y = genfromtxt("data.dat",unpack=True)
# Generated some random data
w = 3
x,y = np.arange(100), np.random.randint(0,100+w,100)
y = np.array([y[i-w:i+w].mean() for i in range(3,100+w)])
z = np.zeros(x.shape)
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
#ax.add_collection3d(plt.fill_between(x,y,-0.1, color='orange', alpha=0.3,label="filled plot"),1, zdir='y')
verts = [(x[i],z[i],y[i]) for i in range(len(x))] + [(x.max(),0,0),(x.min(),0,0)]
ax.add_collection3d(Poly3DCollection([verts],color='orange')) # Add a polygon instead of fill_between
ax.plot(x,z,y,label="line plot")
ax.legend()
ax.set_ylim(-1,1)
plt.show()
The code above generates some random data. Builds vertices from it and plots a polygon with those vertices. This will give you the plot you wish (but does not use fill_between). The result is:

Create legend for scatter plot using the label of the samples in matplotlib

I am using scatter plot in matplotlib to plot some points. I have two 1D arrays each storing the x and y coordinate of the samples. Also there is another 1D array that stores the label(to decide in which colour the point should be plotted). I programmed thus far:
import matplotlib.pyplot as plt
X = [1,2,3,4,5,6,7]
Y = [1,2,3,4,5,6,7]
label = [0,1,4,2,3,1,1]
plt.scatter(X, Y, c= label, s=50)
plt.show()
Now I want to be able to see which color corresponds to which label?
I looked up the implementation of legends in matplotlib like the one here:
how to add legend for scatter()?
However they are suggesting to create a plot for each label of sample. However all my labels are in the same 1D array(label). How can I achieve this?
You could do it with a colormap. Some examples of how to do it are here.
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.colors as colors
X = [1,2,3,4,5,6,7]
Y = [1,2,3,4,5,6,7]
label = [0,1,4,2,3,1,1]
# Define a colormap with the right number of colors
cmap = plt.cm.get_cmap('jet',max(label)-min(label)+1)
bounds = range(min(label),max(label)+2)
norm = colors.BoundaryNorm(bounds, cmap.N)
plt.scatter(X, Y, c= label, s=50, cmap=cmap, norm=norm)
# Add a colorbar. Move the ticks up by 0.5, so they are centred on the colour.
cb=plt.colorbar(ticks=np.array(label)+0.5)
cb.set_ticklabels(label)
plt.show()
You might need to play around to get the tick labels centred on their colours, but you get the idea.

Categories