Plotting a smooth surface with matplotlib - python

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.

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)

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

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?

2D Density or Frquency Scatter Plot in python

How can I make a scatter plot colored by density in matplotlib?
When I plot a colorbar it shows density scale, I want counts/percentage instead. How to convert density estimation to frequency counts?
Expected result is Fig.3 on page 8 of this paper: https://www.atmos-meas-tech.net/9/3293/2016/amt-9-3293-2016.pdf
If anyone can guide me to draw a plot similar to one shown in paper, it will be really helpful. Thank you in advance.
import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import gaussian_kde
# Generate fake data
x = np.random.normal(size=1000)
y = x * 3 + np.random.normal(size=1000)
# Calculate the point density
xy = np.vstack([x,y])
z = gaussian_kde(xy)(xy)
fig, ax = plt.subplots()
cax=ax.scatter(x, y, c=z, s=10, cmap=plt.cm.jet)
cbar = fig.colorbar(cax)
plt.show()
Another Method Tried:
#libraries
import matplotlib.pyplot as plt
import numpy as np
from scipy.stats import kde
# create data
x = np.random.normal(size=500)
y = x * 3 + np.random.normal(size=500)
# Evaluate a gaussian kde on a regular grid of nbins x nbins over
nbins=50
k = kde.gaussian_kde([x,y])
xi, yi = np.mgrid[min(x):max(x):nbins*1j, min(y):max(y):nbins*1j]
zi = k(np.vstack([xi.flatten(), yi.flatten()]))
# Add color bar
plt.pcolormesh(xi, yi, zi.reshape(xi.shape), cmap=plt.cm.jet)
plt.colorbar()
plt.show()
That's not a histogram, it's just sampling your kde on a grid...
Try this: plt.hist2d(x, y)
you can specify the bins using bins=whatever argument and many more options...

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:

Colormap in Matplotlib without using the "scatter" function

I have constructed a scatter plot with x and y positions. Now I have an array with a third variable, density, and I want to assign a color for each point in my scatter plot depending on its density value. I know how to do it using the "scatter" task of matplotlib, for example:
x = [1,2,3,4]
y = [5,3,7,1]
density = [1,2,3,4]
map = plt.scatter(x, y, c=density)
colorbar = plt.colorbar(map)
Now, I would like to do the same using the "plot" function instead, something like:
map = plt.plot(x,y, '.', c=t)
I am trying to do an animation of a galaxy merger, and assign each particle a color depending of the density of that region. So far the code only works with the "plot" task, so I need to implement it that way, but all the examples I've found use the former way.
Thanks in advance!
First off, #tcaswell is right. You're probably wanting to animate a scatter plot. Using lots of plot calls for this will result in much worse performance than changing the collection that scatter returns.
However, here's how you'd go about using multiple plot calls to do this:
import numpy as np
import matplotlib.pyplot as plt
xdata, ydata, zdata = np.random.random((3, 10))
cmap = plt.cm.gist_earth
norm = plt.Normalize(zdata.min(), zdata.max())
fig, ax = plt.subplots()
for x, y, z in zip(xdata, ydata, zdata):
ax.plot([x], [y], marker='o', ms=20, color=cmap(norm(z)))
sm = plt.cm.ScalarMappable(norm, cmap)
sm.set_array(zdata)
fig.colorbar(sm)
plt.show()
Just for comparison, here's the exact same thing using scatter:
import numpy as np
import matplotlib.pyplot as plt
xdata, ydata, zdata = np.random.random((3, 10))
fig, ax = plt.subplots()
scat = ax.scatter(xdata, ydata, c=zdata, s=200, marker='o')
fig.colorbar(scat)
plt.show()
If you wanted to change the position of the markers in the scatter plot, you'd use scat.set_offsets(xydata), where xydata is an Nx2 array-like sequence.

Categories